COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
largenum_incomplete.cpp File Reference
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>
Include dependency graph for largenum_incomplete.cpp:

Go to the source code of this file.

Classes

struct  Node
 

Functions

Nodecreate_num_list ()
 
void delete_head (Node *&head)
 
void delete_list (Node *&head)
 
void grow_array (char *&array, int &size)
 
void head_insert (Node *&head, int v)
 
void input_num (char *&digits, int &numDigits)
 
bool larger (Node *n1, Node *n2)
 
int list_length (Node *head)
 
int main ()
 
void print_list (Node *head)
 
void print_num (Node *head)
 

Function Documentation

◆ create_num_list()

Node * create_num_list ( )

Definition at line 127 of file largenum_incomplete.cpp.

128{
129 // TASK 1a: declare a pointer pointing to the head of the link list
130
131
132
133 string str;
134 char * digits = NULL; // a dynamic array for storing an input number
135 int numDigits;
136 int val;
137
138 // get a number from the user
139 input_num( digits, numDigits);
140
141 // scan the digits in reverse, and create a list of nodes for
142 // the value of every 5 digits
143 str.clear();
144 for (int i = numDigits-1; i >=0; --i) {
145 str = digits[i] + str;
146 if (str.length()==5) {
147 val = atoi(str.c_str());
148
149 // TASK 1b: insert a value as a node to the head of the linked list
150
151
152
153
154 str.clear();
155 }
156 }
157 // the digits array is scanned and there are still digits
158 // stored in str that are not inserted into the list yet
159 if (!str.empty()) {
160 val = atoi(str.c_str());
161
162 // TASK 1c: insert a value as a node to the head of the linked list
163
164
165
166 }
167
168 // free the dynamic array
169 if (digits != NULL) {
170 delete [] digits;
171
172 }
173
174 // TASK 1d: return the pointer to the linked list
175
176
177}
void input_num(char *&digits, int &numDigits)

References input_num().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ delete_head()

void delete_head ( Node *& head)

Definition at line 52 of file largenum_incomplete.cpp.

53{
54 if (head != NULL) {
55 Node * p = head;
56 head = head->next;
57 delete p;
58 }
59}
Definition 3.cpp:6
Node * next
Definition ex4ex5.cpp:8

References Node::next.

Referenced by delete_list().

Here is the caller graph for this function:

◆ delete_list()

void delete_list ( Node *& head)

Definition at line 62 of file largenum_incomplete.cpp.

63{
64 while ( head != NULL )
65 {
66 delete_head(head);
67 }
68}
void delete_head(Node *&head)

References delete_head().

Here is the call graph for this function:

◆ grow_array()

void grow_array ( char *& array,
int & size )

Definition at line 73 of file largenum_incomplete.cpp.

74{
75 if (array == NULL)
76 return;
77
78 int newSize = size * 2;
79
80 // doubled the size of the array;
81 char * tmp = new char [ newSize ];
82 // copy original contents
83 for (int i = 0; i < size; ++i)
84 tmp[i] = array[i];
85
86 delete [] array;
87
88 array = tmp;
89 size = newSize;
90}

Referenced by input_num().

Here is the caller graph for this function:

◆ head_insert()

void head_insert ( Node *& head,
int v )

Definition at line 43 of file largenum_incomplete.cpp.

44{
45 Node * p = new Node;
46 p->value = v;
47 p->next = head;
48 head = p;
49}
int value
Definition 3.cpp:7

References Node::next, and Node::value.

◆ input_num()

void input_num ( char *& digits,
int & numDigits )

Definition at line 97 of file largenum_incomplete.cpp.

98{
99 int arraysize = 32;
100 digits = new char [arraysize];
101 char c;
102 int numRead = 0;
103
104 // read each digit as a character until a white space is hit
105 c = cin.get();
106 while (!isspace(c))
107 {
108 if (numRead >= arraysize)
109 grow_array( digits, arraysize );
110
111 digits[numRead] = c;
112 numRead++;
113
114 c = cin.get();
115 }
116
117 numDigits = numRead;
118
119}
void grow_array(char *&array, int &size)

References grow_array().

Referenced by create_num_list().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ larger()

bool larger ( Node * n1,
Node * n2 )

Definition at line 199 of file largenum_incomplete.cpp.

200{
201 int len1 = list_length(n1);
202 int len2 = list_length(n2);
203
204 // TASK 4a: handle the case
205 // when the list lengths are different
206
207
208
209
210 // the two lists are of equal length
211
212 Node * curr1 = n1, * curr2 = n2;
213
214 while (curr1 != NULL) {
215 if (curr1->value > curr2->value)
216 return true;
217 else if (curr1->value < curr2->value)
218 return false;
219
220 // TASK 4b: advance curr1, curr2
221 // to point to the next nodes
222
223
224
225
226 }
227
228 return false;
229}
int list_length(Node *head)

References list_length(), and Node::value.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_length()

int list_length ( Node * head)

Definition at line 182 of file largenum_incomplete.cpp.

183{
184 // TASK 3: Modify this print function to one that
185 // count the number of nodes in a linked list
186
187 Node * current = head;
188 while (current != NULL)
189 {
190 // process the current node, e.g., print the content
191 cout << current->value << " -> ";
192 current = current->next;
193 }
194
195 cout << "NULL\n";
196}

References Node::next, and Node::value.

Referenced by larger().

Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 233 of file largenum_incomplete.cpp.

234{
235 Node * n1, * n2;
236
237 cout << "expr> ";
238 n1 = create_num_list();
239 cin.get(); // skip the '>' sign
240 cin.get(); // the space after the '>' sign
241 n2 = create_num_list();
242
243 // TASK 2: call print_list() on n1 and n2 for checking
244
245
246
247
248 if (larger(n1, n2)) {
249 cout << "Yes, ";
250 print_num(n1);
251 cout << " is larger." << endl;
252 }
253 else {
254 cout << "No, ";
255 print_num(n1);
256 cout << " is not larger." << endl;
257 }
258
259 // TASK 5: free the linked lists
260
261
262
263
264 return 0;
265}
Node * create_num_list()
void print_num(Node *head)
bool larger(Node *n1, Node *n2)

References create_num_list(), larger(), and print_num().

Here is the call graph for this function:

◆ print_list()

void print_list ( Node * head)

Definition at line 16 of file largenum_incomplete.cpp.

17{
18 Node * current = head;
19 while (current != NULL)
20 {
21 // process the current node, e.g., print the content
22 cout << current->value << " -> ";
23 current = current->next;
24 }
25 cout << "NULL\n";
26}

References Node::next, and Node::value.

◆ print_num()

void print_num ( Node * head)

Definition at line 29 of file largenum_incomplete.cpp.

30{
31 Node * current = head;
32 while (current != NULL)
33 {
34 if (current == head)
35 cout << current->value;
36 else
37 cout << setw(5) << setfill('0') << current->value;
38 current = current->next;
39 }
40}

References Node::next, and Node::value.

Referenced by main().

Here is the caller graph for this function: