-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputResDEQueue.cpp
169 lines (118 loc) · 2.1 KB
/
InputResDEQueue.cpp
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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
//insertion only allowed at the end
struct node
{
struct node *next,*prev;
int data;
};
typedef struct node node;
node *start=NULL,*end=NULL; //start->end = left->right
void insert_end(int data);
void delete_beg();
void delete_end();
void display();
int main()
{
char c;
int data;
while(1)
{
printf("What do you want to do? \n a.Insert \n b.Delete(first element) \n c.Delete(last element) \n d.Display Queue \n e.Exit\n");
scanf(" %c",&c);
switch(c)
{
case 'a' : printf("Enter the element to be inserted\n");
scanf("%d",&data);
insert_end(data);
printf("Element inserted \n");
break;
case 'b' : delete_beg();
break;
case 'c' : delete_end();
break;
case 'd' : display();
break;
case 'e' : exit(0);
default : printf("Invalid Input. Please try again\n");
}
}
}
void insert_end(int data)
{
node *new_node=(node *)malloc(sizeof(node));
new_node->data=data;
if(start==NULL)
{
start=end=new_node;
start->next=start->prev=NULL;
}
else
{
new_node->prev=end;
end->next=new_node;
end=new_node;
end->next=NULL;
}
}
void delete_beg()
{
node *temp;
if(start==NULL)
{
printf("The queue is empty. Please insert at least one element before performing deletion \n");
return;
}
temp=start;
printf("The element deleted is : %d \n",start->data);
if(start==end)
{
start=end=NULL;
free(temp);
}
else
{
start=start->next;
start->prev=NULL;
free(temp);
}
}
void delete_end()
{
node *temp;
if(start==NULL)
{
printf("The queue is empty. Please insert at least one element before performing deletion \n");
return;
}
temp=end;
printf("The element deleted is : %d \n",end->data);
if(start==end)
{
start=end=NULL;
free(temp);
}
else
{
end=end->prev;
end->next=NULL;
free(temp);
}
}
void display()
{
node *temp;
if(start==NULL)
{
printf("The queue is empty. Nothing to display \n");
return;
}
temp=start;
while(temp!=end)
{
printf("%d -> ",temp->data);
temp=temp->next;
}
printf("%d \n",temp->data);
}