-
Notifications
You must be signed in to change notification settings - Fork 1
/
cs235_assign10.cpp
240 lines (215 loc) · 7.54 KB
/
cs235_assign10.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/******************************************************************************
* Program:
* Assignment 10, Linked List implementation
* Brother Ercanbrack, CS 235
* Author:
* Tyler Scott
* Summary:
* this program implements the member functions of the list class
*
*
* Estimated time: 8.0hrs
* Actual time: 5.0hrs
******************************************************************************/
#include <iostream>
#include "list.h"
using namespace std;
// Node Member Functions
/******************************************************************************
* Default constructor needs to be totally empty so it can work with stuff
*****************************************************************************/
Node :: Node() // --- Creates empty node
{
}
/******************************************************************************
* Grants access to the private variable "next"
*****************************************************************************/
Node* Node :: getNext()
{
return next; // --- returns next pointer of a node
}
/******************************************************************************
* Grants access to the private variable "data"
*****************************************************************************/
int Node :: getData()
{
return data; // --- returns data of a node
}
/******************************************************************************
* Sets the value of Data to what is passed in through the parameters
*****************************************************************************/
void Node :: setData(int item)
{
data = item; // --- stores item in data member of current node
}
/******************************************************************************
* Sets the value of "next" to what is passed in through the parameters
*****************************************************************************/
void Node :: setNext(Node* node)
{
next = node; // --- sets next to point to node
}
// List Member Functions
/******************************************************************************
* Default Constructor
*****************************************************************************/
List :: List()
{
numItems = 0;
firstNode = NULL;
}
/******************************************************************************
* Destructor clears out the list
*****************************************************************************/
List :: ~List()
{
while (firstNode != NULL)
{
Node* deletor = firstNode;
firstNode = firstNode->getNext();
delete deletor;
}
}
/******************************************************************************
* Copy Constructor
*****************************************************************************/
List :: List(const List &listToCopy)
{
numItems = 0;
firstNode = 0;
for (int i = 0; i < listToCopy.numItems; i++)
{
insert(listToCopy.getData(i), i);
}
}
/******************************************************************************
* Assignment Operator
*****************************************************************************/
List& List :: operator = (const List &rightSide)
{
while (firstNode != NULL)
{
Node* deletor = firstNode;
firstNode = firstNode->getNext();
delete deletor;
}
numItems = 0;
firstNode = 0;
for (int i = 0; i < rightSide.numItems; i++)
{
insert(rightSide.getData(i), i);
}
}
/******************************************************************************
* The insert function puts things (nodes) into the list. in the right places
*****************************************************************************/
void List :: insert(int item, int pos)
{
// check to make sure position is pointing within the list boundaries
if (pos < 0 || pos > numItems)
{
if (pos < 0)
pos = 0;
else
pos = numItems;
}
// Set up some new nodes
Node* stuff = new Node();
Node* curr = firstNode;
stuff->setData(item); // set one of them with the data passed in
// check to make sure its not the first position, or if the list is empty
if (pos < 1 || firstNode == NULL)
{
stuff->setNext(curr); // set the next node as the current one
firstNode = stuff;
}
else
{
for (int i = 1; i < pos; i++)
{
curr = curr->getNext(); // set the current node to point to the next
}
stuff->setNext(curr->getNext()); // set the next node
curr->setNext(stuff); // set the current node to the temp node
}
numItems++; // increment the number of items since adding one
}
/******************************************************************************
* Remove deletes an item in the specified position of the list
*****************************************************************************/
void List :: remove(int pos)
{
// check to make sure position is pointing within the list boundaries
if (pos < 0 || pos > numItems)
{
if (pos < 0)
pos = 0;
else
pos = numItems - 1;
}
Node* curr = firstNode;
// check to make sure its not the first position, or if the list is empty
if (pos < 1 || firstNode == NULL)
{
firstNode = curr->getNext(); // point the first to the next
delete curr; // delete the current one
}
else
{
for (int i = 1; i < pos; i++)
{
curr = curr->getNext(); // set the current node to point to the next
}
Node* remove = curr->getNext(); // make a temp one set to the node being
// removed
curr->setNext(remove->getNext()); // set the next node to the temp ones
delete remove; // delete it
}
numItems--; // decrement the number of items
}
/******************************************************************************
* The lookup function walks through the list to check if a specific item
* is located in the list.
*****************************************************************************/
int List :: lookup(int item) const
{
Node* search = firstNode; // set up a temporary node to search with
for (int i = 0; i < numItems; i++)
{
if (search->getData() == item) // if the search matches an item return
return i;
search = search->getNext(); // set to the next node and keep looping
}
return -1; // it was not in the list
}
/******************************************************************************
* Gets the Data from each node in the list
*****************************************************************************/
int List :: getData(int pos) const
{
if (pos < 0 || pos > numItems) // if the postion is pointing to outside
return -1; // the list boundaries end
Node* temp = firstNode; // set up a temp holder
for (int i = 0; i < pos; i++)
{
temp = temp->getNext(); // walk through the list, point to the next
}
return temp->getData(); // return the data recieved
}
/******************************************************************************
* Checks to see if the list is empty
*****************************************************************************/
int List :: empty(void) const
{
if (firstNode == NULL) // if the first node is pointing to Null its empty
return 1;
else
return 0;
}
/******************************************************************************
* Grants access to the private variable "numItems"
*****************************************************************************/
int List :: getNumItems(void) const
{
return numItems;
}