-
Notifications
You must be signed in to change notification settings - Fork 214
/
doublycircular.cpp
147 lines (145 loc) · 3.07 KB
/
doublycircular.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
#include <iostream>
using namespace std;
class Node{
public:
Node* head;
Node* prev;
Node* next;
int data;
Node(int d){
data=d;
prev=NULL;
next=NULL;
}
Node(){
prev=NULL;
next=NULL;
}
};
void display(Node* &head){
Node* temp=head;
while(temp->next!=head){
cout << temp->data << endl;
temp=temp->next;
}
cout << temp->data << endl;
}
void InsertBeg(Node* &head, int x){
Node* temp=new Node(x);
if (head==NULL){
temp->prev=temp->next=temp;
head=temp;
}
else{
temp->next=head;
temp->prev=head->prev;
head->prev=temp;
temp->prev->next=temp;
head=temp;
}
}
void InsertEnd(Node* &head, int x){
Node* temp = new Node(x);
if (head==NULL){
temp->next=temp->prev=temp;
head=temp;
}
else{
temp->next=head;
temp->prev=head->prev;
head->prev->next=temp;
head->prev=temp;
}
}
void InsertSpec(Node* &head, int x, int value){
Node* temp= new Node(x);
if (head==NULL){
temp->prev=temp->next=temp;
head=temp;
}
else {
Node* temp1=head;
while(temp1->next!=head && temp1->data!=value){
temp1=temp1->next;
}
temp->next=temp1->next;
temp->prev=temp1;
temp1->next=temp;
temp1->next->prev=temp;
}
}
void DeleteBeg(Node* &head){
if (head==NULL){
cout << "List is empty";
}
else{
Node* temp=head;
if (temp->next==head){
head=NULL;
}
else{
head->prev->next=head->next;
head->next->prev=head->prev;
head=head->next;
free(temp);
}
}
}
void DeleteEnd(Node* &head){
if (head==NULL){
cout << "List is empty";
}
else{
Node* temp=head;
while(temp->next!=head){
temp=temp->next;
}
if (temp==head){
head=NULL;
}
else {
temp->prev->next=head;
head->prev=temp->prev;
delete temp;
}
}
}
void DeleteSpec(Node* &head, int value){
if (head==NULL){
cout << "List is empty";
}
else {
Node* temp =head;
while(temp->next!=head && temp->data!=value){
temp=temp->next;
}
if (temp->data!=value){
cout << "Element not found";
}
else if (temp==head){
DeleteBeg(head);
}
else if (temp->next==head){
DeleteEnd(head);
}
else {
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
}
}
int main(){
Node *n1=new Node(4);
Node *n2=new Node(12);
Node *n3=new Node(10);
Node* head=n1;
n1->next=n2;
n1->prev=n3;
n2->next=n3;
n2->prev=n1;
n3->next=n1;
n3->prev=n2;
DeleteSpec(head,4);
display(head);
}