IMPLEMENTATION OF QUEUE USING LINKED LISTS
#include
#include
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf(" 1 : Enque");
printf("\n 2 : Deque");
printf("\n 3 : Diaplay the front element");
printf("\n 4 : Make the queue empty");
printf("\n 5 : Display the queue");
printf("\n 6 : Display the queue size");
printf("\n 7 : exit the queue");
create();
while (1)
{
printf("\n Enter the choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the elements to be pushed : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("%d is the front element\n", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
display();
break;
case 6:
queuesize();
break;
case 7:
exit(0);
default:
printf("Please enter the correct choice ");
break;
}
}
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("%d is the size of the queue\n", count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty\n");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n The queue is empty");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("%d is the dequeued element\n", front->info);
free(front);
front = front1;
}
else
{
printf("%d is the dequeued element\n", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
front = rear = NULL;
printf("The queue is cleared\n");
}
#include
#include
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf(" 1 : Enque");
printf("\n 2 : Deque");
printf("\n 3 : Diaplay the front element");
printf("\n 4 : Make the queue empty");
printf("\n 5 : Display the queue");
printf("\n 6 : Display the queue size");
printf("\n 7 : exit the queue");
create();
while (1)
{
printf("\n Enter the choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the elements to be pushed : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("%d is the front element\n", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
display();
break;
case 6:
queuesize();
break;
case 7:
exit(0);
default:
printf("Please enter the correct choice ");
break;
}
}
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("%d is the size of the queue\n", count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty\n");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n The queue is empty");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("%d is the dequeued element\n", front->info);
free(front);
front = front1;
}
else
{
printf("%d is the dequeued element\n", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
front = rear = NULL;
printf("The queue is cleared\n");
}
0 Comments