7 Stacks
7 Stacks
7 Stacks
Overview
Stack
A stack is a linear data structure or abstract data type for collection of items, with the
restriction that items can be added one at a time and can only be removed in the
reverse order in which they were added.
The last item represents the top of the stack
It is also known as LIFO (Last In First Out) or FILO(First In Last Out) data structure.
TOP
Basic Operations
Insert Delete
ABCDE EDC
E TOP
D
C
B B TOP
A A
Implementation
Create stack
C code: #define SIZE 10
struct stack{
int array[SIZE];
int TOP;
};
Initialize stack:
Algorithm Init_Stack (struct stack* STK): This algorithm initializes a stack with TOP = -1. Here,
STK is a pointer to a structure. TOP represents the index of the last item in a stack.
1. STK -> TOP = -1 // -1 means stack is empty
Illustration
SIZE = 8 Push 8, 56, 57, 7, 56, 9
0 1 2 3 4 5 6 7
5 8 56 57 7 56 9
TOP
Push 89, 90
7 8 56 57 7 56 9 89 90
TOP
4 Times Pop
3 8 56 57 7
TOP
Push operation
Push operation
Below mentioned algorithm inserts an element into stack STK. Item is an element which is to
be inserted.
This algorithm returns an element from stack TOP. Item is local variable.
C code:
struct Node{
int INFO;
struct Node *NEXT;
};
Initialize stack:
Algorithm Init_LStack( struct Node *TOP): This algorithm initializes a stack by creating empty
linked list. TOP represents the last item of stack and pointing to empty linked list.
Push 8, 56, 57
8 NULL
1. Push 8
TOP
2. Push 56 56 8 NULL
TOP
57 56 8 NULL
3. Push 57
TOP
Push operation
This algorithm inserts an element into stack by inserting as first node into linked list. Item is
element which is to be inserted. TOP is pointing to last inserted node i.e. first node of linked
list.
57 56 8 NULL
TOP
56 8 NULL
1. Pop
TOP
8 NULL
2. Pop
TOP
4. Pop Underflow
Pop operation
This algorithm deletes and returns an element from stack TOP. Item is local variable.
1. Node* Temp=TOP
2. While(Temp!=NULL)
3. printf(“%d\n”,Temp->INFO);
4. Temp=Temp->NEXT;
Assume that given expression does not contain any unary operator.
For same level operations, precedence are performed from left to right.
Operations Precedence
Exponential (↑) 4
Multiplication (*) 3
Division (/) 3
Modulus (%) 3
Addition (+) 2
Subtraction (-) 2
Left Parenthesis 1
Right Parenthesis 1
InFix Expression –
1. A+B*C-D/E
2. A*B-(C+D)+E
3. A + (B * C – (D / E ↑ F) * G) * H
PostFix Expression –
1. ABC*+DE/-
2. AB*CD+-E+
3. ABC*DEF↑/G*-H*+
Infix to Prefix
Algorithm Infix_To_Pre (InFix) – This algorithm takes infix expression and returns
equivalent prefix expression.
InFix Expression –
1. A+B*C-D/E
2. A*B-(C+D)+E
3. A + (B * C – (D / E ↑ F) * G) * H
PreFix Expression –
1. -+A*BC/DE
2. +-*AB+CDE
3. +A*-*BC*/D↑EFGH
Postfix to Infix Conversion
Algorithm Postfix_To_Infix (PostFix) – This algorithm takes postfix expression and
returns equivalent infix expression. In this case, we push and pop the strings. str1,
str2 are the temporary variables holding strings.
C D
B B (B * C) (B * C)
A A A A A
E
D (D / E)
(B * C) (B * C) ((B * C) – (D / E)) (A + ((B * C) – (D / E)))
A A A
Example –
23–1+43*+ = 12
3
2 2 – 3 = -1
3
1 4 4 12
-1 -1 0 0 0 0 12
Exercises
PostFix Infix
2. 2 2 * 3 – ((2 * 2 ) – 3) = 1
3. 7 6 2 ^ * 4 / 8 - (((7 * (6 ^ 2)) / 4) – 8) = 55
PostFix Expression –
1. ABC*+DE/-
2. AB*CD+-E+
3. ABC*DEF↑/G*-H*+
Prefix to Infix Conversion
Algorithm Prefix_To_Infix (PreFix) – This algorithm takes prefix expression and
returns equivalent infix expression. In this case, we push and pop the strings. str1,
str2 are the temporary variables holding strings.
A
B B
C (C / D)
(C / D) (C / D)
D D
(A – B)
(C / D) ((A – B) * (C/D))
PreFix Expression –
1. -+A*BC/DE
2. +-*AB+CDE
3. +A*-*BC*/D↑EFGH
Evaluation of Prefix Expression
Example –
Reverse
+-423 324-+
4 Infix:
2 4 - 2 =2 ((4 - 2) + 3) = 5
3
2
3 5
Exercises
PreFix Infix
1. - * 9 9 81 ((9 * 9) – 81) = 0