You are on page 1of 39

Data Structures

STACKS & QUEUES


Stack
• Stack is a linear data structure, where
insertion and deletion takes place at one
end.
• Stack follows Last In First Out
(LIFO)

top
Stacks
Stack Operations :
– void push(int);
– int pop(void);
– int IsFull(void);
– int IsEmpty(void);
Inserting and deleting elements in a stack

E top
D top D D top
C top C C C
B top B B B B
top A A A A A
STACK ADT
structure Stack is
objects: a finite ordered list with zero or more elements.
functions:
for all stack  Stack, item  element, max_stack_size
 positive integer
Stack CreateS(max_stack_size) ::=
create an empty stack whose maximum size is
max_stack_size
Boolean IsFull(stack, max_stack_size) ::=
if (number of elements in stack == max_stack_size)
return TRUE
else return FALSE
Stack Add(stack, item) ::=
if (IsFull(stack)) stack_full
else insert item into top of stack and return
Boolean IsEmpty(stack) ::=
if(stack == CreateS(max_stack_size))
return TRUE
else return FALSE
Element Delete(stack) ::=
if(IsEmpty(stack)) return
else remove and return the item on the
top of the stack.
IsFull()

int IsFull()
{
if(top==MAX_SIZE-1)
return 1;
else
return 0;
}
IsEmpty()
int IsEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
push()
void push(int element)
{
if(IsFull()){
printf("\t Stack Over Flow.............!");
}
else {
stack[++top]=element;
printf("Inserted.......!");
}
}
pop()

int pop(void)
{
if(IsEmpty())
{
printf("\n\t Stack Underflow............!");

}
else{
return stack[top--];
}
}
Stack Applications:
– Infix to postfix conversion
– Postfix expression evaluation
– Recursion/ Function calling
– Towers of Hanoi problem
– Symbol balancing
– Infix to prefix conversion
– Undo and Redo Operations
– Back button Operations in a web browser.
Infix to Postfix Conversion
Algorithm : Infix to Postfix conversion(E)

Input : E, Simple Arithmetic Expression in the Infix form delimited at


the end by ‘)’ .

Output : An Arithmetic Expression in Postfix form.

Functions :

Read_Symbol( ) : Read one symbol at a time from the Infix


expression from left to right.

ISP(symbol) : Returns the In stack priority value of the symbol.

ICP(symbol) : Returns the Incoming priority value of the symbol.


Infix to Postfix Conversion
Ouput(Symbol) : Appends the symbol to the resultant postfix
expression.

Push(Symbol) : Push the Symbol into Stack.

Pop( ) : Pop the top most element from Stack.

Data Structure : Array representation of Stack.

STEPS :

1) Push( ‘(‘ ) /* Initialize the Stack */


Infix to Postfix Conversion
2) While( top > =0 ) do

2.1) item= E.Read_Symbol( )


/* Scan the next Symbol in Infix Expression */

2.2) x=Pop( )

2.3) Case : item=operand

Push(x)
Ouput(item)

/* Add the item to the Ouput Expression */


Infix to Postfix Conversion
2.4) Case : item = ‘ ) ’

While( x != ‘(‘ ) do
Output(x)
x=Pop( )
End While

2.5) Case : ISP(x) >= ICP( item)


While(ISP(x)>=ICP(item) do
Output(x)
x=Pop( )
End While
Push(x)
Push(item)
Infix to Postfix Conversion

2.6) Case : ISP(x) < ICP( item)


Push(x)
Push(item)

End While

3) Stop
The orders of operands in infix and postfix are the same.
a + b * c, * > +
Token Stack Top Output
[0] [1] [2]
a -1 a
+ + 0 a
b + 0 ab
* + * 1 ab
c + * 1 abc
eos -1 abc*= abc*+

Translation of a+b*c to postfix


a *1 (b +c) *2 d
Token Stack Top Output
[0] [1] [2]
a -1 a
*1 *1 0 a
( *1 ( 1 a
b *1 ( 1 ab
+ *1 ( + 2 ab
c *1 ( + 2 abc
) *1 match )
0 abc+
*2 *2 *1 = *2
0 abc+*1
d *2 0 abc+*1d
eos *2 0 abc+*1d*2

Translation of a*(b+c)*d to postfix


Rules
(1) Operators are taken out of the stack as long as
their
in-stack precedence is higher than or equal to the
incoming precedence of the new operator.

(2) ( has low in-stack precedence, and high


incoming precedence.

( ) + - * / ^

Isp 0 - 2 2 4 4 6
Icp 7 0 1 1 3 3 5
Evaluation of Postfix expression
Algorithm : Eval_ Postfix expression(E)

Input : E, Arithmetic Expression in the Postfix form delimited by ‘$’

Output : Result of the Expression in Postfix form.

Functions :

Get_Next_Symbol( ) : Read one symbol at a time from the Postfix


expression from left to right.

Push(Symbol) : Push the Symbol into Stack.

Pop( ) : Pop the top most element from Stack.


Evaluation of Postfix expression

Data Structure : Array representation of Stack.

STEPS :

1) While(true)
Evaluation of Postfix expression
2)Do
2.1) x= E.Get_Next_Symbol( )
/* Scan the next Symbol from Postfix Expression */

2.2) Case : x=operand


Push(x)
2.3) Case : x=operator
o2=PoP( )
o1=PoP( )
res=o1 operator o2
Push(res)
2.4) Case : x=‘$’
Pop( )
Exit;
Evaluate the following Postfix
expressions
1) 2 3 4 * 6 / + $

2) 7 5 2 + * 4 6 1 - / - $
Towers of Hanoi problem The objective of the puzzle
is to move all the disks from
the leftmost peg to the
rightmost peg, adhering to
the following rules:

Move only one disk at a


time.

A larger disk may not be


placed on top of a smaller
disk.

All disks, except the one


being moved, must be on a
peg.
Towers of Hanoi problem
void tower_of_hanoi(int n, char sour, char des, char mid)
{
if(n>0){
count++;
tower_of_hanoi(n-1, sour, mid, des);
printf("\n\t\tMove disk %d from %c --> %c", n, sour, des);
tower_of_hanoi(n-1, mid, des, sour);
}

void main() {
int n;
printf("\nEnter no. of disks: ");
scanf("%d",&n);
tower_of_hanoi(n,'A','C','B');
printf("\n\n Number of Moves are --> %d",count);
}
Queue
• Queue is a linear data structures, where
insertion and deletion takes place at two
ends.

• Queue operations
– int isFull(void);
– int isEmpty(void);
– void enqueue(int);
– void dequeue(void);
Queue ADT
Queue ADT
Isfull()
int front=-1,rear=-1,queue[20];

int isFull(void)
{
if(rear==MAX-1)
return 1;
else
return 0;
}
Isempty()
int isEmpty(void){
if(front<0 || front>rear)
return 1;
else
return 0;
}
enqueue()
void enqueue(int in){
if(!isFull()){
if(front==-1)
++front;
queue[++rear]=in;
printf("\n Inserted..!");
}
else{
printf("\n Queue is Full");
}
}
Dequeue()
void dequeue(void){
if(!isEmpty())
printf("\n %d is Deleted
",queue[front++]);
else
printf("\n Queue is Empty...!");
}
Display()
void display(void)
{
int i;
for(i=front;i<=rear;i++)
printf("\t %d",queue[i]);
}
Circular queue
• int front=-1,rear=-1,cque[20];

int isFull(void){
if(front==((rear+1)%MAX))
return 1;
else
return 0;
}
Isempty()
int isEmpty(void){
if(front<0)
return 1;
else
return 0;
}
Enqueue()
void enqueue(int in){
if(!isFull()){
if(front==-1)
{
front=0;rear=0;}
else
rear = (rear+1)%MAX;
cque[rear]=in;
printf("\n Inserted..!");
}
else{
printf("\n Queue is Full");
}
}
Dequeue()
void dequeue(void){
if(!isEmpty())
{
printf("\n %d is Deleted ",cque[front]);
if(front==rear){
front=-1; rear=-1;}
else
front=(front+1)%MAX;
}
else
printf("\n Queue is Empty...!");

}
Display()
void display(void)
{
int i;
if(isEmpty())
printf("\n Queue is Empty...!");
else
{
printf("front-->%d\n ",front);
for(i=front; i!=rear; i=(i+1)%MAX)
printf("%d\t",cque[i]);

printf("%d",cque[i]);
printf("\n<--%d",rear);
}
}

You might also like