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

Go to the source code of this file.

Classes

struct  Node
 

Functions

void delete_head (Node *&head)
 
void delete_list (Node *&head)
 
void delete_node (Node *after)
 
Nodefind (Node *head, int num)
 
Nodefind_prev (Node *head, int num)
 
Nodeget_item (Node *head, int k)
 
int get_option ()
 
void head_insert (Node *&head, int num)
 
void insert (Node *after, int num)
 
void insert_sorted (Node *&head, int num)
 
int main ()
 
void option_delete (Node *&head)
 
void option_get (Node *head)
 
void option_insert (Node *&head)
 
void print_list (Node *head)
 
void reverse (Node *&head)
 

Function Documentation

◆ delete_head()

void delete_head ( Node *& head)

Definition at line 77 of file ex4ex5.cpp.

78{
79 if (head != NULL)
80 {
81 Node * p = head;
82 head = head->next;
83 delete p;
84 }
85}
Definition 3.cpp:6
Node * next
Definition ex4ex5.cpp:8

References Node::next.

Referenced by delete_list(), and option_delete().

Here is the caller graph for this function:

◆ delete_list()

void delete_list ( Node *& head)

Definition at line 147 of file ex4ex5.cpp.

148{
149 cout << "deleting list... " << endl;
150 while ( head != NULL )
151 {
152 delete_head(head);
153 }
154}
void delete_head(Node *&head)
Definition ex4ex5.cpp:77

References delete_head().

Referenced by main().

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

◆ delete_node()

void delete_node ( Node * after)

Definition at line 89 of file ex4ex5.cpp.

90{
91 Node * p = after->next;
92 after->next = p->next;
93 delete p;
94}

References Node::next.

Referenced by option_delete().

Here is the caller graph for this function:

◆ find()

Node * find ( Node * head,
int num )

Definition at line 62 of file ex4ex5.cpp.

63{
64 Node * current = head;
65
66 while (current != NULL)
67 {
68 if (current->info == num)
69 return current;
70 else
71 current = current->next;
72 }
73
74 return NULL;
75}
int info
Definition ex4ex5.cpp:7

References Node::info, and Node::next.

Referenced by main(), option_delete(), and search_phonebook().

Here is the caller graph for this function:

◆ find_prev()

Node * find_prev ( Node * head,
int num )

Definition at line 43 of file ex4ex5.cpp.

44{
45 if (head == NULL || head->info >= num )
46 return NULL;
47
48 // at least one node in the list now
49 Node * current = head;
50
51 while (current->next != NULL)
52 {
53 if (current->next->info >= num)
54 return current;
55 else
56 current = current->next;
57 }
58
59 return current;
60}

References Node::info, and Node::next.

Referenced by insert_sorted(), and option_delete().

Here is the caller graph for this function:

◆ get_item()

Node * get_item ( Node * head,
int k )

Definition at line 175 of file ex4ex5.cpp.

176{
177 Node * current = head;
178 int idx = 0;
179 while (current != NULL) {
180 if (idx == k)
181 return current;
182 idx++;
183 current = current->next;
184 }
185 return NULL;
186}

References Node::next.

Referenced by option_get().

Here is the caller graph for this function:

◆ get_option()

int get_option ( )

Definition at line 96 of file ex4ex5.cpp.

97{
98 int c;
99 cout << endl << "enter option (1: insert; 2: delete; 3: reverse; 4: get; 0: quit) ";
100 cin >> c;
101 return c;
102}

Referenced by main().

Here is the caller graph for this function:

◆ head_insert()

void head_insert ( Node *& head,
int num )

Definition at line 23 of file ex4ex5.cpp.

24{
25 Node * p = new Node;
26 p->info = num;
27 p->next = head;
28 head = p;
29}

References Node::info, and Node::next.

Referenced by insert_sorted().

Here is the caller graph for this function:

◆ insert()

void insert ( Node * after,
int num )

Definition at line 33 of file ex4ex5.cpp.

34{
35 Node * p = new Node;
36 p->info = num;
37 p->next= after->next;
38 after->next = p;
39}

References Node::info, and Node::next.

Referenced by insert_sorted().

Here is the caller graph for this function:

◆ insert_sorted()

void insert_sorted ( Node *& head,
int num )

Definition at line 105 of file ex4ex5.cpp.

