From 60cdbc1f5e77337910bc77fd0f591322c4764eb1 Mon Sep 17 00:00:00 2001 From: nikkhilbisht24 <97297220+nikkhilbisht24@users.noreply.github.com> Date: Fri, 14 Oct 2022 13:32:28 +0530 Subject: [PATCH] Create queue_using_linked_list.c --- algorithms/C/queues/queue_using_linked_list.c | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 algorithms/C/queues/queue_using_linked_list.c diff --git a/algorithms/C/queues/queue_using_linked_list.c b/algorithms/C/queues/queue_using_linked_list.c new file mode 100644 index 00000000..4c746b1d --- /dev/null +++ b/algorithms/C/queues/queue_using_linked_list.c @@ -0,0 +1,93 @@ +#include +#include +struct node *enqueue(struct node*); +struct node *dequeue(struct node*); +void display(struct node*); +void peek(struct node*); +struct node{ + int data; + struct node *next; +}; + +int main(){ + struct node *front=0,*rear=0; + int choice; + printf("Press 1 for Enqueue\nPress 2 for Dequeue\nPress 3 for Display\nPress 4 for Peek\nPress 5 for Exit\n"); + do{ + printf("\nEnter the choice: "); + scanf("%d",&choice); + switch(choice){ + case 1: rear=enqueue(rear); + if(front==0){ + front=rear; + } + break; + + case 2: if(front==0 && rear==0){ + printf("Queue is Empty"); + break; + } + else{ + front=dequeue(front); + if(front==0){ + rear=0; + } + break; + } + + case 3: if(front==0){ + printf("Queue is Empty"); + break; + } + else{ + display(front); + break; + } + + case 4: if(front==0){ + printf("Queue is Empty"); + break; + } + else{ + peek(front); + break; + } + + case 5: break; + default: printf("Invalid Choice"); + } + }while(choice!=5); +} +struct node *enqueue(struct node *rear){ + struct node *newnode; + newnode=(struct node*)malloc(sizeof(struct node)); + printf("Enter the data: "); + scanf("%d",&newnode->data); + newnode->next=0; + if(rear==0){ + rear=newnode; + } + else{ + rear->next=newnode; + rear=newnode; + return rear; + } +} +struct node *dequeue(struct node *front){ + struct node*temp=front; + printf("The enqueued element is %d",front->data); + front=front->next; + free(temp); + return front; +} +void display(struct node *front){ + struct node *temp=front; + printf("The elements in the queue are: "); + while(temp!=0){ + printf("%d ",temp->data); + temp=temp->next; + } +} +void peek(struct node *front){ + printf("The Peek element is %d",front->data); +}