Write a menu driven program to implement queue operations
such as Enqueue, Dequeue, Peek, Display of elements, isEmpty
using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node *top = NULL, *bottom = NULL;
void enqueue(int num)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->info = num;
newNode->next = NULL;
if(top == NULL && bottom == NULL)
top = bottom = newNode;
else
{
bottom->next = newNode;
bottom = newNode;
}
}
void dequeue()
{
struct node *temp;
if(top == NULL)
printf("Queue is Empty!\n");
else
{
temp = top;
top = top->next;
if(top == NULL)
bottom = NULL;
free(temp);
}
}
void display()
{
struct node *temp = top;
while(temp)
{
printf("%d->",temp->info);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
enqueue(15);
enqueue(25);
enqueue(35);
printf("Queue :");
display();
dequeue();
printf("New Queue after dequeue:");
display();
dequeue();
printf("New Queue after dequeue::");
display();
return 0;
}
Comments
Leave a comment