You are on page 1of 6

2. Aim: Write a program to implement stack & queue.

Theory:
1. Implementation of Stack
Stack is a data structure which works based on principle of last in first out (LIFO). In computing
world, stack data structure can be applied in many applications such as parsing syntax of
expressions, runtime memory management (used in Java virtual machine) and solving search
problem.

2.1
1
By seeing the figure above you can see that this data structure is really a restricted list. You have
restricted the access to one end of the list by using the pop and push operations. The result of this
restriction is that items in the list will be stored one on top of the other.
We must first remove all the items above it till you get to the bottom item. "LastIn, FirstOut" or
LIFO, which is used to describe the behavior, since the last item to enter the stack is the first
item to leave the stack. The top item is the item always the last item to enter the stack and it is
always the first item to leave the stack since no other items can be removed until the top item is
removed.

Stack Operations:
push(newitem :itemtype)
Adds an item onto the stack.
top():itemtype
Returns the last item pushed onto the stack.
pop()
Removes the most recently pushed item from the stack.
isempty(): Boolean
True iff no more items can be popped and there is no top item.
isfull(): Boolean
True iff no more items can be pushed.
getsize(): Integer
Returns the number of elements on the stack.
Algorithm:

PUSH
Step1:
Increment the 'top' by 1
Step2:
Check for the 'stack overflow' condition. If stack not overflowing then go to step3
else say "stack overflow" and quit
Step3:
Put the new element at the position pointed by 'top'.

Program Snippets:
void push()
{
if(top == SIZE 1)
{
printf("Stack is full (overflow)”) ;
getch() ;
return ;
}
top++ ;
printf("Enter the element to PUSH : ") ;
scanf("%d", &stack[top]) ;
}

POP
Step1:
Check for the 'stack underflow' condition. If stack not empty then go to step2
else say "stack underflow" and quit
Step2:
Display and remove element at the position pointed by 'top'
Step3:
Decrement the top by 1.

Program Snippets:
void pop()
{
if(top == 1)
{
printf("Stack is empty (underflow)”) ;
getch() ;
return ;
}
printf("The POPPED element is : %d, stack[top]) ;
getch() ;
top;
}
void display()
{
if(top == 1)
{
10
printf("Stack is empty (underflow)”) ;
getch() ;
return ;
}
printf("The elements in stack from TOP are:") ;
for(i = top ; i >= 0 ; i)
printf(" %d", stack[i]) ;
getch() ;
}

Output:
e.g
For Push operation: Enter the element to push: 10
For Push operation: Enter the element to push: 20
For Push operation: Enter the element to push: 30
For Pop operation: The POPPED element is: 30
For Display operation: The elements in stack from TOP are: 20 10
Conclusion:
All operations except getsize() can be performed in O(1) time. getsize() runs in at worst
O(N).
2. Implementation of Queue
Queue is a data structure which works based on principle of first in first out (FIFO). In
computing world, queue data structure can be applied in many applications FCFS scheduling
algorithm, buffer of printer.

2.2

By seeing the figure above you can see that this data structure is really a restricted list.
You have restricted the access to two ends of the list by using the insert and delete operations.
The result of this restriction is that items in the list will be stored from the REAR end and can be
removed from the other end called FRONT.

Queue Operations:
insert(newitem :itemtype)
Adds an item onto the queue from rear end.
delete()
Removes the last entered item from the queue.
isempty(): Boolean
True iff no more items can be deleted.
isfull(): Boolean
True iff no more items can be inserted.
getsize():Integer
Returns the number of elements in the queue.

Algorithm:

INSERT
Step1:
Increment the 'rear' by 1
Step2:
Check for the 'queue overflow' condition. If queue not overflowing then go to
step3
else say "queue overflow" and quit
Step3:
Put the new element at the position pointed by 'rear'.

Program Snippets:
void insert()
{
if(rear == SIZE 1)
{
printf("Queue is full (overflow)”) ;
getch() ;
return ;
}
rear++ ;
printf("Enter the element to insert : ") ;
scanf("%d", &queue[rear]) ;
}
DELETE
Step1:
Check for the 'queue underflow' condition. If queue not empty then go to step2
else say "queue underflow" and quit
Step2:
Display and remove element at the position pointed by 'front'
Step3:
Increment the front by 1.

Program Snippets:
void delete()
{
if(front == 1)
{
printf("Queue is empty (underflow)”) ;
getch() ;
return ;
}
printf("The deleted element is : %d, queue[front]) ;
getch() ;
front;
}
void display()
{
if(front == 1)
{
printf("Queue is empty (underflow)”) ;
getch() ;
return ;
}
printf("The elements in Queue are:") ;
for(i = rear ; i >= front; i)
printf(" %d", queue[i]) ;
getch() ;
}
Output:

e.g.
For Insert operation: Enter the element to insert: 10
For Insert operation: Enter the element to insert: 20
For Insert operation: Enter the element to insert: 30
For Delete operation: The deleted element is: 10
For Display operation: The elements in queue are: 20 30

Conclusion:
All operations except getsize() can be performed in O(1) time. getsize() runs in at worst O(N).

You might also like