106{
107 Node * after_this = find_prev(head, num);
108 if (after_this == NULL)
109 head_insert(head, num);
110 else
111 insert(after_this, num);
112}
Node * find_prev(Node *head, int num)
Definition ex4ex5.cpp:43
void head_insert(Node *&head, int num)
Definition ex4ex5.cpp:23
void insert(Node *after, int num)
Definition ex4ex5.cpp:33

References find_prev(), head_insert(), and insert().

Referenced by main(), and option_insert().

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

◆ main()

int main ( void )

Definition at line 203 of file ex4ex5.cpp.

204{
205 Node * head = NULL, * after_this;
206 int num = 0;
207
208 // build sorted linked list
209 cout << "input integers (-999 to end): ";
210 cin >> num;
211 while ( num != -999 )
212 {
213 insert_sorted( head, num );
214 cin >> num;
215 }
216 print_list(head);
217
218 int c;
219 while (c = get_option())
220 {
221 switch (c)
222 {
223 case 1:
224 // insert a node
225 option_insert(head);
226 break;
227 case 2:
228 // delete a node
229 option_delete(head);
230 break;
231 case 3:
232 // reverse a list
233 reverse(head);
234 break;
235 case 4:
236 // get an item
237 option_get(head);
238 }
239 print_list(head);
240 }
241
242 delete_list(head);
243
244 return 0;
245}
void insert_sorted(Node *&head, int num)
Definition ex4ex5.cpp:105
void delete_list(Node *&head)
Definition ex4ex5.cpp:147
void option_insert(Node *&head)
Definition ex4ex5.cpp:115
void option_get(Node *head)
Definition ex4ex5.cpp:188
void option_delete(Node *&head)
Definition ex4ex5.cpp:124
int get_option()
Definition ex4ex5.cpp:96
void reverse(Node *&head)
Definition ex4ex5.cpp:156
void print_list(Node *head)
Definition ex4ex5.cpp:11

References delete_list(), get_option(), insert_sorted(), option_delete(), option_get(), option_insert(), print_list(), and reverse().

Here is the call graph for this function:

◆ option_delete()

void option_delete ( Node *& head)

Definition at line 124 of file ex4ex5.cpp.

125{
126 int num;
127 cout << endl << "number to delete: ";
128 cin >> num;
129
130 Node * p = find(head, num);
131
132 if (p == NULL)
133 {
134 cout << "item not found!" << endl;
135 }
136 else
137 {
138 // find the previous node, so that we can delete
139 p = find_prev(head, num);
140 if (p == NULL)
141 delete_head(head);
142 else
143 delete_node(p);
144 }
145}
void delete_node(Node *after)
Definition ex4ex5.cpp:89
Node * find(Node *head, int num)
Definition ex4ex5.cpp:62

References delete_head(), delete_node(), find(), and find_prev().

Referenced by main().

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

◆ option_get()

void option_get ( Node * head)

Definition at line 188 of file ex4ex5.cpp.

189{
190 int k;
191 cout << endl << "enter 0-based position for an item: ";
192 cin >> k;
193
194 Node * p = get_item(head, k);
195 if (p != NULL)
196 cout << "Item at position " << k << " is " << p->info << endl;
197 else
198 cout << "Item does not exist." << endl;
199
200 cout << endl;
201}
Node * get_item(Node *head, int k)
Definition ex4ex5.cpp:175

References get_item(), and Node::info.

Referenced by main().

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

◆ option_insert()

void option_insert ( Node *& head)

Definition at line 115 of file ex4ex5.cpp.

116{
117 int num;
118 cout << endl << "number to insert: ";
119 cin >> num;
120
121 insert_sorted( head, num );
122}

References insert_sorted().

Referenced by main().

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

◆ print_list()

void print_list ( Node * head)

Definition at line 11 of file ex4ex5.cpp.

12{
13 Node * current = head;
14 while (current != NULL)
15 {
16 // process the current node, e.g., print the content
17 cout << current->info << " -> ";
18 current = current->next;
19 }
20 cout << "NULL\n";
21}

References Node::info, and Node::next.

Referenced by main().

Here is the caller graph for this function:

◆ reverse()

void reverse ( Node *& head)

Definition at line 156 of file ex4ex5.cpp.

157{
158 Node * curr = head;
159 Node * prev = NULL;
160 Node * next;
161
162 while ( curr != NULL )
163 {
164 next = curr->next;
165 curr->next = prev;
166 prev = curr;
167 curr = next;
168 }
169
170 head = prev;
171}

References Node::next.

Referenced by main().

Here is the caller graph for this function: