COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
build_list_sorted.cpp File Reference
#include <iostream>
Include dependency graph for build_list_sorted.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)
 
int get_option ()
 
void head_insert (Node *&head, int num)
 
void insert (Node *after, int num)
 
int main ()
 
void option_delete (Node *&head)
 
void option_insert (Node *&head)
 
void print_list (Node *head)
 

Function Documentation

◆ delete_head()

void delete_head ( Node *& head)

Definition at line 75 of file build_list_sorted.cpp.

76{
77 if (head != NULL) {
78 Node * p = head;
79 head = head->next;
80 delete p;
81 }
82}
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 135 of file build_list_sorted.cpp.

136{
137 while ( head != NULL )
138 {
139 delete_head(head);
140 print_list(head);
141 }
142}
void delete_head(Node *&head)
void print_list(Node *head)

References delete_head(), and print_list().

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 86 of file build_list_sorted.cpp.

87{
88 Node * p = after->next;
89 after->next = p->next;
90 delete p;
91}

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 61 of file build_list_sorted.cpp.

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}
int info
Definition ex4ex5.cpp:7

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

Referenced by option_delete().

Here is the caller graph for this function:

◆ find_prev()

Node * find_prev ( Node * head,
int num )

Definition at line 43 of file build_list_sorted.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 if (current->next->info >= num)
53 return current;
54 else
55 current = current->next;
56 }
57
58 return current;
59}

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

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

Here is the caller graph for this function:

◆ get_option()

int get_option ( )

Definition at line 93 of file build_list_sorted.cpp.

94{
95 int c;
96 cout << endl << "enter option (1: insert; 2: delete; 0: quit) ";
97 cin >> c;
98 return c;
99}

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 build_list_sorted.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 main(), and option_insert().

Here is the caller graph for this function:

◆ insert()

void insert ( Node * after,
int num )

Definition at line 33 of file build_list_sorted.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 main(), and option_insert().

Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 144 of file build_list_sorted.cpp.

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 head_insert(Node *&head, int num)
void insert(Node *after, int num)
void option_delete(Node *&head)
int get_option()

References delete_list(), find_prev(), get_option(), head_insert(), insert(), option_delete(), option_insert(), and print_list().

Here is the call graph for this function:

◆ option_delete()

void option_delete ( Node *& head)

Definition at line 114 of file build_list_sorted.cpp.

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}
void delete_node(Node *after)
Node * find(Node *head, int num)

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_insert()

void option_insert ( Node *& head)

Definition at line 101 of file build_list_sorted.cpp.

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}

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

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 build_list_sorted.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 delete_list(), and main().

Here is the caller graph for this function: