Professional Documents
Culture Documents
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)
Functions :
STEPS :
2.2) x=Pop( )
Push(x)
Ouput(item)
While( x != ‘(‘ ) do
Output(x)
x=Pop( )
End While
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*+
( ) + - * / ^
Isp 0 - 2 2 4 4 6
Icp 7 0 1 1 3 3 5
Evaluation of Postfix expression
Algorithm : Eval_ Postfix expression(E)
Functions :
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) 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:
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);
}
}