Stack
operations performed on stack
stack applications
Infix to postfix conversion
Infix to prefix conversion
Postfix to infix conversion
Prefix to infix conversion
algorithm to push an element in a stack
algorithm to pop an element from a stack
2. CONteNtsCONteNts
staCk
OperatiONs perfOrmed ON staCk
staCk appliCatiONs
iNfix tO pOstfix CONversiON
iNfix tO prefix CONversiON
pOstfix tO iNfix CONversiON
prefix tO iNfix CONversiON
algOrithm tO push aN elemeNt iN a staCk
algOrithm tO pOp aN elemeNt frOm a
staCk
03/17/18 BY MS. SHAISTA QADIR 2
3. staCkstaCk
03/17/18 BY MS. SHAISTA QADIR 3
A stack is a linear data structure that can be accessed only
at one of its ends for storing and retrieving data.
Such a stack resembles a stack of trays in a cafeteria: New
trays are put on the top of the stack and taken off the top.
The last tray put on the stack is the first tray removed from
the stack.
For this reason, a stack is called an LIFO structure last
in/first out.
4. OperatiONs perfOrmed ONOperatiONs perfOrmed ON
staCkstaCk
03/17/18 BY MS. SHAISTA QADIR 4
The operations are as follows:
◦ clear() : Clear the stack.
◦ isEmpty() : Check to see if the stack is empty.
◦ push(el) : Put the element el on the top of the
stack.
◦ pop() : Take the topmost element from the
stack.
◦ topEl() : Return the topmost element in the
stack without removing it.
5. 03/17/18 BY MS. SHAISTA QADIR 5
Stack Overflow:
If adding an element onto the top of the stack and if that
stack is full this situation is called stack overflow.
Stack Underflow:
Removing an element from the stack and if that stack is
empty this situation is called stack underflow
OperatiONs perfOrmed ON
staCk
6. staCk appliCatiONsstaCk appliCatiONs
03/17/18 BY MS. SHAISTA QADIR 6
APPLICATIONS OF STACK:
◦ Reversing Data: We can use stacks to reverse data.
(example: files, strings) Very useful for finding
palindromes.
◦ To evaluate Arithmetic expressions: (Prefix, Infix, Postfix
Notations)
◦ Arithmetic expressions have
Operands (variables or numeric constants).
Operators
i. Binary : +, -, *, / ,%
ii. Unary: - (sign for negative numbers)
7. 03/17/18 BY MS. SHAISTA QADIR 7
Example:
Prefix Notation Infix Notation Postfix Notation
+A * B C A + B * C A B C * +
* + A B C (A+B) * C A B + C *
+ – A B C A – B + C A B – C +
– A + B C A – (B+C) A B C + –
*+AB-CD (A+B)*(C-D) AB+CD-*
Priority convention(Rules):
◦ Unary minus has Highest priority
◦ *, /, % have Medium priority
◦ +, - have Lowest priority
stack applicationsstack applications
8. 03/17/18 BY MS. SHAISTA QADIR 8
stack applicationsstack applications
Infix Expression Prefix Expression Postfix Expression
A + B * C + D + + A * B C D A B C * + D +
(A + B) * (C + D) * + A B + C D A B + C D + *
A * B + C * D + * A B * C D A B * C D * +
A + B + C + D + + + A B C D A B + C + D +
9. 03/17/18 BY MS. SHAISTA QADIR 9
stack applicationsstack applications
Example:
To evaluate the expression A+B*C using Postfix notation in
a
Stack
Postfix notation for A+B*C is ABC*+
PUSH A
PUSH B
PUSH C
MULTIPLY
ADD
POP
PUSH A PUSH B PUSH C MULTIPLY ADD POP
13. 03/17/18 BY MS. SHAISTA QADIR 13
Postfix to Infix conversionPostfix to Infix conversion
Example: abc-+de-fg-h+/* to infix
14. 03/17/18 BY MS. SHAISTA QADIR 14
Postfix to Infix conversionPostfix to Infix conversion
Example: abc-+de-fg-h+/* to infix (contd)
15. 03/17/18 BY MS. SHAISTA QADIR 15
Postfix to Infix conversionPostfix to Infix conversion
Example: abc-+de-fg-h+/* to infix (contd)
16. 03/17/18 BY MS. SHAISTA QADIR 16
Prefix to Infix conversionPrefix to Infix conversion
Example: *+a-bc/-de+-fgh to infix
17. 03/17/18 BY MS. SHAISTA QADIR 17
Prefix to Infix conversionPrefix to Infix conversion
Example: *+a-bc/-de+-fgh to infix (contd)
18. algorithm to push analgorithm to push an
element in a stackelement in a stack
03/17/18 BY MS. SHAISTA QADIR 18
ALGORITHM:
1. If (TOP == MAX-1) Then
2. Print: Overflow
3. Else
4. Set TOP = TOP + 1
5. Set STACK[TOP] = ITEM
6. Print: ITEM inserted EndIf
7. Exit.
19. algorithm to push analgorithm to push an
elementelement
03/17/18 BY MS. SHAISTA QADIR 19
PROGRAM LOGIC:
public boolean isFull()
{
if(top == maxsize-1)
return true;
else
return false;
}
public void push(int a)
{
++top;
arr[top] = a;
}
20. algorithm to pop analgorithm to pop an
element From staCKelement From staCK
03/17/18 BY MS. SHAISTA QADIR 20
ALGORITHM:
1. If (TOP == -1) Then
2. Print: Underflow
3. Else
4. Set ITEM = STACK[TOP]
5. Set TOP = TOP - 1
6. Print: ITEM deleted EndIf
7. Exit
21. algorithm to pop analgorithm to pop an
element From staCKelement From staCK
03/17/18 BY MS. SHAISTA QADIR 21
PROGRAM LOGIC:
public boolean isEmpty()
{
if(top == -1)
return true;
else
return false;
}
public int pop ()
{
int e = arr[top];
top--;
return e;
}