-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoublelinkedlist_ops.cpp
162 lines (162 loc) · 2.84 KB
/
doublelinkedlist_ops.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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *prev;
Node *next;
Node(int val){
data=val;
prev=NULL;
next=NULL;
}
};
class dlinkedlist{
Node *head;
public:
dlinkedlist(){
head=NULL;
}
void insertAtBeginning(int);
void insertAtEnd(int);
void deleteFromBeginning();
void deleteFromEnd();
void deleteItem(int);
void display();
};
void dlinkedlist::insertAtBeginning(int value){
Node *curr=new Node(value);
curr->data=value;
curr->prev=NULL;
curr->next=head;
if(head!=NULL){
head->prev=curr;
}
head=curr;
return;
}
void dlinkedlist::insertAtEnd(int value){
Node *curr=new Node(value);
curr->data=value;
curr->next=NULL;
if(head==NULL){
curr->prev=NULL;
head=curr;
return;
}
Node *temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=curr;
curr->prev=temp;
return;
}
void dlinkedlist::deleteFromBeginning(){
if(head==NULL){
cout<<"List is empty"<<endl;
return;
}
Node *temp=head;
head=head->next;
if(head!=NULL){
head->prev=NULL;
}
delete temp;
}
void dlinkedlist::deleteFromEnd(){
if(head==NULL){
cout<<"List is empty"<<endl;
return;
}
Node *temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
if(temp->prev!=NULL){
temp->prev->next=NULL;
}
else{
head=NULL;
}
delete temp;
}
void dlinkedlist::deleteItem(int value){
if(head==NULL){
cout<<"List is empty"<<endl;
return;
}
Node *temp=head;
while(temp->data!=value){
temp=temp->next;
}
if(temp->prev!=NULL){
temp->prev->next=temp->next;
}
else{
head=temp->next;
}
if(temp->next!=NULL){
temp->next->prev=temp->prev;
}
delete temp;
}
void dlinkedlist::display(){
if(head==NULL){
cout<<"List is empty"<<endl;
return;
}
Node *temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main(){
dlinkedlist list;
int ch,data;
do{
cout<<"1. Insert at beginning"<<endl;
cout<<"2. Insert at end"<<endl;
cout<<"3. Delete from beginning"<<endl;
cout<<"4. Delete from end"<<endl;
cout<<"5. Delete item"<<endl;
cout<<"6. Display"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch){
case 1:
cout<<"Enter data: ";
cin>>data;
list.insertAtBeginning(data);
break;
case 2:
cout<<"Enter data: ";
cin>>data;
list.insertAtEnd(data);
break;
case 3:
list.deleteFromBeginning();
break;
case 4:
list.deleteFromEnd();
break;
case 5:
cout<<"Enter data: ";
cin>>data;
list.deleteItem(data);
break;
case 6:
list.display();
break;
case 7:
exit(0);
break;
default:
cout<<"Invalid choice"<<endl;
}
}while(1);
return 0;
}