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