Design and implement a given
type(ordinary queue ,circular
queue) queue in C (array
implementation/Linked list
implementation ).And demonstrate
its working with suitable
inputs .Display appropriate
messages in case of Exceptions
a) Queue using arrays
#include<stdio.h>
#include<stdlib.h>
#define qs 5
int front=-1,rear=-1,queue[qs];
void enqueue(int ele)
{
if(isFull())
printf("Overflow\n");
else if(rear==-1 && front==-1)
{
front=0;
rear=0;
queue[rear]=ele;
printf("Element enqueued is %d\n",queue[rear]);
}
else
{
rear=rear+1;
queue[rear]=ele;
printf("Element enqueued is %d\n",queue[rear]);
}
}
void dequeue()
{
if(isEmpty())
printf("Underflow condition\n");
else if(front == rear)
{
printf("Element deleted is %d\n",queue[front]);
front=-1;
rear=-1;
}
else
{
int ele=queue[front];
front++;
printf("Element deleted is %d\n",ele);
}
}
int isEmpty()
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}
int isFull()
{
if(rear==qs-1)
return 1;
else
return 0;
}
void peek()
{
if(isEmpty())
printf("Peek operation cannot be performed\n");
else
{
printf("Peek element is %d\n",queue[front]);
}
}
void display()
{
int i;
if(isEmpty())
printf("No elements to display\n");
printf("Elements are : ");
for(i=front;i<=rear;i++)
printf("%d ",queue[i]);
printf("\n");
}
void main()
{
int choice,n;
printf("[Link]\n [Link]\n [Link]\n [Link]\n [Link]\n");
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted : ");
scanf("%d",&n);
enqueue(n);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
}
}
}
OUTPUT:
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice : 1
Enter the element to be inserted : 10
Element enqueued is 10
Enter your choice : 1
Enter the element to be inserted : 20
Element enqueued is 20
Enter your choice : 1
Enter the element to be inserted : 30
Element enqueued is 30
Enter your choice : 3
Peek element is 10
Enter your choice : 4
Elements are : 10 20 30
Enter your choice : 2
Element deleted is 10
Enter your choice : 4
Elements are : 20 30
Enter your choice : 2
Element deleted is 20
Enter your choice : 2
Element deleted is 30
Enter your choice : 2
Underflow condition
B) Queue using Linked List
#include <stdio.h>
#include <stdlib.h>
struct node{
int data ;
struct node *next ;
};
struct node *front,*rear;
void enqueue()
{
int ele ;
struct node *p= (struct node*) malloc(sizeof(struct
node)); printf("Enter the value to be inserted: ");
scanf("%d",&ele);
p->data=ele;
p->next=NULL;
if(front==NULL)
{
front=p;
rear=p;
printf("%d is inserted\n",rear->data);
}
else
{
rear->next=p;
rear=p;
printf("%d is inserted\n",rear->data);
}
}
void dequeue()
{
struct node *p;
if(front==NULL)
{
printf("Queue is Empty\n");
return;
}
p=front;
printf("%d is deleted\n",p->data);
front =front->next;
free(p);
}
void display()
{
if(front==NULL)
{
printf("Queue is empty\n");
return;
}
struct node *temp=front;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
void peek()
{
if(front == NULL)
{
printf("Queue is empty\n");
return;
}
printf("Peek element is %d\n",front->data);
}
void main()
{
int choice,ele; printf("[Link]\[Link]\[Link]\
[Link]\[Link]\n") ; while(1){
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
exit(0);
break;
default: printf("Invalid choice\n");
}
}
}
OUTPUT:
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice: 1
Enter the value to be inserted: 10
10 is inserted
Enter your choice: 1
Enter the value to be inserted: 20
20 is inserted
Enter your choice: 1
Enter the value to be inserted: 30
30 is inserted
Enter your choice: 3
10 20 30
Enter your choice: 4
Peek element is 10
Enter your choice: 2
10 is deleted
Enter your choice: 3
20 30
Enter your choice: 3
20 30
Enter your choice: 2
20 is deleted
Enter your choice: 2
30 is deleted
Enter your choice: 2
Queue is Empty
C) Circular queue using arrays
#include<stdio.h>
#include<stdlib.h>
#define qs 3
int front=-1,rear=-1,queue[qs];
void enqueue(int ele)
{
if(isFull())
printf("Overflow\n");
else if(rear==-1 && front==-1)
{
front=0;
rear=0;
queue[rear]=ele;
printf("Element enqueued is %d\n",queue[rear]);
}
else
{
rear=(rear+1)%qs;
queue[rear]=ele;
printf("Element enqueued is %d\n",queue[rear]);
}
}
void dequeue()
{
if(isEmpty())
printf("Underflow condition\n");
else if(front == rear)
{
printf("Element deleted is %d\n",queue[front]);
front=-1;
rear=-1;
}
else
{
int ele=queue[front];
front=(front+1)%qs;
printf("Element deleted is %d\n",ele);
}
}
int isEmpty()
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}
int isFull()
{
if((rear+1)%qs==front)
return 1;
else
return 0;
}
void peek()
{
if(isEmpty())
printf("Peek operation cannot be performed\n");
else
{
printf("Peek element is %d\n",queue[front]);
}
}
void display()
{
int i;
if(isEmpty())
printf("No elements to display\n");
printf("Elements are : ");
for(i=front;i!=rear+1;(++i)%qs)
printf("%d ",queue[i]);
printf("\n");
}
void main()
{
int choice,n;
printf("[Link]\n [Link]\n [Link]\n [Link]\n [Link]\n");
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted : ");
scanf("%d",&n);
enqueue(n);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
}
}
}
OUTPUT:
[Link]
e
[Link]
ue
[Link]
[Link]
[Link]
Enter your choice : 1
Enter the element to be inserted :
10 Element enqueued is 10
Enter your choice : 1
Enter the element to be inserted :
20 Element enqueued is 20
Enter your choice : 1
Enter the element to be inserted :
30 Element enqueued is 30
Enter your choice : 3
Peek element is 10
Enter your choice : 4
Elements are : 10 20
30 Enter your choice :
2 Element deleted is
10 Enter your choice :
4 Elements are : 20
30 Enter your choice :
2 Element deleted is
20 Enter your choice :
2 Element deleted is
30 Enter your choice :
4 No elements to
display Elements are :
0