From 4f2d29e87826bcb1129b9cf93a2d77d05ce5caa3 Mon Sep 17 00:00:00 2001 From: KedarGS <91322700+kedarrr@users.noreply.github.com> Date: Thu, 6 Oct 2022 08:21:41 +0530 Subject: [PATCH] Queue Using LinkList --- C/Queue Using LinkList.c | 106 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 C/Queue Using LinkList.c diff --git a/C/Queue Using LinkList.c b/C/Queue Using LinkList.c new file mode 100644 index 0000000..f3e36c4 --- /dev/null +++ b/C/Queue Using LinkList.c @@ -0,0 +1,106 @@ +#include +#include +struct queue{ + int data; + struct queue *next; +}; +struct queue *front = NULL; +struct queue *rear = NULL; +typedef struct queue Queue; + +void Enqueue(Queue **front,Queue **rear,int Item) +{ + Queue *new = malloc(sizeof(Queue)); + new->next=NULL; + new->data=Item; + + if(*front == NULL && *rear == NULL) + { + *front = *rear = new; + } + else + { + (*rear)->next=new; + *rear=new; + } +} + +int Dequeue(Queue **front,Queue **rear) +{ + Queue *temp; + if(*front == NULL) + { + printf("Queue is Empty can not Dequeue.\n"); + } + else + { + temp = *front; + int Data; + if(*front == *rear) + { + *front = *rear = NULL; + free(temp); + } + else + { + int Item; + Item=(*front)->data; + (*front)=(*front)->next; + free(temp); + return Item; + } + } +} + +void Display(Queue *front,Queue *rear) +{ + if(front==NULL && rear==NULL) + { + printf("Queue is Empty.\n"); + } + else + { + while(front!=NULL) + { + printf("%d ",front->data); + front=front->next; + } + } +} + +void main() +{ + int ch; + int Data; + while(1) + { + printf("\nEnter 1:-Enqueue 2:-Dequeue 3:-Display 4:-Exit.\n"); + printf("Enter your choice: "); + scanf("%d",&ch); + switch(ch) + { + case 1: + printf("Enter data to be inserted: "); + scanf("%d",&Data); + Enqueue(&front,&rear,Data); + Display(front,rear); + break; + + case 2: + Dequeue(&front,&rear); + Display(front,rear); + break; + + case 3: + Display(front,rear); + break; + + case 4: + exit(0); + break; + + default: + printf("Invalid Input.\n"); + } + } +}