-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueUsingCLL.c
86 lines (67 loc) · 1.64 KB
/
QueueUsingCLL.c
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
86
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
typedef struct {
struct node* front, *rear;
}queue;
struct node* create(int val){
struct node* new= (struct node* )malloc(sizeof(struct node));
new->data = val;
new->next = NULL;
return new;
}
queue * enq(queue *q,int val){
struct node* new = create(val);
if(q->rear == NULL){
q->rear = q->front = new;
}
else{
q->rear->next = new;
q->rear = new;
new->next = q->front;
}
return q;
}
struct node* deq(queue *q){
if(q->front==NULL) return NULL;
struct node* temp = q->front;
q->front = q->front->next;
q->rear->next = q->front;
if(q->front == NULL) q->rear =NULL;
return temp;
}
void disp(queue* q){
struct node* temp = q->front;
while(temp!=q->rear){
printf("%d ",temp->data);
temp=temp->next;
}
printf("%d\n\n",temp->data);
}
int main(){
queue *q =(queue *)malloc(sizeof(queue));
q->front =NULL;
q->rear =NULL;
while(1){
int c=1,x;
struct node* n = NULL;
printf("Choice:: ");
scanf("%d",&c);
switch(c){
case 1: scanf("%d",&x);
enq(q,x);
disp(q);
break;
case 2: n=deq(q);
printf("%d got deleted!\n",n->data);
break;
case 3: disp(q);
break;
default: printf("Invalid Choice\n\n");
break;
}
}
}