-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54_circularLinkedList.cpp
94 lines (90 loc) · 2.02 KB
/
54_circularLinkedList.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
#include <iostream>
using namespace std;
class Node{
public:
int info;
Node *next;
Node(int data){
this->info = data;
this->next = NULL;
}
~Node(){
int value = this->info;
if(this->next != NULL){
delete next;
this->next = NULL;
}
cout<<"Node deleted: "<<value<<endl;
}
};
Node* tail = NULL, *temp = NULL, *newn = NULL;
int count = 0;
void insertNode(int element,int data){
//empty list
if(tail == NULL){
newn = new Node(data);
tail = newn;
newn->next = newn;
count++;
}
//non-empty list
else{
temp = tail;
while(temp->info != element){
temp = temp->next;
}
//element found and temp is pointing to the node with info as element
newn = new Node(data);
newn->next = temp->next;
temp->next = newn;
count++;
}
}
void deleteNode(int element){
if(tail == NULL){
cout<<"List is empty"<<endl;
return;
}
else{
Node *prev = tail;
temp = prev->next;
while(temp->info != element){
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
if(temp == prev){
tail = NULL;
}
if(tail == temp){
tail = prev;
}
temp->next = NULL;
delete temp;
count--;
}
}
void printList(){
temp = tail->next;
int n = 1;
while(n<=count){
cout << temp->info << " ";
temp = temp->next;
n++;
}
cout<<endl;
}
int main(){
insertNode(3,3);
insertNode(3,10);
insertNode(3,20);
insertNode(20,15);
insertNode(15,30);
insertNode(30,7);
printList();
deleteNode(3);
printList();
cout<<endl<<"Total Nodes: "<<count<<endl;
cout<<"Tail: "<<tail->info<<endl;
return 0;
}