-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinked.cpp
More file actions
141 lines (123 loc) · 2.45 KB
/
Copy pathCircularLinked.cpp
File metadata and controls
141 lines (123 loc) · 2.45 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
#include "CircularLinked.h"
CircularLinkedList::CircularLinkedList() //default just in case
{
root = new Node();
root -> next = root;
size = 0;
}
CircularLinkedList::CircularLinkedList(const CircularLinkedList& other) // copy const
{
if (other.size == 0)
{
CircularLinkedList(); // check if other is empty
}
else
{
root = new Node();
size = 0;
Node* temp = new Node(); //creation of new one using temporary to move through the other one
temp = other.root;
temp -> next = other.root -> next;
add(temp -> data);
while(temp -> next! = other.root)
{
temp = temp -> next;
add(temp -> data);
}
delete[]temp;
}
get_last() -> next = root;
}
CircularLinkedList& CircularLinkedList::operator = (const CircularLinkedList& other) // operator =
{
if (&other != this)
{
delete this;
if (other.size == 0) // same as above but deleting the old list and checking also if they are already the same
{
CircularLinkedList();
}
else
{
root = new Node();
size = 0;
Node* temp = new Node();
temp = other.root;
temp -> next = other.root -> next;
add(temp -> data);
while(temp -> next! = other.root)
{
temp = temp -> next;
add(temp -> data);
}
delete[]temp;
}
get_last() -> next = root;
}
return *this;
}
CircularLinkedList::~CircularLinkedList() //destructor
{
Node* temp = get_root() -> next;
Node* temp2;
delete[]root;
size--;
while (size > 0)
{
temp2 = temp -> next;
delete[]temp;
temp = temp2;
size--;
}
}
Node* CircularLinkedList::get_root()
{
return root;
}
Node* CircularLinkedList::get_second()
{
return root -> next;
}
Node* CircularLinkedList::get_third()
{
Node* second = root -> next;
return second -> next;
}
Node* CircularLinkedList::get_last() //ease of access
{
Node* last = get_root();
int br = 1;
while(br<size)
{
last = last -> next;
br++;
}
return last;
}
int CircularLinkedList::get_size()
{
return size;
}
void CircularLinkedList::add(Symbol x) // adding a new node at the end of the list
{
if(size == 0) // check if the list is empty
{
delete[]root;
root = new Node(x, root);
size++;
}
else
{
Node* temp = new Node(x, root);
get_last() -> next = temp;
size++;
}
}
void CircularLinkedList::spin(int times)
{
while(times != 0) //rotatin by moving the root pointer
{
root = root -> next;
times--;
}
}