2. Stack
Stack Operations
Linked List Implementation of a Stack.
Stack Applications
3. Stack
An Ordered list in which only two operations are
permissible:
Insertion
Deletion
A List (ADT) with restriction that insertions and
deletions can be performed at only one position i.e. the
end of the list called top.
Items are stored and retrieved in a last-in, first –out
(LIFO) manner.
4. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
top
5. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
Push (S, B)
top
6. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
top
7. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D) top
8. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D)
Pop ()
top
9. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D)
Pop ()
Pop ()
top
10. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D)
Pop ()
Pop ()
Pop ()
top
11. Stack
Fundamental operations on a stack are Push, which is
equivalent to insert & Pop which is equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D)
Pop ()
Pop ()
Pop ()
Pop ()
top
12. Stack
Push by inserting at the front of the list.
Pop by deleting the element at the front of the list.
Top examines the element at the front of the list and
returns its value.
21. Push Operation (Book Imp.)
S
PtrToNodeTmpCell = malloc (sizeof(struct Node));
TmpCell Element = “A”;
TmpCell
22. Push Operation (Book Imp.)
PtrToNodeTmpCell = malloc (sizeof(struct Node));
TmpCell Element = “A”;
TmpCell Next = S Next;
S Next = TmpCell;
TmpCell
S
23. Push Operation (Book Imp.)
PtrToNodeTmpCell = malloc (sizeof(struct Node));
TmpCell Element = “B”;
S
TmpCell
24. Push Operation (Book Imp.)
TmpCell
S
PtrToNodeTmpCell = malloc (sizeof(struct Node));
TmpCell Element = “B”;
TmpCell Next = S Next;
25. Push Operation (Book Imp.)
TmpCell
S
PtrToNodeTmpCell = malloc (sizeof(struct Node));
TmpCell Element = “B”;
TmpCell Next = S Next;
S Next = TmpCell;
31. Array Implementation of Stack
Associated with each stack is TopOfStack, which is -1 for
an empty stack.
To push some element X onto the stack.We increment
TopOfStack and then set Stack[TopOfStack] = X.
To pop we set the return value to Stack[TopOfStack]
and then decrement.
32. Array Implementation of Stack
Struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
}
35. Array Implementation of Stack
Stack CreateStack(int MaxElements)
{
if (MaxElements < MinStackSize)
{
Error (“Stack size is too small”);
}
Stack S = malloc(sizeof(StackRecord));
SArray =
malloc(sizeof(ElementType)*MaxElements);
SCapacity = MaxElements;
return S;
}
36. Stack Applications
Balancing Symbols
Read Characters till EOF
If Character == opening symbol then Push (Character)
If Character == closing symbol && !IsEmpty(Stack) then Pop( )
If Poped Symbol isn't the corresponding opening symbol then
Error( )
At EOF IsEmpty(Stack) == False then Error( )
37. Stack Applications
Postfix Calculators
Postfix notation is : Operand Operand Operator
5 4 + , 6 5 3 4 * 4 4 + 8 * + 3 *
Easiest way to calculate is by using a stack.
When input == Operand than Push (Number)
When input == Operator than Pop ( Two Pushed
Numbers) and apply the operator and Push (Result).
Execute couple of examples:
6 5 2 3 + 8 * + 3 + * = 288
5 1 2 + 4 * + 3 − = 14
40. Stack Applications
Infix to Postfix Conversion
If input = operand : add it to the output.
Else if input = operator, o1 :
1) while there is an operator, o2, at the top of the stack, and either o1 is associative
or left-associative, and its precedence is less than or equal to that of o2, or o1 is
right-associative and its precedence is less than that of o2, pop o2 off the stack,
onto the output
2) push o1 onto the operator stack. Else if the input is a left parenthesis, then push
it onto the stack.
Else if the input is a right parenthesis, then pop operators off the stack,
onto the output until the token at the top of the stack is a left
parenthesis, at which point it is popped off the stack but not added to
the output queue. If the stack runs out without finding a left parenthesis,
then there are mismatched parentheses.
When there are no more inputs, pop all the operators, if any, off the
stack, add each to the output as it is popped out and exit. (These must
only be operators; if a left parenthesis is popped, then there are
mismatched parentheses.)
42. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operand “a” is read passed to the output.
Operator “+” is read pushed into the stack.
Operand “b” is read passed to the output.
43. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operator “ * “ is read, top of the stack has lower
Precedence than * , so * is pushed.
Operand “c” is read passed to the output.
44. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operator “+” is read. top of the stack has higher
Precedence than + so * is poped. Other + is also poped
As it has equal precedence as “+”. + is pushed.
45. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operator “(” is read.
Highest Precedence “(“ is pushed.
Operand “d” is read passed to the output..
46. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operator “ * ” is read & pushed.
47. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operator “+” is read.
Operator “*” is pop passed to the output then “+” is
Pushed.
48. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operator “)” is read.
Stack is emptied back to “)”. + is passed to the output
49. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Operator “ * ” is read & pushed.
Operand “g” is read and passed to the output.
50. Stack Applications
Infix to Postfix Conversion
a+b*c+(d*e+f)*g abc*+de*f+g*+
Input is empty so pop the stack and pass the operators
To output.