Stack Data Structure: Implementation, Operations, Applications
1. INTRODUCTION
What is Stack?
Stack Representation
ADT specification of Stack
Operations on Stack
Stack Overflow and Stack Underflow
Implementing of Stack
Prefix, Postfix, Infix Notation
Polish Notation
Precedence and Priority
Applications
2. WHAT IS STACK??
Stores a set of elements in a particular order.
Stack is like a container of memory.
Stack principle- LAST IN FIRST OUT(LIFO)
Examples:-
5. ADT SPCIFICATIONS OF STACK
Operations:
Make Empty
Boolean Is Empty
Boolean Is Full
Push (Item Type new Item)
Pop (Item Type& item) (or pop and top)
6. TOP
A list with the restriction that insertion and deletion
can be performed only from one end is called top.
If stack is empty, then top is at -1 position.
If stack is not empty, then top is at last position(n-1).
7. PUSH OPERATION
Stack is empty, top is at -1 position.
Function: Adds new Item to the top of the stack.
Preconditions: Stack has been initialized and is not full.
Post conditions: new Item is at the top of the stack.
8. PSEUDO CODE IN C
Void push() {
int item;
printf(“Enter item to push”);
scanf(“%d”.&item);
if(top == max – 1) { // overflow condition
printf(“Stack overflow”);
}
else {
top = top + 1;
stack[top] = item;
}
}
10. POP OPERATION
Stack is not empty, then top is at last position (n-1).
Removes top Item from stack and returns it in item.
Always done on the top of stack
The top most element is deleted first
Preconditions: Stack has been initialized and is not
empty.
Post conditions: Top element has been removed from
stack and item is a copy of the removed element.
11. PSEUDO CODE IN C
Void pop () {
if(top == -1)
printf(“Stack underflow”);
else {
printf(“The deleted item is %d”,stack[top]);
top--;
}
}
20. POLISH NOTATION
Prefix notation requires that all operators precede the two
operands that they work on.
Postfix notation requires that its operators come after the
corresponding operands.
22. INFIX TO POSTFIX
The infix string from left to right.
Initialize an empty stack.
Character is an operand, add it to the Postfix
string.
If the stack is empty, push the character is an
operator to stack
stack is not empty, compare the precedence of
the character with the element on top of the
stack.
26. EVALUATION OF POSTFIX
Create a stack to store operands (or values).
If the element is a number, push it into the stack.
If the element is a operator, pop operands for the operator
from stack.
Evaluate the operator and push the result back to the stack
when the expression is ended, the number in the stack is
the final answer.
28. EVALUATION OF PREFIX
Accept a prefix string from the user.
The string from the right one character at a time.
If it is an operand, push it in stack.
If it is an operator, pop and perform the operation,
specified by the operator.
Push the result in the stack.
The number in stack is final answer.