forked from suman-shah/c-program-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DQueueLinkedList.c
206 lines (188 loc) · 3.16 KB
/
DQueueLinkedList.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int item;
struct node *next;
};
void insertTail(struct node **h,struct node **t,int data)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->item=data;
temp->next=NULL;
temp->prev=NULL;
if((*h)==NULL&&(*t)==NULL)
{
*h=temp;
*t=temp;
}
else
{
temp->prev=*t;
(*t)->next=temp;
(*t)=temp;
}
}
void insertHead(struct node **h,struct node **t,int data)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->prev=NULL;
temp->next=NULL;
temp->item=data;
if((*h)==NULL&&(*t)==NULL)
{
*h=temp;
*t=temp;
}
else
{
temp->next=*h;
(*h)->prev=temp;
*h=temp;
}
}
void deletionHead(struct node **h,struct node **t)
{
struct node *r;
r=*h;
if((*h)==NULL&&(*t)==NULL)
{
printf("Queue is Empty\n");
}
else if(*h==*t)
{
free(*h);
free(*t);
*h=NULL;
*t=NULL;
}
else
{
(*h)=(*h)->next;
(*h)->prev=NULL;
free(r);
}
}
void deletionTail(struct node **h,struct node **t)
{
struct node *r;
r=*t;
if((*h)==NULL&&(*t)==NULL)
printf("Queue is Empty\n");
else if(*h==*t)
{
free(*h);
free(*t);
*h=NULL;
*t=NULL;
}
else
{
(*t)=(*t)->prev;
(*t)->next=NULL;
free(r);
}
}
struct node* search(struct node *h,int data)
{
while(h){
if(h->item==data)
return h;
h=h->next;
}
return NULL;
}
void viewFirst(struct node *h,struct node *t)
{
while(h)
{
printf("%d \t",h->item);
h=h->next;
}
}
void viewLast(struct node *h,struct node *t)
{
while(t)
{
printf("%d \t",t->item);
t=t->prev;
}
}
int menu()
{
int ch;
printf("\n\n 1. insert an item at Head ");
printf("\n 2. insert an item at Tail ");
printf("\n 3. delete node at Tail ");
printf("\n 4. delete node at Head ");
printf("\n 5. view at Head to Tail ");
printf("\n 6. view at Tail to Head ");
printf("\n 7. search node");
printf("\n 8. exit");
printf("\n\n Enter your choice: ");
scanf("%d",&ch);
return ch;
}
void main()
{
struct node *head=NULL;
struct node *tail=NULL;
struct node *temp;
while(1)
{
switch(menu())
{ int data;
case 1: {
int i,n;
printf("How many element you want to insert = ");
scanf("%d",&n);
printf("\n enter data to be inserted at head \n ");
for(i=1;i<=n;i++){
scanf("%d",&data);
insertHead(&head,&tail,data);
}
break;
}
case 2: {
int i,n;
printf("How many element you want to insert = ");
scanf("%d",&n);
printf(" enter data to be inserted at Tail \n ");
for(i=1;i<=n;i++){
scanf("%d",&data);
insertTail(&head,&tail,data);
}
break;}
case 3:
deletionTail(&head,&tail);
break;
case 4:
deletionHead(&head,&tail);
break;
case 5:
viewFirst(head,tail);
break;
case 6:
viewLast(head,tail);
break;
case 7:
{
struct node *temp;
printf(" enter data you want to search \n ");
scanf("%d",&data);
temp=search(head,data);
if(temp==NULL)
{
printf(" no such node found %c %c \n",2,2);
}
else
printf(" data %d is found ",temp->item);
break;
}
case 8:
exit(0);
}
}
}