DAY 3
STACKS AND QUEUES
VRITIKA NAIK
Twitter: @NaikVritika
LinkedIn: Vritika Naik
TOPICS COVERED
Stack – Array and Linked List implementation
Queue – Array and Linked List Implementation
Circular Queue
Deque
Priority Queue
WHAT IS A STACK?
• Linear List in which insertions and deletions are
allowed only at one end, called top of the stack
• LIFO: Last In First Out
• Example: Stack of trays in Cafeteria
• Insertion: push()
• Deletion: pop()
HOW
DOES A
STACK
WORK?
ARRAY •int stack_arr[MAX];
IMPLEMENTATION •int top = -1;
OF STACK
PUSH
void push(int item)
{
if( isFull() )
{
printf("Stack Overflow\n");
return;
}
top = top+1;
stack_arr[top] = item;
}/*End of push()*/
POP
int pop()
{
int item;
if( isEmpty() )
{
printf("Stack Underflow\n");
exit(1);
}
item = stack_arr[top];
top = top-1;
return item;
}/*End of pop()*/
PEEK – TOP OF THE STACK
int peek()
{
if( isEmpty() )
{
printf("Stack Underflow\n");
exit(1);
}
return stack_arr[top];
}/*End of peek()*/
ISEMPTY
int isEmpty()
{
if( top == -1 )
return 1;
else
return 0;
}/*End of isEmpty*/
ISFULL
int isFull()
{
if( top == MAX-1 )
return 1;
else
return 0;
}/*End of isFull*/
DISPLAY
void display()
{
int i;
if( isEmpty( ) )
{ printf("Stack is empty\n");
return;
}
printf("Stack elements :\n\n");
for(i=top;i>=0;i--)
printf(" %d\n", stack_arr[i] );
printf("\n");
}/*End of display()*/
struct node
LINKED LIST {
int info;
IMPLEMENTATION struct node *link;
}*top=NULL;
PUSH
void push(int item)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf("Stack Overflow\n");
return;
}
tmp->info=item;
tmp->link=top;
top=tmp;
}/*End of push()*/
POP
int pop()
{ struct node *tmp;
int item;
if( isEmpty() )
{ printf("Stack Underflow\n");
exit(1);
}
tmp=top;
item=tmp->info;
top=top->link;
free(tmp);
return item;
}/*End of pop()*/
PEEK
int peek()
{
if( isEmpty() )
{
printf("Stack Underflow\n");
exit(1);
}
return top->info;
}/*End of peek() */
ISEMPTY
int isEmpty()
{
if(top == NULL)
return 1;
else
return 0;
}/*isEmpty()*/
DISPLAY
void display()
{ struct node *ptr;
ptr=top;
if(isEmpty())
{ printf("Stack is empty\n");
return;
}
printf("Stack elements :\n\n");
while(ptr!=NULL)
{ printf(" %d\n",ptr->info);
ptr=ptr->link;
}
printf("\n");
}/*End of display()*/
• A linear list in which elements can be inserted only at one end called
rear of the queue and deleted at the other end called front.
QUEUE • FIFO – First In First Out
• Example: Queue of cars
• Insertion – Enqueue
• Deletion - Dequeue
HOW
DOES IT
WORK?
int queue_arr[MAX];
ARRAY int rear=-1;
IMPLEMENTATION int front=-1;
ENQUEUE
void insert(int item)
{
if( isFull() )
{
printf("Queue Overflow\n");
return;
}
if( front == -1 )
front=0;
rear=rear+1;
queue_arr[rear]=item ;
}/*End of insert()*/
DEQUEUE
int del()
{
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
item=queue_arr[front];
front=front+1;
return item;
}/*End of del()*/
PEEK
int peek()
{
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
return queue_arr[front];
}/*End of peek()*/
ISEMPTY
int isEmpty()
{
if( front==-1 || front==rear+1 )
return 1;
else
return 0;
}/*End of isEmpty()*/
ISFULL
int isFull()
{
if( rear==MAX-1 )
return 1;
else
return 0;
}/*End of isFull()*/
DISPLAY
void display()
{
int i;
if ( isEmpty() )
{
printf("Queue is empty\n");
return;
}
printf("Queue is :\n\n");
for(i=front;i<=rear;i++)
printf("%d ",queue_arr[i]);
printf("\n\n");
}/*End of display() */
struct node
LINKED LIST {
IMPLEMENTATION int info;
struct node *link;
}*front=NULL,*rear=NULL;
ENQUEUE
void insert(int item)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
if(tmp==NULL)
{ printf("Memory not available\n");
return;
}
tmp->info = item;
tmp->link=NULL;
if(front==NULL) /*If Queue is empty*/
front=tmp;
else
rear->link=tmp;
rear=tmp;
}/*End of insert()*/
DEQUEUE
int del()
{ struct node *tmp;
int item;
if( isEmpty( ) )
{ printf("Queue Underflow\n");
exit(1);
}
tmp=front;
item=tmp->info;
front=front->link;
free(tmp);
return item;
}/*End of del()*/
PEEK
int peek()
{
if( isEmpty( ) )
{
printf("Queue Underflow\n");
exit(1);
}
return front->info;
}/*End of peek()*/
ISEMPTY
int isEmpty()
{
if(front==NULL)
return 1;
else
return 0;
}/*End of isEmpty()*/
DISPLAY
void display()
{ struct node *ptr;
ptr=front;
if(isEmpty())
{ printf("Queue is empty\n");
return;
}
printf("Queue elements :\n\n");
while(ptr!=NULL)
{ printf("%d ",ptr->info);
ptr=ptr->link;
}
printf("\n\n");
}/*End of display()*/
CIRCULAR QUEUE
• In a queue as array, insertion is not
possible after rear reaches the last
position of the array.
• Even if there is vacant position at the
front, it cant be utilized.
WORKING
OF A
CIRCULA
R QUEUE
DECK or DQ : Linear List in which elements can be inserted or
DEQUE deleted at either end of the list.
HOW
DOES A
DEQUE
WORK?
PRIORITY QUEUE
• Every element of the queue has priority over
the other.
• Higher priority will be processed before.
• Same priority => FIFO rule application
• Implemented using queue or sorted list
APPLICATIONS OF STACK
• Reversal of string
• Checking validity of an expression containing nested parentheses
• Function calls
• Conversion of infix to postfix
• Evaluation of Postfix
REVERSA
L OF
STRING
USING
STACK
CHECKING
VALIDITY
OF AN
EXPRESSI
ON
FUNCTIO
N CALLS
INFIX TO
POSTFIX
EVALUATI
ON OF
POSTFIX
234+*6-
APPLICATIONS OF QUEUE
• Job Scheduling in CPU
• Priority Queue
STACKS
VS
QUEUE
Thank You!
VRITIKA NAIK
Twitter: @NaikVritika
LinkedIn: Vritika Naik