-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathsinglycircularlinkedlist.cpp
More file actions
163 lines (162 loc) · 3.48 KB
/
singlycircularlinkedlist.cpp
File metadata and controls
163 lines (162 loc) · 3.48 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
#include <iostream>
using namespace std;
class Node{
public:
Node* head;
Node* prev;
Node* next;
int data;
Node(int d){
data=d;
next=NULL;
prev=NULL;
}
Node()
{
next=NULL;
prev=NULL;
}
};
void Search(Node* &head, int num){
if (head==NULL){
cout << "List is empty";
}
else{
Node*temp =head;
while(temp->next!=head && temp->data!=num){
temp=temp->next;
}
if (temp->data==num){
cout << "Element found";
}
else cout << "Element not found";
}
}
void display(Node* & head){
if (head==NULL){
cout << "List is empty";
}
else{
Node* temp=head;
while(temp->next!=head){
cout << temp->data << endl;
temp=temp->next;
}
cout << temp->data << endl;
}
}
void InsertBeg(Node* &head, int num){
Node* temp= new Node(num);
if(head==NULL){
head=temp;
head->next=head;
}
else {
temp->next=head;
Node* temp1=head;
while(temp1->next!=head){
temp1=temp1->next;
}
temp1->next=temp;
head=temp;
}
}
void InsertEnd(Node* &head, int num){
Node* temp= new Node(num);
if (head==NULL){
head=temp;
head->next=head;
}
else {
Node*temp1=head;
while(temp1->next!=head){
temp1=temp1->next;
}
temp1->next=temp;
temp->next=head;
}
}
void InsertSpec(Node* &head, int num, int value){
Node* temp= new Node(num);
if (head==NULL){
head=temp;
head=head->next;
}
else {
Node* temp1=head;
while(temp1->next!=head && temp1->data!=value){
temp1=temp1->next;
}
if (temp1->next==head && temp1->data!=value){
cout << "Node is not present";
}
else {
temp->next=temp1->next;
temp1->next=temp;
}
}
}
void DeleteBeg(Node* &head){
if (head==NULL){
cout << "The list is empty";
}
else {
Node* temp=head;
while(temp->next!=head){
temp=temp->next;
}
temp->next=head->next;
head=head->next;
free(temp);
}
}
void DeleteEnd(Node* &head){
if (head==NULL){
cout << "The list is empty";
}
else{
Node*temp=head;
Node*prev=temp;
while(temp->next->next!=head){
prev=temp;
temp=temp->next;
}
prev->next=temp;
temp->next=head;
free(temp);
}
}
void DeleteSpec(Node* &head, int value){
if (head==NULL) cout << "List is empty";
else {
Node* temp=head;
Node* prev=temp;
while(temp->next!=head && temp->data!=value){
prev=temp;
temp=temp->next;
}
if (temp->data!=value){
cout << "Value is not present";
}
else if (temp==head){
DeleteBeg(head);
}
else if (temp->next==head){
DeleteEnd(head);
}
else {
prev->next=temp->next;
}
}
}
int main(){
Node *n1=new Node(10);
Node *n2=new Node(20);
Node *n3= new Node(30);
Node* head=n1;
n1->next=n2;
n2->next=n3;
n3->next=head;
DeleteSpec(head,30);
display(head);
}