forked from adi1998/CSN-102
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularLinkedList.cpp
142 lines (114 loc) · 3 KB
/
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
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
// Circular Linked List implementation in C++
#include<iostream>
#include<string>
using namespace std;
template<typename T>
class Node{
private:
public:
Node<T> *left;
Node<T> *right;
T data;
Node();
Node(Node<T> *left, T data, Node<T> *right);
~Node();
};
template <class T>
Node<T>::Node(){
this->left = NULL;
this->right = NULL;
}
template <class T>
Node<T>::Node(Node<T> *left, T data, Node<T> *right){
this->left = left;
this->right = right;
}
template <class T>
Node<T>::~Node(){
delete this->left;
delete this->right;
delete this->data;
}
template <typename T>
class CircularLinkedList{
private:
Node<T> *first;
public:
CircularLinkedList();
~CircularLinkedList();
int length;
CircularLinkedList<T>* Add(const T data);
CircularLinkedList<T>* Insert(int k, const T data);
int Find(int k, const T data);
int Search(const T data);
CircularLinkedList<T> Delete(int k);
void print();
void printReferences();
};
template <class T>
CircularLinkedList<T>::CircularLinkedList(){
Node<T> *headNode = new Node<T>();
headNode->left = headNode->right = headNode;
headNode->data = 0;
first = headNode;
length = 0;
}
template <class T>
CircularLinkedList<T>* CircularLinkedList<T>::Add(const T data){
Node<T> *newNode = new Node<T>();
newNode->left = first->left;
newNode->left->right = newNode;
newNode->right = first;
newNode->data = data;
first->left = newNode;
length++;
return this;
}
template<class T>
CircularLinkedList<T> *CircularLinkedList<T>::Insert(int k, const T data){
if (k >= length || k < 0)
throw out_of_range("Index does not exists.");
Node<T> *newNode = new Node<T>();
Node<T> *current = first;
int index = 0;
while (index != k+1){
index++;
current = current->right;
}
current->left->right = newNode;
newNode->left = current->left;
newNode->right = current;
current->left = newNode;
newNode->data = data;
cout << data << endl;
length++;
return this;
}
template <class T>
void CircularLinkedList<T>::print(){
Node<T> *current = first->right;
while (current->right != first){
cout << current->data << " ";
current = current->right;
}
cout << endl;
}
template <class T>
void CircularLinkedList<T>::printReferences(){
Node<T> *current = first->right;
while (current->right != first){
cout << current->left << "<-" << current << "->" << current->right << " | " << current->data << endl;
current = current->right;
}
cout << endl;
}
int main(){
CircularLinkedList<int> *newList = new CircularLinkedList<int>();
for (int i = 0; i < 10; i++){
newList->Add((int)(i));
}
newList->Insert(8, 410);
cout << endl << "Length is: " << newList->length << endl;
//newList->printReferences();
newList->print();
}