COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
largenum.cpp File Reference
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>
Include dependency graph for largenum.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.cpp.

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

References head_insert(), and 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.cpp.

53{
54 if (head != NULL) {
55 Node * p = head;
56 head = head->next;
57 delete p;
58 }
59}
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.cpp.

63{
64 while ( head != NULL )
65 {
66 delete_head(head);
67 }
68}
void delete_head(Node *&head)
Definition largenum.cpp:52

References delete_head().

Referenced by main().

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

◆ grow_array()

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

Definition at line 73 of file largenum.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.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.

Referenced by create_num_list().

Here is the caller graph for this function:

◆ input_num()

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

Definition at line 97 of file largenum.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)
Definition largenum.cpp:73

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 194 of file largenum.cpp.

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

References list_length(), Node::next, 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 175 of file largenum.cpp.

176{
177 // TASK 3: Modify this print function to one that
178 // count the number of nodes in a linked list
179
180 int num = 0;
181
182 Node * current = head;
183 while (current != NULL)
184 {
185 // process the current node, e.g., print the content
186 ++num;
187 current = current->next;
188 }
189
190 return num;
191}

References Node::next.

Referenced by larger().

Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 226 of file largenum.cpp.

227{
228 Node * n1, * n2;
229
230 cout << "expr> ";
231 n1 = create_num_list();
232 cin.get(); // skip the '>' sign
233 cin.get(); // the space after the '>' sign
234 n2 = create_num_list();
235
236 // TASK 2: call print_list() on n1 and n2 for checking
237 print_list(n1);
238 print_list(n2);
239
240 if (larger(n1, n2)) {
241 cout << "Yes, ";
242 print_num(n1);
243 cout << " is larger." << endl;
244 }
245 else {
246 cout << "No, ";
247 print_num(n1);
248 cout << " is not larger." << endl;
249 }
250
251 // TASK 5: free the linked lists
252 delete_list(n1);
253 delete_list(n2);
254
255 return 0;
256}
Node * create_num_list()
Definition largenum.cpp:127
void delete_list(Node *&head)
Definition largenum.cpp:62
void print_num(Node *head)
Definition largenum.cpp:29
bool larger(Node *n1, Node *n2)
Definition largenum.cpp:194
void print_list(Node *head)
Definition largenum.cpp:16

References create_num_list(), delete_list(), larger(), print_list(), 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.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.

Referenced by main().

Here is the caller graph for this function:

◆ print_num()

void print_num ( Node * head)

Definition at line 29 of file largenum.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: