Lab_7
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
struct node
{
int data;
struct node *link;
};
struct node *head, *p, *front, *rear, *last;
void main()
{
void insert(int);
void display();
int del();
int item, info, ch;
front = rear = NULL;
clrscr();
while (1)
{
printf("\n Circular Queue Menu");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display");
printf("\n 4. exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 :printf("\n Enter the element to be added to the list:");
scanf("%d",&info);
insert(info);
break;
case 2 :item = del();
printf("\n Deleted item is %d", item);
break;
case 3 :printf("\n Circular queue elements:");
display();
break;
case 4 : exit(0);
default :printf("\n error in choice");
break;
}
}
}
void insert (int num)
{
p = malloc (sizeof(struct node));
p->data = num;
p->link = NULL;
if (front == NULL)
front = p;
else rear ->link = p;
rear = p;
rear ->link = front;
}
void display()
{
p = front;
if (p==NULL)
{
printf("\n list is empty");
return;
}
while(p!=rear)
{
printf("\n %d",p->data);
p = p->link;
}
printf("\n%d",p->data);
}
int del()
{
int item;
if (front
== NULL)
{
printf("\n circular queue is empty");
//getch();
//exit();
}
else
{
if (front == rear)
{
item = front >data;
free(front);
front = NULL; rear = NULL;
} else {
p = front;
item = p->data;
front = front ->link;
rear ->link = front; free(p);
} return(item);
}
}