COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
ex4ex5.cpp
Go to the documentation of this file.
1#include <iostream>
2
3using namespace std;
4
5struct Node
6{
7 int info;
9};
10
11void print_list(Node * head)
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}
22
23void head_insert(Node * & head, int num)
24{
25 Node * p = new Node;
26 p->info = num;
27 p->next = head;
28 head = p;
29}
30
31// assume that after points to a node
32// i.e., after not equals null
33void insert( Node * after, int num )
34{
35 Node * p = new Node;
36 p->info = num;
37 p->next= after->next;
38 after->next = p;
39}
40
41// return the node which is the last one in
42// the list that is smaller than num
43Node * find_prev( Node * head, int num )
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}
61
62Node * find( Node * head, int num )
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}
76
77void delete_head( Node * & head)
78{
79 if (head != NULL)
80 {
81 Node * p = head;
82 head = head->next;
83 delete p;
84 }
85}
86
87// assume that after points to a node
88// i.e., after not equals null
89void delete_node( Node * after)
90{
91 Node * p = after->next;
92 after->next = p->next;
93 delete p;
94}
95
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}
103
104
105void insert_sorted( Node * & head, int num)
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}
113
114
115void option_insert(Node * & head)
116{
117 int num;
118 cout << endl << "number to insert: ";
119 cin >> num;
120
121 insert_sorted( head, num );
122}
123
124void option_delete(Node * & head)
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}
146
147void delete_list(Node * & head)
148{
149 cout << "deleting list... " << endl;
150 while ( head != NULL )
151 {
152 delete_head(head);
153 }
154}
155
156void reverse( Node * & head )
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}
172
173// return the k-th item
174// return NULL if item not exists
175Node * get_item(Node * head, int k)
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}
187
188void option_get(Node * head)
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}
202
203int main()
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
Node * find_prev(Node *head, int num)
Definition ex4ex5.cpp:43
void delete_head(Node *&head)
Definition ex4ex5.cpp:77
void head_insert(Node *&head, int num)
Definition ex4ex5.cpp:23
void delete_node(Node *after)
Definition ex4ex5.cpp:89
Node * get_item(Node *head, int k)
Definition ex4ex5.cpp:175
void option_get(Node *head)
Definition ex4ex5.cpp:188
void insert(Node *after, int num)
Definition ex4ex5.cpp:33
void option_delete(Node *&head)
Definition ex4ex5.cpp:124
int get_option()
Definition ex4ex5.cpp:96
void reverse(Node *&head)
Definition ex4ex5.cpp:156
int main()
Definition ex4ex5.cpp:203
void print_list(Node *head)
Definition ex4ex5.cpp:11
Node * find(Node *head, int num)
Definition ex4ex5.cpp:62
Definition 3.cpp:6
int info
Definition ex4ex5.cpp:7
Node * next
Definition ex4ex5.cpp:8