-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_queue_using_linkedlist.c
More file actions
34 lines (34 loc) · 1.22 KB
/
circular_queue_using_linkedlist.c
File metadata and controls
34 lines (34 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<stdlib.h>
struct node { int data; struct node *next;};
struct node *front=NULL, *rear=NULL;
void enqueue(); void dequeue(); void peek(); void display();
int main(){
int choice=1; while(choice){ printf("Enter your choice:\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n"); scanf("%d", &choice);
switch(choice){
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: exit(0); break;
default: printf("Invalid Choice"); break;
}
}
}
void enqueue(){ struct node *newnode=(struct node *)malloc(sizeof(struct node)); printf("Enter data: ");
scanf("%d", &newnode->data);
if(front==NULL && rear==NULL) {front=rear=newnode; rear->next=front; }
else { newnode->next=front; rear->next=newnode; rear=newnode; }
}
void dequeue(){
if(front==NULL && rear==NULL) printf("Queue is empty\n");
else if(front==rear) front=rear=NULL;
else {struct node *temp=front; front=front->next; free(temp);}
}
void peek(){
if(front==NULL && rear==NULL) printf("Queue is empty\n");
else{ printf("%d\n", front->data); }
}
void display(){
struct node *temp=front; do { printf("%d ", temp->data); temp=temp->next;}while(temp!=rear->next);
}