Unit 1 Stack (DSA)
Unit 1 Stack (DSA)
• Integer
• Float Linear Data structure Non Linear Data structure
• Character
• Array • Tree
• Stack • Graph
• Queue • Hash Table
• Linked list
Linear Data structure : In this elements are stored in contiguous memory locations or are arranged sequentially.
It can be constructed by using an array. The adjacency relationship is maintained between
elements.
e.g. Array, Stack, Queue, Linked List
Non Linear Data structure: In this the elements are not arranged sequentially or linearly. The adjacency
relationship is not maintained between data elements. They are not easy to
implement as compare to linear data structure. Data elements are attached in
hierarchical manner. Manoj Kataria BKBIET, pilani
e.g. Tree, Graph
STACKS(Unit-1)
Stack: It is a linear data structure in which elements are inserted and deleted only from one end, called top of stack.
It is LIFO(Last In First Out) the element inserted in last is taken out first or FILO(First In Last Out) the element
inserted in first is taken out in last. LIFO(Last In First Out)
e.g. Deck of cards
Stack of plates in cafeteria FILO(First In Last Out)
4
4 4
3
3 3
2
60 2 TOP 2
1
40 1 40 1 TOP
20 TOP
20 20
POP top element POP top element
TOP=TOP-1 or TOP-- TOP=TOP-1 or TOP--
Manoj Kataria BKBIET, pilani
POP operation
A stack can be implemented in two ways:
1. Using array - Static implementation
2. Using Linked List - Dynamic implementation
Static array representation of stack: we take an array of fixed size, so that we can’t push more elements in the stack
more than the size of stack.
e.g. #define MAX 10
int s[MAX]; or int s[10];
PUSH operation:
Steps: 1. Check if stack is Full
2. If stack is Full, then print “Overflow” and exit
3. If stack is not Full, increment TOP by one to point to next memory space
4. Add data item
5. Exit
} 40 1
20
initially stack is
empty
TOP=-1
Manoj Kataria BKBIET, pilani
POP operation:
Steps: 1. Check if stack is Empty
2. If stack is Empty, then print “Underflow” and exit
3. If stack is not Empty, access the element at the top of stack
4. Decrement TOP by one
5. Exit
stack1
stack1(index 0)
15 top1 15 top1
Intially both stacks are empty PUSH item onto stack1 PUSH item onto stack2
top1=top2=-1 top1=top1+1 top2=MAX-1
85 6
124 5
top2 is decremented for each PUSH operation
100 4 top2
top1 is incremented for each PUSH operation
3
top2 is incremented for each POP operation
56 top1 top1 is decremented for each POP operation
15
Manoj Kataria BKBIET, pilani
//push function for stack1 //push function for stack2
void push1(int item) void push2(int item)
{ {
if(top1==top2-1) if(top2==top1+1)
printf("\nstack is full"); printf("\nstack is full");
else else
{ {
top1++; top2--;
s[top1]=item; s[top2]=item;
} }
} }
5! = 5*4! = 5*24=120
4! = 4*3! = 4*6=24
3! = 3*2! = 3*2=6
2! = 2*1! = 2*1=2
1!=1
Steps:
1. Traverse the list and push all elements of list on to stack
2. Traverse the list again and pop a value from top of stack and put them in reverse order
1 TOP 1
2 2
3 3
Push Pop
Infix to Postfix transformation
a+b -> infix expression
ab+ -> postfix expression (reverse polish notation)
+ab -> prefix expression (polish notation)
For evaluating an expression we know the concept of precedence and associativity. When an expression
contains more than one operator, the order in which the operators are evaluated depends on their precedence
and associativity (L->R or R->L)
If the precedence level is same then the order of evaluation depends on their associativity.
+ - / * -> L->R
++ -- -> R->L
Infix expressions are readable and solvable by humans. We can easily distinguish the order of operators and
can use the parenthesis to solve that part first during solving expression. The computer cannot differentiate
the operators and parenthesis easily that’s why postfix conversion is needed.
Difficulties in dealing with infix expression:
There is great deal of ambiguity in finding the value of infix expression.
e.g 10+8*6 = 58
(10+8)*6=108
To overcome this problem, expressions are evaluated by using one of the two forms: a). Postfix b). Prefix
The fundamental property of these two expressions is that the order in which the operations are performed
completely determined by the operators and operands in the expression.
To convert infix expression into postfix we use STACK(holds operators during conversion).
The reason for using STACK is that it reverses the order of operators in the expression.
*If an incoming operator has higher precedence than the operator on the top stack, push it on to stack
*If an incoming operator has lower or equal precedence than the operator on the top stack, pop from stack and
add to P and then push the incoming operator on to stack
Algorithm:
1. Add ‘)’ right parenthesis to postfix expression
2. Read postfix expression from left to right until ‘)’ is encountered
3. If an operand is encountered PUSH it on to stack
4. If an operator is encountered, POP two elements
a). A is the top element
b). B is next to top
c). Evaluate B operator A (e.g. B + A)
PUSH result on to stack. (got to step2)
5. POP result from stack
6. END
e.g. ( A + B ^ C ) * D + E ^ 5
Disks
Rules: our main aim is to move all disks from one tower to another tower without violating the sequence of
arrangement.
• Only one disk can be moved among tower at any given time
• Only the top disk can be removed
• No large disk can sit over a small disk.
Steps:
1. Move (n-1) disks from source to auxiliary
2. Move nth disk from source to destination
3. Move (n-1) disks from auxiliary to destination
For C implementation
Function : tower(n, beg, aux, end)
tower(0,B,A,C)
tower(0,A,C,B)
tower(2,B,A,C) Move B->C 6
tower(0,B,A,C)