-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab4.tar
More file actions
276 lines (225 loc) · 20 KB
/
Copy pathlab4.tar
File metadata and controls
276 lines (225 loc) · 20 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
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
list.h 0000600 0065137 0016273 00000002140 14314753375 011760 0 ustar lsmit248 lsmit248 /* Laura Smith 9/28/2022 1:09 AM list.h 40 lines
Lab 4 CS 202 Adding List-Based Intergers
This header file initilizes things for the node and list classes.
Node class just worries about the nodes or the chains of the linked
lists, each chain has a number and a pointer to the next chain.
List initialized functions used to make the linked lists and then add
them together later.
*/
#include <iostream>
#include <string>
#include <cstdio>
//start of Node class
class Node {
public: //public functions to make the nodes
Node(int); //function to make the node which takes an int
int data; //number of the node
Node *next; //pointer to next node
};
//end of Node class
//start of List class
class List {
private:
Node *head; //private pointer to the head
Node *newNode(int); //private function to only use in list.cpp to make a new node
int count; //used in getCount
public: //public function initilizations
List(); //constructor
~List(); //deconstructor
void addNode(int);
int empty();
int getCount();
Node *getHead();
void print();
};
//end of List class list.cpp 0000600 0065137 0016273 00000005515 14314753376 012325 0 ustar lsmit248 lsmit248 /* Laura Smith 9/28/2022 1:09 AM list.cpp 105 lines
Lab 4 CS 202 Adding List-Based Intergers
This creates the functions that were initialized in the header file. The node
function just creates the nodes of the linked list which each node has data and
a pointer to the next node. For list there is a constructor and deconstructor along
with functions to help in main. It initiates the functions newNode, addNode, empty,
getCount, getHead, and print. In print I also learned how to use strings and push_back better.
*/
#include "list.h"
#include <string>
#include <sstream>
using namespace std;
//start of Node functions
Node::Node(int i) {
data = i;
next = NULL;
}
//end of Node functions
//start of List functions
List::List() {
head = NULL;
count = 0;
}
//copied Dr. Emrich's function
List::~List() {
if (!empty()) { // follow the links, clobbering as we go
Node *p = head;
while (p != NULL) {
Node *next = p->next; // retrieve this node's "next" before we clobber it
delete p;
p = next;
}
}
}
//helper function to create a new node
Node *List::newNode(int i) {
Node *node = new Node(i);
node->next = NULL;
return node;
}
//copied Dr. Emrich's function
void List::addNode(int i) {
Node *node; //creates a node pointer to the linked list
if (head == NULL){ //if this is a new list and it is empty, calls newNode
head = newNode(i);
}
else { //otherwise it adds the inputed value into the next node on the list
for(node=head; node->next != NULL; node = node->next);
node->next = newNode(i);
}
}
//function to check if a list is empty
int List::empty() {
if (head != NULL) //if it has stuff in it
return(false);
else //if it doesn't have stuff in it
return(true);
}
int List::getCount() {
Node *node; //make a pointer to the head node
int count = 0; //int to keep track of the count
for (node = head; node->next != NULL; node = node->next)
count++; //for loop goes through each node and just adds to count for each node
return (count);
}
//returns pointer to the first node/ the head
Node *List::getHead() {
return (head);
}
//function to print out the linked lists in the correct order
void List::print() {
if (!empty()) {
string numPrint; //string of the numbers to use later
if (!empty()){
Node *node = head; //pointer to the head of the linked list
while(node != NULL){ //until the list reaches the end, it pushes the list's data into the string
numPrint.push_back(node->data + '0');
node = node->next;
}
for (int i = numPrint.size()-1; i>=0; i--){
cout << numPrint[i]; //for loop to go through the string to print it out to the screen
}
}
}
else
printf("empty\n"); //if the linked list is empty it returns as empty
}
//end of List functions main.cpp 0000600 0065137 0016273 00000007704 14314753373 012275 0 ustar lsmit248 lsmit248 /* Laura Smith 9/28/2022 1:09 AM main.cpp 133 lines
Lab 4 CS 202 Adding List-Based Intergers
First is the sumList function which actually adds together the lists, while
in main the lists are created and then sumList is called for them. I learned
how to use node pointers and the other syntax related to linked lists.
*/
#include "list.h"
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
//helper function to add contents of list
List *sumLists(List *list1, List *list2) {
int carry = 0; //The carried value, should always be 1 or 0
int value = 0; //The value getting put into the answer linked list
List *list3 = new List; //answer linked list
//pointers to the head of both numbers
Node *node1 = list1->getHead();
Node *node2 = list2->getHead();
//if either of the lists are empty, just return the other list
if (list1->empty()){
return list2;
}
else if (list2->empty()){
return list1;
}
//while loop which goes through until both numbers are at the end
while (node1 != NULL || node2 != NULL){
//these keep track of the current value of each list
int val1 = 0;
int val2 = 0;
//both of these if/else statements check if it's at the end
//when at the end it sets the current value of the list at 0
//if it has a value, it sets the current value to it and then goes to the next node in the list
if (node1 == NULL)
val1 = 0;
else{
val1 = node1->data;
node1 = node1->next;
}
if (node2 == NULL)
val2 = 0;
else {
val2 = node2->data;
node2 = node2->next;
}
//this caculates the value that goes into the answer list
//carried value, and the data from the same point in the lists are added
value = carry + val1 + val2;
//if the value is over 9 then 1 will have to be carried and value is only the remainder
//if not over nine the carry value is set back to 0
if (value > 9){
carry = 1;
value = value % 10;
}
else
carry = 0;
//finally adds the final value to the answer list
list3->addNode(value);
}
//if carry is over 0 after both lists are at the end, it adds one to the end of the answer linked list
if (carry > 0)
list3->addNode(carry);
//returns the answer linked list
return list3;
}
int main(int argc, char *argv[]) {
string Digits; //string of the inputed line of numbers
while (getline(cin, Digits)){ //while loop goes as long as there is input
//puts the numbers into digits
istringstream sin(Digits);
//puts each number into it's own string
string num1;
sin >> num1;
string num2;
sin >> num2;
//list pointers to make linked lists for both numbers and the answer list
List *list1 = new List;
List *list2 = new List;
List *list3;
//these keep track of the length of each number
int size1 = num1.size();
int size2 = num2.size();
//for loops to go through each number string to put it into a linked list backwards
for (int i = 1; i <= size1; i++){
int digit = (int)num1[size1 - i]; //starts at the last number and goes to the first
list1->addNode(digit - 48); //-48 to translate from ASCII to int
}
for (int i = 1; i <= size2; i++){
int digit = (int)num2[size2 - i];
list2->addNode(digit - 48);
}
//puts the answer list into list3 and then prints it
list3 = sumLists(list1, list2);
list3->print();
cout << endl;
//these delete anything new was called on, which are the three linked lists
delete list1;
delete list2;
delete list3;
}
return 0;
}