-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.2.cpp
More file actions
183 lines (183 loc) · 4.6 KB
/
8.2.cpp
File metadata and controls
183 lines (183 loc) · 4.6 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
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
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
struct node *prev;
};
struct node *start = NULL;
int n;
int insert_end(int data);
int insert_begin(int data);
int insert_after(int data);
int delete_begin();
int delete_at();
int delete_end();
void display();
void reverse();
int main()
{
int c=-1,data,ch,r;
while(c!=0){
cout<<"0.exit\n1.Insertion\n2.Deletion\n3.Display\n4.Reverse the list"<<endl;
cin>>c;
switch(c){
case 1 : cout<<"1.Insertion at Begining\n2.Insertion after an element\n3.Insertion at End"<<endl;
cin>>ch;
cout<<"enter the data"<<endl;
cin>>data;
if(ch==1)
insert_begin(data);
else if(ch==2){
r = insert_after(data);
if(r==-1) cout<<"search element is not in the list"<<endl;}
else if(ch==3)
insert_end(data);
break;
case 2 : if(start==NULL)
cout<<"linked list is empty"<<endl;
else if(start->next==NULL)
start = NULL;
else{
cout<<"1.Deletion at Begining\n2.Deletion of a specific element\n3.Deletion at End"<<endl;
cin>>ch;
if(ch==1)
delete_begin();
else if(ch==2){
r = delete_at();
if(r==-1) cout<<"search element is not in the list"<<endl;}
else if(ch==3)
delete_end();
}
break;
case 3 : if(start==NULL)
cout<<"linked list is empty"<<endl;
else
display();
break;
case 4 : if(start==NULL)
cout<<"linked list is empty"<<endl;
else
reverse();
break;
default : if(c!=0) cout<<"enter correct choice"<<endl;
}
}
return 0;
}
int insert_begin(int data)
{
struct node *t;
t = (struct node*)malloc(sizeof(struct node));
t->data=data;
t->next = start;
t->prev = NULL;
start = t;
return 0;
}
int insert_after(int data)
{
int element;
if(start!=NULL){
cout<<"enter the element you want to insert the new data after it"<<endl;
cin>>element;
struct node *temp,*t;
t = (struct node*)malloc(sizeof(struct node));
t->data=data;
temp = start;
while(element!=temp->data){
if(temp->next == NULL)
return -1;
temp = temp->next;
}
t->prev = temp;
t->next = temp->next;
temp->next = t;
temp = temp->next;
t = t->next;
t->prev = temp;
}
else
insert_begin(data);
}
int insert_end(int data)
{
struct node *temp,*t;
temp = start;
t = (struct node*)malloc(sizeof(struct node));
t->data=data;
if(start==NULL){
start=t;
start->next = NULL;
return 0;
}
while (temp->next != NULL)
temp = temp->next;
t->prev = temp;
temp->next = t;
t->next = NULL;
return 0;
}
int delete_begin(){
start = start->next;
start->prev = NULL;
}
int delete_at(){
int element;
cout<<"enter the element you want to delete."<<endl;
cin>>element;
struct node *temp,*t;
temp = start;
if(start->data==element)
{
start = start->next;
start->prev = NULL;
return 0;
}
while(element!=temp->data){
if(temp->next == NULL)
return -1;
t = temp;
temp = temp->next;
}
t->next = temp->next;
temp = temp->next;
temp->prev = t;
}
int delete_end()
{
struct node *temp,*t;
temp=start;
while(temp->next!=NULL){
t = temp;
temp = temp->next;
}
t->next = NULL;
}
void display(){
struct node *temp;
temp = start;
while(temp->next != NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data<<endl;
}
void reverse(){
struct node *temp,*r,*t;
temp=start;
while(temp->next != NULL){
r = temp;
temp = temp->next;
t = r->prev;
r->prev = r->next;
r->next = t;
}
r = temp;
t = r->prev;
r->prev = r->next;
r->next = t;
start=r;
cout<<"list after reversing"<<endl;
display();
}