forked from Sbiswas001/Basic-c-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingLinkedList.c
More file actions
85 lines (82 loc) · 1.69 KB
/
QueueUsingLinkedList.c
File metadata and controls
85 lines (82 loc) · 1.69 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
struct node* head=NULL;
void enqueue(){
int n;
printf("Enter element to enqueue:");
scanf("%d",&n);
if(head==NULL){
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL){
printf("Overflow\n");
return;
}
head->data=n;
head->next=NULL;
return;
}
struct node* q=(struct node*)malloc(sizeof(struct node));
if(q==NULL){
printf("Overflow\n");
return;
}
q->data=n;
q->next=NULL;
struct node* p=head;
while(p->next!=NULL){
p=p->next;
}
p->next=q;
}
void dequeue(){
if(head==NULL){
printf("Underflow\n");
return;
}
struct node* p=head;
head=head->next;
printf("dequeued element :%d\n",p->data);
free(p);
}
void display(){
if(head==NULL){
printf("Queue is empty\n");
return;
}
struct node* p=head;
printf("Queue:");
while(p->next!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("%d ",p->data);
printf("\n");
}
int main()
{
int choice;
while(1){
printf("Instructions:\n");
printf("1 to enqueue\n2 to dequeue\n3 to exit\n");
printf("Enter choice:");
scanf("%d",&choice);
if(choice==3){
printf("Your session has ended.");
return 0;
}
switch(choice){
case 1:enqueue();
display();
break;
case 2:dequeue();
display();
break;
default: printf("Invalid input, try again!\n");
}
}
return 0;
}