COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
build_list_sorted.cpp
Go to the documentation of this file.
1#include <iostream>
2
3using namespace std;
4
5struct Node
6{
7 int info;
8 Node * next;
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 if (current->next->info >= num)
53 return current;
54 else
55 current = current->next;
56 }
57
58 return current;
59}
60
61Node * find( Node * head, int num )
62{
63 Node * current = head;
64
65 while (current != NULL) {
66 if (current->info == num)
67 return current;
68 else
69 current = current->next;
70 }
71
72 return NULL;
73}
74
75void delete_head( Node * & head)
76{
77 if (head != NULL) {
78 Node * p = head;
79 head = head->next;
80 delete p;
81 }
82}
83
84// assume that after points to a node
85// i.e., after not equals null
86void delete_node( Node * after)
87{
88 Node * p = after->next;
89 after->next = p->next;
90 delete p;
91}
92
94{
95 int c;
96 cout << endl << "enter option (1: insert; 2: delete; 0: quit) ";
97 cin >> c;
98 return c;
99}
100
101void option_insert(Node * & head)
102{
103 int num;
104 cout << endl << "number to insert: ";
105 cin >> 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
114void option_delete(Node * & head)
115{
116 int num;
117 cout << endl << "number to delete: ";
118 cin >> num;
119
120 Node * p = find(head, num);
121
122 if (p == NULL) {
123 cout << "item not found!" << endl;
124 }
125 else {
126 // find the previous node, so that we can delete
127 p = find_prev(head, num);
128 if (p == NULL)
129 delete_head(head);
130 else
131 delete_node(p);
132 }
133}
134
135void delete_list(Node * & head)
136{
137 while ( head != NULL )
138 {
139 delete_head(head);
140 print_list(head);
141 }
142}
143
144int main()
145{
146 Node * head = NULL, * after_this;
147 int num = 0;
148
149 // build sorted linked list
150 cout << "input integers (-999 to end): ";
151 cin >> num;
152 while ( num != -999 ) {
153 after_this = find_prev(head, num);
154
155 if (after_this == NULL)
156 head_insert(head, num);
157 else
158 insert(after_this, num);
159
160 print_list(head);
161
162 cin >> num;
163
164 }
165
166 int c;
167 while (c = get_option()) {
168 switch (c)
169 {
170 case 1:
171 // insert a node
172 option_insert(head);
173 break;
174 case 2:
175 // delete a node
176 option_delete(head);
177 break;
178 }
179 print_list(head);
180 }
181
182 delete_list(head);
183
184 return 0;
185}
void delete_list(Node *&head)
void option_insert(Node *&head)
Node * find_prev(Node *head, int num)
void delete_head(Node *&head)
void head_insert(Node *&head, int num)
void delete_node(Node *after)
void insert(Node *after, int num)
void option_delete(Node *&head)
int get_option()
int main()
void print_list(Node *head)
Node * find(Node *head, int num)
Definition 3.cpp:6
int info
Definition ex4ex5.cpp:7
Node * next
Definition ex4ex5.cpp:8