-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathdoublylinkedlist.cpp
125 lines (123 loc) · 2.51 KB
/
doublylinkedlist.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
#include <iostream>
using namespace std;
class Node{
public:
Node* next;
Node* prev;
int data;
Node(int d){
data =d;
next=NULL;
prev=NULL;
}};
void search(Node* &head, int d){
if (head == NULL){
cout << "List is empty";
exit(0);
}
Node* temp=head;
while(temp!=NULL && temp->data!=d){
temp=temp->next;
}
if (temp==NULL){
cout << "Element not found";
}
else cout << "Element found";
}
void display(Node* &head){
if (head == NULL){
cout << "List is empty";
}
Node* temp=head;
while(temp!=NULL){
cout << temp->data << endl;
temp=temp->next;
}
}
void InsertBeg(Node* &head, int d){
Node* temp = new Node(d);
temp->prev=NULL;
temp->next=head;
if (head==NULL){
head=temp;
}
else {
head->prev=temp;
head=temp;
}
}
void InsertEnd(Node* &head, int d){
Node*temp = new Node(d);
temp->prev=NULL;
temp->next=NULL;
if (head==NULL){
temp=head;
}
else{
Node*temp1=head;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp1->next=temp;
temp->prev=temp1;
}
}
void deleteBeg(Node* &head){
if (head==NULL) cout << "list is empty";
else {
head=head->next;
head->prev=NULL;
}
}
void deleteEnd(Node* &head){
if (head==NULL) cout << "list is empty";
else {
Node*temp=head;
while(temp->next->next!=NULL){
temp=temp->next;
}
temp->next=NULL;
}
}
void deleteSpec(Node* &head,int num)
{
if(head==NULL)
cout<<"Empty List"<<endl;
else
{
Node* temp=head;
while(temp->next!=NULL && temp->data!=num)
{
temp=temp->next;
}
if(temp==head)
{
head=head->next;
head->prev=NULL;
}
else if(temp->next==NULL)
{
temp->prev->next=NULL;
}
else
{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
}
}
}
int main(){
Node *n1=new Node(10);
Node *n2=new Node(20);
Node *n3= new Node(50);
Node *n4=new Node(60);
Node *head =n1;
n1->next=n2;
n2->prev=n1;
n2->next=n3;
n3->prev=n2;
n3->next=n4;
n4->prev=n3;
deleteEnd(head);
display(head);
}