Module 2-ND
Module 2-ND
MODULE 2
INTRODUCTION
The linear lists and linear arrays allows one to insert
and delete elements at any place in the list- at the
beginning, at the end or in the middle
There are certain frequent situations when one wants
to restricts insertions and deletions so that they can
take place only at the beginning or the end of the list,
not in the middle
They are STACKS and QUEUES
What is a stack?
It is an ordered group of homogeneous items of elements.
Elements are added to and removed from the top of the
stack (the most recently added items are at the top of the
stack).
The last element to be added is the first to be removed
(LIFO: Last In, First Out).
STACKS
Stack is a linear list in which addition and deletion of
elements are restricted to one end called the top.
A linear list is a list in which each element has unique
successor.
A linear list may be a restricted list or a general list.
Stack is also called as
o LIFO Data structure
o Push down list
Stack Operations
PUSH – Used to insert an element into Stack.
POP – Used to delete an element from Stack.
Underflow is a condition occurs if we try to POP item
from the empty STACK
Overflow is a condition occurs if we try to PUSH an
ITEM to the STACK which is already full
Array representation of Stack
Array can be used as home to the stack
Data structure
#define MAX _STK 25
typedef struct {
int top;
char item[MAX_STK];
}STACK;
STACK s;
s.top= -1;
A B C D
top
Stack after – PUSH( ‘A’), PUSH( ‘B’), PUSH( ‘C’),
PUSH( ‘D’)
Algorithm to Push the element
PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a stack
1.[ Stack already filled? ]
If TOP = MAXSTK, then: print: OVERFLOW, and
return
2. Set TOP := TOP + 1 [ Increases TOP BY 1]
3. Set STACK[ TOP ] := ITEM [ Inserts ITEM in new
TOP position ]
4. Return
Algorithm to pop the element
POP(STACK, TOP, ITEM)
This procedure deletes the top element of STACK and
assigns it to the variable ITEM
1. [ Stack has an item to be removed? ]
If TOP = -1, then: Print : UNDERFLOW, and Return
2. Set ITEM := STACK[ TOP ]. [ Assigns TOP element to
ITEM ]
3. Set TOP := TOP – 1. [ Decreases TOP by 1 ]
4. Return
Stack representation using arrays in C
#define MAXSTK 10
typedef struct {
int item[MAXSTK];
int top;
}STACK;
STACK s;
s.top = -1;
Stacks using dynamic arrays
#define MAX 25
typedef struct {
int key;
/*other fields*/
}Element;
Element * stack;
int top= -1; top =0
capacity = 1; 10 (1000-1004)
stack = (Element *) malloc (sizeof (*stack)); 1000-1004
int IsFull( ) {
if ( top >= capacity – 1) stack capacity =1
return 1;
return 0;
}
Void Push(Element item)
{
If(IsFull( ))
Stackfull( );
Top++;
* stack=item;
return;
}
Stackfull( )
{
Stack=realloc(2*capacity*size of (*stack)); 2000-2008
Capacity*=2;
}
int Isempty( ){
If(top <0)
return 1;
return 0;
}
Element Pop( )
{
Element x;
If(!Is empty(s)
{
X=*stack;
top--;
return x;
}
}
Array doubling
void stackFull()
{
realloc(s , 2*capacity*sizeof(*s));
capacity * = 2;
}
Stack Applications
Expression Conversion d
C
Reversing Data
B
Expression Evaluation A
Recursion. Sum() s=abcd
{ sum()
{ sum()
3+4
Infix= 3+4 (operator between operands)
Postfix (REVERSE POLISH or SUFFIX )=34+
(operator is at end)
Prefix (polish)=+34 (operator is at the beginning)
Postfix Expression
62 / 3 – 4 2 * +
Token
Operand,
6 PUSH onto
stack
stack
Postfix Expression
2/3–42*+
6
stack
Postfix Expression
/3–42*+
Token
Operand,
2 PUSH onto
stack
6
stack
Postfix Expression
/3–42*+
2
6
stack
Postfix Expression
3–42*+
Token
Operator, POP
/ 2 top elements
from the stack
2
and store it in
6 opnd2 and
opnd1
stack
Postfix Expression
3–42*+
opnd1 opnd2
6 2
stack
Postfix Expression
3–42*+
Evaluate
opnd1 opnd2
opnd1
6 2 operator
opnd2
stack
Postfix Expression
3–42*+
Postfix Expression
3–42*+
3
stack
Postfix Expression
–42*+
Token
Operand,
3 PUSH onto
stack
3
stack
Postfix Expression
–42*+
3
3
stack
Postfix Expression
42*+
Token
Operator, POP
- 2 top elements
from the stack
3
and store it in
3 opnd2 and
opnd1
stack
Postfix Expression
42*+
Evaluate
opnd1 opnd2
opnd1
3 3 operator
opnd2
stack
Postfix Expression
42*+
Postfix Expression
42*+
0
stack
Postfix Expression
2*+
Token
Operand,
4 PUSH onto
stack
0
stack
Postfix Expression
2*+
4
0
stack
Postfix Expression
*+
Token
Operand,
2 PUSH onto
stack
4
0
stack
Postfix Expression
*+
2
4
0
stack
Postfix Expression
+
Token
Operator, POP
2 * 2 top elements
from the stack
4
and store it in
0 opnd2 and
opnd1
stack
Postfix Expression
+
Evaluate
opnd1 opnd2
opnd1
4 2 operator
opnd2
0
stack
Postfix Expression
+
Postfix Expression
+
8
0
stack
Postfix Expression
Token
Operator, POP
+ 2 top elements
from the stack
8
and store it in
0 opnd2 and
opnd1
stack
Postfix Expression
Evaluate
opnd1 opnd2
opnd1
0 8 operator
opnd2
stack
Postfix Expression
Postfix Expression
8
Infix to postfix conversion algorithm
Reverse polish(Q, P) // Q is an arithmetic expression in INFIX notation
Algorithm converts INFIX expression Q into POSTFIX expression P
1 push “(“ onto STACK, and append “)” to the end of Q
2 scan Q left to right and repeat Steps 3 to 6 for each element of Q until the
STACK is empty
3 If an operand is encountered place it in the POSTFIX expression P
4 If an left parenthesis is encountered, push it on to the STACK
5 If an operator is encountered, call it as incoming operator, then:
a) Repeatedly pop from the STACK and place it in the P which has the same
precedence or higher precedence than the incoming operator
b) b) Place the incoming operator to the STACK
[End of IF]
6 If a right parentheses is encountered
a) Repeatedly pop from the STACK and place it in the P until left parenthesis
is encountered
b) Remove the left parenthesis
[ End of If ]
[ End of Step 2 loop ]
Infix to postfix conversion
infixVect
(a+b-c)*d–(e+f)
postfixVect
Infix to postfix conversion
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix to postfix conversion
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix to postfix conversion
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix to postfix conversion
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix to postfix conversion
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix to postfix conversion
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix to postfix conversion
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix to postfix conversion
stackVect
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix to postfix conversion
stackVect
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix to postfix conversion
stackVect
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Precedence OF Operators
+ - 2 1 Left
* / % 4 3 Left
^ $ 5 6 Right
operands 8 8 Left
( 0 9 Left
) 0
Infix to Postfix Conversion using Stack: Example
main()
{
int m,n;
printf("Enter two numbers :: \n");
scanf("%d%d",&m,&n);
printf("\nOUTPUT :: %d\n",A(m,n));
}
Front of queue
6-110
Conceptual View of a Queue
Removing an element
Element is removed
from the front of the
queue
6-111
Queue
Inserting an item into queue
void enqueue( queue , item)
{
if(rear==maxsize-1)
printf(queue is full)
else
rear=rear+1
queue[rear]=item
}
0 1 2 3
6 7 4 8
Deleting an item from queue
void dequeue()
{
if(rear==front)
printf(queue is empty)
else
{ front=front+1
return queue[front]
}
}
0 1 2 3
5
Various Queues
Normal queue (FIFO)
Circular Queue (Normal Queue)
Double-ended Queue (Deque)
Priority Queue
Multiple Queue
rear=0 rear=rear+1%queuesize
0+1 % 8==1
1+1%8===2
7+1%8==0
8+1%8==1
Inserting and Deleting an item into queue using
count
void dqueue( queue , item)
void enqueue( queue , item) {
{ if(count==0)
if(count==maxsize) {
{ printf(queue is empty)
printf(queue is full) return
return; }
} else
else {
{ rear=(rear+1)%maxsize front=(front+1)%maxsize
queue[rear]=item; return(queue[front])
count++ count--
} }
} }
0 1 2 3
Inserting an item into queue
void enqueue( queue , item)
{
if(front==(rear+1)%maxsize)
{
printf(queue is full)
return
else
{
rear=(rear+1)%maxsize
queue[rear]=item;
}
}
0 1 2 3
Deleting an item from queue
void dqueue( queue , item)
{
if(front==rear)
{
printf(queue is empty)
return
}
else
{
front=(front+1)%maxsize
return(queue[front])
}
}
0 1 2 3
2 3 4
Display the content of queue
void display( queue , item)
{
if(front==rear)
{
printf(queue is empty)
return
else
{
while(front!=rear)
front=(front+1)%MAX;
printf("%c\t", Q[front]);
}
}
0 1 2 3
2 3
Double Ended Queue
Operations
Insert at rare end
Insert at front end
Delete from front end
Delete from rear end
Display contents of dqueue
0 1 2 3 4 5
33
Insert at front end
Case 1: when queue is empty
if(f=-1 & r=-1)
{r=r+1; q[r]=item;
return}
Case2 : When some elements are deleted
if(f!=-1)
{ q[f]=item ;
f=f-1; return}
Case 3:Some items are inserted but not deleted
4insertion at57front is not5 possible
Delete at rear end
If(f=-1 & r=-1)
dqueue is empty ; return;
else item=q[r--];
if(f==r)
f=r=-1;
Priority queues (pq)
Priority Queue is a data structure in which the elements are
assigned priority such that the order in which elements are
deleted and processed follow the following rule
Element with the higher priority is processed before
elements with lower priority
Elements with same priority are processed in the order they
are added to the queue
2 types of PQ are
Ascending PQ (APQ): In which items are inserted
arbitrarily and from which smallest item can be removed.
The basic operations are pqinsert(apq,x) and
pqmindelete(apq)
Descending PQ (DPQ): In which items are inserted
arbitrarily and from which largest item can be removed. The
basic operations are pqinsert(dpq,x) and pqmaxdelete(dpq)
1. normal insertion & special deletion
A 3
B 2
C 7
2. B A C
FRONT REAR
3
1 2 3 4 5 6
3
1 A
1 3
2 B C X
0 0 3
1 6
4 D E F
5 G
4 4
A-1
B-2
C-2
X-2
D-4
E-4
F-4
Alogorithm
Algorithm enqueue(q[] , item)
{ if(rear=maxsize-1)
printf(“queue is full) insert 5,pos=-1, rear=0
else { pos=rear insert 2 pos=0 ,rear=1
rear=rear+1 insert 4 pos=1 ,rear=2
while(pos>=0 && q[pos]>=item
{ q[pos+1]=q[pos]
pos=pos-1;
}
q[pos+1]=item
if(front=-1)
Mazing Problem
Example
Algorithm
Two Stacks in Single Array
S[++TOP]=ITEM;
TOP=TOP+1
S[TOP]=ITEM
Implementation
Multiple Stacks
Example
Multiple Stacks
0 1 2 3 4 5 6 7 8 9 10 11 12
Push operation on multiple stack
Void push(int i , int item) //add item to ith stack
{ if (top[i]==boundary[i+1])
{ print(stack i is full); return }
stack[++top[i]]=item
}
Top2= boundary[2] top3= b[3] top1=-1
0 1 2 3 4 5 6 7 8 9 10 11 12
3 4
Pop operation on multiple stack
Void pop(int i) //remove the top item from the ith
stack
{ if (top[i]==boundary[i])
{ return stack i is empty ; return; }
return stack[top[i]--];
}
0 1 2 3 4 5 6 7 8 9 10 11 12
Display operations on multiple stack
Void Display() //remove the top item from the ith
stack
{ if (top[i]==boundary[i])
{ return stack i is empty ; return; }
for(j=boundary[i]+1;j<=top[i];j++)
printf(“%d”, stack[j])
}
0 1 2 3 4 5 6 7 8 9 10 11 12
4 3 3
Multiple Queues