Stack & Queue
Stack & Queue
Stack is a linear data structure in which the insertion and deletion operations are performed at
only one end. In a stack, adding and removing of elements are performed at a single position which is
known as "top". That means, a new element is added at top of the stack and an element is removed
from the top of the stack. In stack, the insertion and deletion operations are performed based
on LIFO (Last In First Out) principle.
In a stack, the insertion operation is performed using a function called "push" and deletion operation
is performed using a function called "pop".
In the figure, PUSH and POP operations are performed at a top position in the stack. That means, both
the insertion and deletion operations are performed at one end (i.e., at Top)
Stack is a linear data structure in which the operations are performed based on LIFO principle.
"A Collection of similar data items in which both insertion and deletion operations are
performed based on LIFO principle".
Example
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom-most
element and 50 is the topmost element. The last inserted element 50 is at Top of the stack as shown in
the image below...
Operations on a Stack
Stack data structure can be implemented in two ways. They are as follows...
1. Using Array
When a stack is implemented using an array, that stack can organize an only limited number of
elements. When a stack is implemented using a linked list, that stack can organize an unlimited
number of elements.
A stack data structure can be implemented using a one-dimensional array. But stack implemented
using array stores only a fixed number of data values. This implementation is very simple. Just define
a one dimensional array of specific size and insert or delete the values into that array by using LIFO
principle with the help of a variable called 'top'. Initially, the top is set to -1. Whenever we want to
insert a value into the stack, increment the top value by one and then insert. Whenever we want to
delete a value from the stack, then delete the top value and decrement the top value by one.
Before implementing actual operations, first follow the below steps to create an empty stack.
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
Step 4 - Define a integer variable 'top' and initialize with '0'. (int top = 0)
Step 5 - In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
In a stack, push() is a function used to insert an element into the stack. In a stack, the new element is
always inserted at top position. Push function takes one integer value as parameter and inserts that
value into the stack. We can use the following steps to push an element into the stack...
Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate
the function.
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from top position. Pop function does not take any value as parameter. We can use the
following steps to pop an element from the stack...
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with ‘0’. Display stack[i] value
and increment i value by one (i++).
#include<stdio.h>
#include<conio.h>
#include<process.h>
int max=5;
int stack[10];
int top=0;
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
printf("\n performing stack operations ");
for(;;)
{
{
printf("\n1. push 2.pop 3.display 4.exit");
printf("\nenter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop(); break;
case 3:display(); break;
case 4:exit(0);break;
default: printf("\nenter proper chioce");
}
}
getch();
}
void push()
{
int ele;
if(top==max)
printf("\nstack is overflow ");
else
{
printf("\nenter the element into stack ");
scanf("%d",&ele);
stack[++top]=ele;
}
}
void pop()
{
if(top==0)
printf("\nstack is undreflow");
else
printf("\npoped element is : %d",stack[top--]);
}
void display()
{
int i;
if(top==0)
printf("\n stack is underflow ");
else
{
printf("\n elements in stack is : \n");
for(i=1;i<=top;i++)
{
printf("%d\t",stack[i]);
}
}
}
Queue
Queue is a linear data structure in which the insertion and deletion operations are performed at
two different ends. In a queue data structure, adding and removing elements are performed at two
different positions. The insertion is performed at one end and deletion is performed at another end. In
a queue data structure, the insertion operation is performed at a position which is known as 'rear' and
the deletion operation is performed at a position which is known as 'front'. In queue data structure, the
insertion and deletion operations are performed based on FIFO (First In First Out) principle.
In a queue data structure, the insertion operation is performed using a function called "enQueue()"
and deletion operation is performed using a function called "deQueue()".
Operations on a Queue
The following operations are performed on a queue data structure...
1. enQueue(value) - (To insert an element into the queue)
2. deQueue() - (To delete an element from the queue)
3. display() - (To display the elements of the queue)
Queue data structure can be implemented in two ways. They are as follows...
1. Using Array
2. Using Linked List
When a queue is implemented using an array, that queue can organize an only limited number of
elements. When a queue is implemented using a linked list, that queue can organize an unlimited
number of elements.
A queue data structure can be implemented using one dimensional array. The queue implemented
using array stores only fixed number of data values. The implementation of queue data structure using
array is very simple. Just define a one dimensional array of specific size and insert or delete the values
into that array by using FIFO (First In First Out) principle with the help of variables 'front' and
'rear'. Initially both 'front' and 'rear' are set to -1. Whenever, we want to insert a new value into the
queue, increment 'rear' value by one and then insert at that position. Whenever we want to delete a
value from the queue, then delete the element which is at 'front' position and increment 'front' value by
one.
Queue Operations using Array
Queue data structure using array can be implemented as follows...
Before we implement actual operations, first follow the below steps to create an empty queue.
Step 1 - Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2 - Declare all the user defined functions which are used in queue implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front =
-1, rear = -1)
Step 5 - Then implement main method by displaying menu of operations list and make suitable
function calls to perform operation selected by the user on queue.
void enQueue(int);
void deQueue();
void display();
int queue[SIZE], front = -1, rear = -1;
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");9
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}
Output:
Insertion success!!!
Insertion success!!!
Insertion success!!!
Insertion success!!!
Insertion success!!!