-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_using_linkedList.c
More file actions
139 lines (116 loc) · 2.7 KB
/
Queue_using_linkedList.c
File metadata and controls
139 lines (116 loc) · 2.7 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdlib.h>
typedef struct Node* Queue;
struct Node{
int data;
struct Node* next;
};
Queue front,rear;
Queue FindPrev(Queue L)
{
L=L->next;
while(L->next->next!=NULL){
L=L->next;
}
return L;
}
Queue L,T;
void Enqueue(Queue L,int newdata){
Queue New=(struct Node*)malloc(sizeof(struct Node));
if(front==NULL && rear==NULL){
printf("Inside if");
New->data=newdata;
New->next=NULL;
T->next=New;
front=New;
T=T->next;
}
else{
New->data=newdata;
New->next=NULL;
T->next=New;
T=T->next;
rear=New;
}
}
void Dequeue(){
if(L->next->next==NULL){
printf("Queue Underflow");
}
L->next=front->next;
front->next=NULL;
front=L->next;
}
void display(Queue L){
//printf("%d",L->next->data);
L=L->next;
while(L->next!=NULL){
printf("%d\n",L->data);
L=L->next;
}
printf("%d",L->data);
}
int main(){
front=malloc(sizeof(struct Node));
front=NULL;
rear=malloc(sizeof(struct Node));
rear=NULL;
int element,s;
Queue P,node2;
L=(Queue)malloc(sizeof(struct Node));
T=(Queue)malloc(sizeof(struct Node));
int n=0;
//List newnode;
/*newnode=(List)malloc(sizeof(struct Node));
printf("Enter the data");
scanf("%d",&newnode->data);
L->next=newnode;
L->next->data=newnode->data;*/
//newnode->next=NULL;
P=L;
T=L;
/*do{
node2=(Queue)malloc(sizeof(struct Node));
printf("Enter the data");
scanf("%d",&node2->data);
P->next=node2;
P=P->next;
P->data=node2->data;
P->next=NULL;
printf("Do You want to continue, Y/N");
scanf("%d",&n);
}while(n==1);
//printf("%d",L->next->next->data);*/
do{
printf("Enter the data\n");
scanf("%d",&element);
Enqueue(L,element);
printf("Enter 1 to continue");
scanf("%d",&n);
}while(n==1);
display(L);
printf("\nEnter 1 to insert, and 2 to Dequeue");
scanf("%d",&s);
switch(s){
case 1:
do{
printf("Enter the element");
scanf("%d",&element);
Enqueue(L,element);
printf("Enter 1 to continue inserting");
scanf("%d",&n);
display(L);
}while(n==1);
break;
case 2:
T=L;
do{
printf("After Dequeuing:\n");
Dequeue();
display(L);
printf("\nEnter 1 to continue Dequeuing\n");
scanf("%d",&n);
}while(n==1);
}
return 0;
}