QUEUS USING SINGLE LINKED LIST
#include
<stdio.h>
#include
<stdlib.h>
#include<malloc.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
void
enq(int data);
void
deq();
void
empty();
void
display();
int
count = 0;
void
main()
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Empty");
printf("\n 4 - Exit");
printf("\n 5 - Display");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
empty();
break;
case 4:
exit(0);
case 5:
display();
break;
default:
printf("Wrong choice, Please
enter correct choice ");
break;
}
}
}
void
enq(int data)
{
if (rear == NULL)
{
rear = (struct node
*)malloc(sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node
*)malloc(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");
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 Error: Trying to delete
elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value :
%d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d",
front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
void
empty()
{
if ((front == NULL) && (rear ==
NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
Comments
Post a Comment