Professional Documents
Culture Documents
Data Structure: A data structure is a storage that is used to store and organize
data. It is a way of arranging data on a computer so that it can be accessed and
updated efficiently. A data structure is not only used for organizing the data. It is also
used for processing, retrieving, and storing data.
o push(): When we insert an element in a stack then the operation is known as a push. If the stack
is full then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is
empty means that no element exists in the stack, this state is known as an underflow state.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
PUSH operation
POP operation
o Before deleting the element from the stack, we check whether the stack is empty.
o If we try to delete the element from the empty stack, then the underflow condition occurs.
o If the stack is not empty, we first access the element which is pointed by the top
o Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
C Program on Stack Operations:
#include<stdio.h>
#include<stdlib.h>
#define Size 4
Void Push();
Void Pop();
Void show();
int main(){
int choice;
while(1)
Switch(choice)
Case 1: push();
Break;
Case 2: pop();
Break;
Case 3: show();
Break;
Case 4: exit(0);
Void push()
int x;
if(Top==size-1)
Printf(“\n Overflow!!”);
Else
Scanf(“%d”,&x);
Top=Top+1;
Int array[Top]=x;
}
}
Void Pop()
if(Top==-1)
Printf(“\n underflow!!”);
Else
Top=Top-1;
Void show()
If(Top==-1)
Printf(“\n Underflow!!”);
Else
For(int i=Top;i>=0;--i)
Printf(“%d\n”,int array[i]);
OUTPUT:
Operations performed by Stack
1.Push the element
2.Pop the element
3.Show
4.End
Queue:
A queue is a data structure in which whatever comes first will go out first, and it follows the FIFO (First-In-First-Out)
policy. Queue can also be defined as the list or collection in which the insertion is done from one end known as the rear
end or the tail of the queue, whereas the deletion is done from another end known as the front end or the head of the
queue.
Simple Queue or Linear Queue: he major drawback of using a linear Queue is that insertion is done only
from the rear end. If the first three elements are deleted from the Queue, we cannot insert more elements even
though the space is available in a Linear Queue. In this case, the linear Queue shows the overflow condition as the
rear is pointing to the last element of the Queue.
o Enqueue: The Enqueue operation is used to insert the element at the rear end of the queue. It returns void.
o Dequeue: It performs the deletion from the front-end of the queue. It also returns the element which has been
removed from the front-end. It returns an integer value.
o Peek: This is the third operation that returns the element, which is pointed by the front pointer in the queue but
does not delete it.
o Queue overflow (isfull): It shows the overflow condition when the queue is completely full.
o Queue underflow (isempty): It shows the underflow condition when the Queue is empty, i.e., no elements are in
the Queue.
#include <stdio.h>
void dequeue();
void show();
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
main()
int ch;
while (1)
printf("1.Enqueue Operation\n");
o printf("2.Dequeue Operation\n");
o printf("3.Display the Queue\n");
o printf("4.Exit\n");
o printf("Enter your choice of operations : ");
o scanf("%d", &ch);
o switch (ch)
o {
o case 1:
o enqueue();
o break;
o case 2:
o dequeue();
o break;
o case 3:
o show();
o break;
o case 4:
o exit(0);
o default:
o printf("Incorrect choice \n");
o }
o }
o }
o
o void enqueue()
o {
o int insert_item;
o if (Rear == SIZE - 1)
o printf("Overflow \n");
o else
o {
o if (Front == - 1)
o
o Front = 0;
o printf("Element to be inserted in the Queue\n : ");
o scanf("%d", &insert_item);
o Rear = Rear + 1;
o inp_arr[Rear] = insert_item;
o }
o }
o
o void dequeue()
o {
o if (Front == - 1 || Front > Rear)
o {
o printf("Underflow \n");
o return ;
o }
o else
o {
o printf("Element deleted from the Queue: %d\n", inp_arr[Front]);
o Front = Front + 1;
o }
o }
o
o void show()
o {
o
o if (Front == - 1)
o printf("Empty Queue \n");
o else
o {
o printf("Queue: \n");
o for (int i = Front; i <= Rear; i++)
o printf("%d ", inp_arr[i]);
o printf("\n");
o }
o }
o Output:
o 1.Enqueue Operation
o 2.Dequeue Operation
o 3.Display the Queue
o 4.Exit
o Enter your choice of operations : 1
o Element to be inserted in the Queue: 10
o
o 1.Enqueue Operation
o 2.Dequeue Operation
o 3.Display the Queue
o 4.Exit
o Enter your choice of operations : 1
o Element to be inserted in the Queue: 20
o
o 1.Enqueue Operation
o 2.Dequeue Operation
o 3.Display the Queue
o 4.Exit
o Enter your choice of operations : 3
o Queue:
o 10 20
o
o 1.Enqueue Operation
o 2.Dequeue Operation
o 3.Display the Queue
o 4.Exit
o Enter your choice of operations : 2
o Element deleted from the Queue: 10
o
o 1.Enqueue Operation
o 2.Dequeue Operation
o 3.Display the Queue
o 4.Exit
o Enter your choice of operations: 3
o Queue:
o 20
Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue except that the last element of
the queue is connected to the first element. It is also known as Ring Buffer, as all the ends are connected to another end.
The representation of circular queue is shown in the below image -
Output: