queue implementation using array:
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. It represents a collection of elements where the addition of new elements takes place at one end, called the rear or enqueue, and the removal of existing elements occurs at the other end, called the front or dequeue. This ordering ensures that the elements are processed in the same order they were added.
The operations commonly associated with a queue are:
Enqueue: Adds an element to the rear of the queue.
Dequeue: Removes and returns the front element of the queue.
Front: Returns the front element of the queue without removing it.
IsEmpty: Checks if the queue is empty.
Size: Returns the number of elements currently in the queue.
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front =-1,rear=-1;
int queue[maxsize];
void main()
{
int choice;
while(choice!=4)
{
printf("\n ###########Main menu####### \n");
printf("\n ===================================\n");
printf("\n 1.insert \n 2. delete \n 3. display \n 4.exit");
printf("\n enter your chice");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("enter a valid choice");
}
}
}
void insert()
{
int item;
printf("\n enter an element");
scanf("%d",&item);
if(rear==maxsize-1)
{
printf("\n overflow\n");
return 0;
}
if(front==-1 && rear==-1)
{
front=0;
rear=0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}
output:
###########Main menu#######
===================================
1.insert
2. delete
3. display
4.exit
enter your chice1
enter an element6
Value inserted
###########Main menu#######
===================================
1.insert
2. delete
3. display
4.exit
enter your chice1
enter an element1
Value inserted
###########Main menu#######
===================================
1.insert
2. delete
3. display
4.exit
enter your chice1
enter an element8
Value inserted
###########Main menu#######
===================================
1.insert
2. delete
3. display
4.exit
enter your chice3
printing values .....
6
1
8
###########Main menu#######
===================================
1.insert
2. delete
3. display
4.exit
enter your chice2
value deleted
###########Main menu#######
===================================
1.insert
2. delete
3. display
4.exit
enter your chice
queue implementation using link list:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
Output:
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter value?
12
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter value?
60
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
12
60
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?2
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
60
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?4
0 Comments