COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
3.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <string>
3#include <sstream>
4using namespace std;
5
6struct Node {
7 int value;
9};
10
11void build_sorted_linkedlist(Node * &headptr1, int input) {
12 // insert a new item 'input' into an existing sorted linked list
13 // with head pointer headptr1
14 Node * previous = headptr1;
15 if (previous != nullptr) {
16 if (previous->value < input) {
17 while (previous->nextptr != nullptr) {
18 if (previous->nextptr->value < input) {
19 previous = previous->nextptr;
20 }
21 else break;
22 }
23 Node * next = previous->nextptr;
24 Node * current = new Node;
25 previous->nextptr = current;
26 current->value = input;
27 current->nextptr = next;
28 }
29 else {
30 // the input is smaller than the first item in the linked list
31 headptr1 = new Node;
32 headptr1->value = input;
33 headptr1->nextptr = previous;
34 // the input becomes the new headptr
35 }
36 }
37 else {
38 // the first linked list is blank, so we create one
39 // since we need to insert something
40 headptr1 = new Node;
41 headptr1->value = input;
42 headptr1->nextptr = nullptr;
43 }
44}
45
46void linkedlist_int(Node * &headptr1) {
47 // we expect headptr1 to be nullptr
48 // otherwise, we will have memory leak!
49 string line;
50 getline(cin, line);
51 istringstream line_in(line);
52 // the first input
53 int x;
54 if (line_in >> x) {
55 headptr1 = new Node;
56 headptr1->value = x;
57 headptr1->nextptr = nullptr;
58 // subsequent inputs
59 int input;
60 while (line_in >> input) {
61 build_sorted_linkedlist(headptr1, input);
62 }
63 }
64 return;
65}
66
67void print_linked_list(Node * headptr1) {
68 // print all stuffs
69 Node * current_node = headptr1;
70 while (current_node != nullptr) {
71 cout << current_node->value;
72 current_node = current_node->nextptr;
73 if (current_node == nullptr) {
74 break;
75 }
76 cout << " ";
77 }
78 cout << endl;
79}
80
82 // headptr1 linked list must be sorted ascendingly
83 Node * current = headptr1;
84 while (current != nullptr && current->nextptr != nullptr) {
85 if (current->value == current->nextptr->value) {
86 // 1 2 x x 6 7
87 // remove the second occurrence of x as duplicate
88 Node * next = current->nextptr;
89 current->nextptr = next->nextptr;
90 delete next;
91 }
92 else {
93 current = current->nextptr;
94 }
95 }
96 // since the list is sorted, the negative values are
97 // located at the beginning of the list
98 while (headptr1 != nullptr) {
99 if (headptr1->value < 0) {
100 Node * next = headptr1;
101 headptr1 = headptr1->nextptr; // set the new head pointer
102 delete next; // free up memory
103 }
104 else {
105 break;
106 }
107 }
108}
109
110Node * last_node_greater_than(Node * currentptr, int input) {
111 if (currentptr != nullptr) {
112 if (currentptr->nextptr != nullptr) {
113 if (currentptr->nextptr->value > input) {
114 return currentptr;
115 }
116 else {
117 Node * result = last_node_greater_than(currentptr->nextptr, input);
118 if (result == nullptr) {
119 return currentptr;
120 }
121 else {
122 return result;
123 }
124 }
125 }
126 else {
127 if (currentptr->value <= input) {
128 return currentptr;
129 }
130 else {
131 return nullptr; // currentptr->value is greater than input
132 // so last_node_greater_than should be previous node
133 }
134 }
135 }
136 else {
137 return nullptr; // uninitialized linked list
138 }
139}
140
141void recursive_remove_dups(Node * headptr1) {
142 // headptr1 linked list must be sorted ascendingly
143 if (headptr1 == nullptr) {
144 return;
145 }
146 if (headptr1->nextptr != nullptr) {
147 if (headptr1->value == headptr1->nextptr->value) {
148 headptr1->nextptr = headptr1->nextptr->nextptr;
149 recursive_remove_dups(headptr1);
150 }
151 else {
153 }
154 }
155}
156
157int main() {
158 Node * node1head = nullptr;
159 Node * node2head = nullptr;
160 linkedlist_int(node1head);
161 print_linked_list(node1head);
162 linkedlist_int(node2head);
163 print_linked_list(node2head);
164 // merge sort
165 Node * current = node2head;
166 while (current != nullptr) {
167 build_sorted_linkedlist(node1head, current->value);
168 current = current->nextptr;
169 }
170 remove_dups_negative_nums(node1head);
171 //recursive_remove_dups(node1head);
172 print_linked_list(node1head);
173 return 0;
174}
void linkedlist_int(Node *&headptr1)
Definition 3.cpp:46
void remove_dups_negative_nums(Node *&headptr1)
Definition 3.cpp:81
void print_linked_list(Node *headptr1)
Definition 3.cpp:67
Node * last_node_greater_than(Node *currentptr, int input)
Definition 3.cpp:110
int main()
Definition 3.cpp:157
void build_sorted_linkedlist(Node *&headptr1, int input)
Definition 3.cpp:11
void recursive_remove_dups(Node *headptr1)
Definition 3.cpp:141
Definition 3.cpp:6
Node * nextptr
Definition 3.cpp:8
int value
Definition 3.cpp:7