-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.cpp
185 lines (175 loc) · 3.76 KB
/
slist.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "slist.h"
#include <iostream>
using namespace std;
// Constructor
Node *next;
void *data;
Node ::Node() {
next = nullptr;
data = nullptr;
}
Node::Node(void *data) {
this->data = data;
next = nullptr;
}
// constructor function
linkedList::linkedList() { head = nullptr; }
void linkedList::add(void *data) {
Node *newNode = new Node(data);
if (head == nullptr) {
head = newNode;
return;
}
Node *temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
// inserts a node before the index
void linkedList::insert(int index, void *data) {
Node *newNode = new Node(data);
if (index == 0) {
Node *temp = head;
} else {
Node *temp = head;
for (int i = 0; i < index; i++) {
temp = temp->next;
}
newNode = temp->next;
temp->next = newNode;
}
}
// removes the specified node
void linkedList::remove(int index) {
if (index == 0) {
Node *temp = head;
head = head->next;
delete temp;
temp = nullptr;
} else {
}
Node *temp = head;
Node *temp2 = head;
for (int i = 0; i < index; i++) {
if (i < index - 1) {
temp2 = temp2->next;
}
temp = temp->next;
}
if (temp->next != NULL) {
temp2->next = temp->next;
delete temp;
temp = nullptr;
}
}
// gets data from the specified index.
void *linkedList::get(int index) {
if (index == 0) {
return head->data;
} else {
Node *temp = head;
for (int i = 0; i < index; i++) {
temp = temp->next;
}
return temp->data;
}
}
Node *linkedList::getNode(int index) {
if (index == 0) {
return head;
} else {
Node *temp = head;
for (int i = 0; i < index; i++) {
temp = temp->next;
}
return temp;
}
}
// returns size of the linked list
int linkedList::size() {
int count = 0;
Node *temp = head;
while (temp != nullptr) {
temp = temp->next;
count++;
}
return count;
}
// returns true if the linked list is equal to the second linked list false
// otherwise
bool linkedList::equals(linkedList slist2) {
Node *temp = head;
Node *temp2 = slist2.head;
if (size() != slist2.size()) {
return false;
}
while (temp != nullptr && temp2 != nullptr) {
if (temp->data != temp2->data) {
return false;
}
temp = temp->next;
temp2 = temp2->next;
}
return true;
}
// clears the list (basically a destructor)
void linkedList::clear() {
Node *temp = head;
while (temp != nullptr) {
head = head->next;
delete temp;
temp = head;
}
temp = nullptr;
}
// exchanges values between two nodes
void linkedList::exchg(int index1, int index2) {
Node *temp = head;
Node *temp2 = head;
for (int i = 0; i < index1; i++) {
temp = temp->next;
}
for (int i = 0; i < index2; i++) {
temp2 = temp2->next;
}
Node *temp3 = new Node(temp->data);
temp->data = temp2->data;
temp2->data = temp3->data;
free(temp3);
}
// swaps node data at specified node (index1) with specified node (index2)
void linkedList::swap(Node *index1, Node *index2) {
void *temp = index1->data;
index1->data = index2->data;
index2->data = temp;
}
bool linkedList::isEmpty() {
if (head == nullptr) {
return false;
}
return true;
}
// sets specified node's data to that of the new data
void linkedList::set(int index, void *data) {
Node *temp = head;
for (int i = 0; i < index; i++) {
temp = temp->next;
}
temp->data = data;
}
// destructor
linkedList::~linkedList() {
Node *temp = head;
while (temp != nullptr) {
head = head->next;
delete temp;
temp = head;
}
temp = nullptr;
}
// Destructor
// DO NOT IMPLEMENT >>> subList(start, length) //Returns a new list containing
// elements from a sub-range of this list.
// DO NOT IMPLEMENT >>> toString() //Converts the
// list to a printable string representation.