-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcs235_assign12.cpp
402 lines (360 loc) · 13.3 KB
/
cs235_assign12.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/******************************************************************************
* Program:
* Assignment 12, Fibbonacci Doubly Linked List implementation
* Brother Ercanbrack, CS 235
* Author:
* Tyler Scott
* Summary:
* This program takes a number input by the user. and then adds all the numbers
* in the fibonacci sequence up to that number
*
* Estimated time: 8.0hrs
* Actual time: 4.0hrs
******************************************************************************/
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
/*****************************************************************************
* Node class definition for building a single linked list of integers.
*****************************************************************************/
class Node
{
private:
int data; // --- data in each node
Node* next; // --- pointer to next node
Node* previous; // --- pointer to the previous node
public:
Node(); // --- Creates empty node
Node* getNext(); // --- returns next pointer of a node
Node* getPrevious(); // --- returns previous pointer of a node
int getData(); // --- returns data of a node
void setData(int item); // ---stores item in data member of current node
void setNext(Node* node); // --- sets next to point to node
void setPrevious(Node* node); // --- sets previous to point to node
};
/*******************************************************************************
* This class provides a dynamically allocated linked list implementation of a
* general list. It includes functions for creating, copying, and destroying a
* linked list data structure. It includes functions for inserting, removing,
* and retrieving data anywhere in the linked list data structure.
******************************************************************************/
class List
{
private:
int numItems;
Node* firstNode;
public:
List();
~List(); // destructor - free the entire linked list
List(const List &listToCopy); // copy constructor
List& operator = (const List &rightSide); // assignment operator overload
List operator + (List &rightSide); // addition operator overload
void insert(int item, int pos); // insert item at the specified position
void remove(int pos); // remove item at the specified position
int lookup(int item) const; // returns position of item (zero relative)
int getData(int pos) const; // returns data located at the position
int empty(void) const; // check for empty list(0=empty 1=not empty)
int getNumItems(void) const; // return # of items in the linked list
void display();
};
// Node Member Functions
/******************************************************************************
* Default constructor needs to be totally empty so it can work with stuff
*****************************************************************************/
Node :: Node() // --- Creates empty node
{
data = 0;
next = NULL;
previous = NULL;
}
/******************************************************************************
* Grants access to the private variable "next"
*****************************************************************************/
Node* Node :: getNext()
{
return next; // --- returns next pointer of a node
}
/******************************************************************************
* Grants access to the private variable "previous"
*****************************************************************************/
Node* Node :: getPrevious()
{
return previous; // --- returns previous 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
}
/******************************************************************************
* Sets the value of "previous" to what is passed in through the parameters
*****************************************************************************/
void Node :: setPrevious(Node* node)
{
previous = node; // --- sets previous 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);
}
}
/******************************************************************************
* Addition Operator
*****************************************************************************/
List List :: operator + (List &rightSide)
{
List sum; // new list for the sum
int first; // first number
int second; // second number
int result; // result of their addition
int carry = 0; // carry over
int i = 0;
//loop through until you reach the size, which is the end
while (i < numItems || i < rightSide.numItems)
{
if (i < numItems)
{
first = getData(i); // get the first number from the first list
}
else
first = 0;
if (i < rightSide.numItems)
{
second = rightSide.getData(i); // get the second number from the second
}
else
second = 0;
int temp = first + second + carry; // set up a temp to hold the sum of all
result = temp % 100000000;
carry = temp / 100000000;
sum.insert(result, i); // insert the result into the sum list
i++;
}
if (carry > 0) // if there is a carry insert that too.
sum.insert(carry, i);
return sum;
}
/******************************************************************************
* 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;
stuff->setPrevious(NULL);
}
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
stuff->setPrevious(curr); // set the previous node
if (stuff->getNext())
stuff->getNext()->setPrevious(stuff);
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
remove->getNext()->setPrevious(curr);
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;
}
/******************************************************************************
* this is the display function, outputs to the screen for users
*****************************************************************************/
void List::display()
{
Node* disp = firstNode; //set up a new node to point to the first in the list
while (disp->getNext() != NULL)
{
disp = disp->getNext(); // point to the next one
}
for (; disp!= NULL; disp = disp->getPrevious())
{
cout << disp->getData(); // display to the screen
}
}
int main(int argc, char* argv[])
{
int cmdIn; // integer from command line
if (argc == 2)
{
cmdIn = atoi(argv[argc - 1]);
}
else
{
cout << "error" << endl;
return 0;
}
List fibNum1; // new list object for first number
fibNum1.insert(1,0); // insert a 1 at position 0 (since you will be adding
// all the integers between 0 and the input number
List fibNum2; // new list object for second number
fibNum2.insert(1,0);
List fibNum3; // new list object for third number
for (int i = 3; i <= cmdIn; i++)
{
fibNum3 = fibNum1 + fibNum2; // add 1 and 2 for sum
fibNum1 = fibNum2; // now set the original 1st to the second
fibNum2 = fibNum3; // and set the second to the sum
}
fibNum3.display(); // call display
cout << endl << endl;
return 0;
}