1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Stack
Abstract Data Type (ADT), commonly used in most programming languages.
Linear Data Structure
This structure is following LIFO principle
(LIFO) Last-in-First-out
The elements which is placed (inserted or added) last, is accessed first.
push()
Pushing (storing) an element on the stack
pop()
Removing (accessing) an element from the stack
peek()
This get the top data element of the stack, without removing it
isFull()
Check if stack is full
isEmpty()
check if stack is empty
int peek() {
return stack[top];
}
Algorithm of peek() function
bool isempthy() {
if (top == -1)
return true;
else
return false;
}
Algorithm of isfull() function
bool isfull() {
if (top == MAXSIZE)
return true;
else
return false;
}
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success
Push Operation
void push (int data) {
if (!isFull()) {
top = top +1;
stack[top] = data;
}eslse {
printf (“Could not insert data, Stack is full.\n”);
}
}
Algorithm for PUSH Operation
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Pop Operation steps
int pop (int data) {
if (!isempty()){
data = stack [top];
top = top - 1;
return data;
} else {
printf (“Could not retrieve data, Stack is empty.\n”);
}
}
Algorithm for Pop Operation
Functional call management in recursion
Undo mechanism in text editors
Syntax parsing in compilers
Backtracking algorithms (ex. Solving Mazes)
Application of Stack
Simplicity, Efficiency, Last-in, First-out (LIFO), Limited memory usage
Advantages of Stack Data Structure
Limited Access, Potential for Overflow, Not suitable for random access, Limited Capacity
Disadvantages of Stack Data Structure