-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu_DLL.cpp
177 lines (152 loc) · 3.02 KB
/
Menu_DLL.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
166
167
168
169
170
171
172
173
174
175
176
177
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<iostream>
using namespace std;
class node{
public:
node *prev;
int data;
node *next;
};
node *head=NULL;
void insertatbeg(int val)
{
node *p=new node;
p->data =val;
p->prev=NULL;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
p->next=head;
head->prev=p;
head=p;
}
}
void display()
{
node*temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
void insertatend(int val)
{
node *p=new node;
p->data =val;
p->prev=NULL;
p->next=NULL;
node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->prev=temp;
}
void insertatpos(int val, int pos)
{
node *p=new node;
p->data =val;
p->prev=NULL;
p->next=NULL;
node *temp=head;
int i=1;
while(i<pos-1)
{
temp=temp->next;
i++;
}
if(temp->next==NULL)
{
temp->next=p;
p->prev=temp;
}
else
{
p->next=temp->next;
p->prev=temp;
temp->next->prev=p;
temp->next=p;
}
}
void deleteatbeg()
{
node *temp = head;
head=head->next;
head->prev=NULL;
delete(temp);
}
void deleteatend()
{
node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->prev->next=NULL;
delete(temp);
}
void deleteatpos(int pos)
{
node *temp=head;
int i=1;
while(i<pos)
{
temp=temp->next;
i++;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
delete(temp);
}
int main()
{
int choice,val,pos;
cout<<" MENU DRIVEN PROGRAM OF DOUBLY LINKED LIST ";
cout<<"\n1.Insertion at beg\n2.Insertion at end\n3.Insertion at random position\n4. Display\n"
"5.Deletion at beg\n6.Deletion at end\n7.Deletion at random position\n8.Exit";
while(1)
{
cout<<"\nEnter the choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter the value to be inserted: ";
cin>>val;
insertatbeg(val);
break;
case 2: cout<<"Enter the value to be inserted: ";
cin>>val;
insertatend(val);
break;
case 3: cout<<"Enter the value to be inserted: ";
cin>>val;
cout<<"Enter the position where value to be inserted: ";
cin>>pos;
insertatpos(val,pos);
break;
case 4:display();
break;
case 5:deleteatbeg();
break;
case 6:deleteatend();
break;
case 7:cout<<"Enter the position where value to be deleted: ";
cin>>pos;
deleteatpos(pos);
break;
case 8: exit(0);
default: cout<<" Invalid choice.......... ";
}
}
return 0;
}