-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.cpp
More file actions
208 lines (174 loc) · 5.1 KB
/
task2.cpp
File metadata and controls
208 lines (174 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
// Node structure for the linked list
struct Node {
int position;
char letter; // Stored in lowercase
char part; // f = first name, m = middle name, l = last name
Node* next;
};
// Class to store the full name as a linked list
class Full_Name {
private:
Node* head;
int size;
public:
// Constructor: start with an empty list
Full_Name() {
head = NULL;
size = 0;
}
// Add a single letter to the end of the list
void Add_element(char letter, char part) {
Node* n = new Node; // create a new node
n->letter = tolower(letter);
n->part = part;
n->next = NULL;
size++; // Increase size of the list
n->position = size;
// If list is empty new node becomes head
if (head == NULL) {
head = n;
}
else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = n; // Add new node to the end
}
}
// Add a whole word letter-by-letter
void Add_word(string word, char part) {
for (int i = 0; i < word.length(); i++) {
if (!isspace(word[i])) { // Ignore spaces
Add_element(word[i], part);
}
}
}
// Print the full linked list
void Print_list() {
Node* temp = head;
cout << "[";
while (temp != NULL) {
// Print each node as (position, 'letter', 'part')
cout << "(" << temp->position << ", '" << temp->letter << "', '" << temp->part << "')";
if (temp->next != NULL) cout << ", ";
temp = temp->next;
}
cout << "]\n";
}
// Reconstruct and print the full name properly
void Print_full_name() {
string f = "", m = "", l = "";
Node* temp = head;
// Sort letters back into first, middle, last
while (temp != NULL) {
if (temp->part == 'f') f += temp->letter;
else if (temp->part == 'm') m += temp->letter;
else if (temp->part == 'l') l += temp->letter;
temp = temp->next;
}
// Capitalise first letters
if (f.length() > 0) f[0] = toupper(f[0]);
if (m.length() > 0) m[0] = toupper(m[0]);
if (l.length() > 0) l[0] = toupper(l[0]);
// Print full name in normal format
cout << f;
if (m != "") cout << " " << m;
cout << " " << l << "\n";
}
// Search for a letter and print all positions
void Search_element(char letter) {
letter = tolower(letter);
Node* temp = head;
bool found = false;
// Search through list
while (temp != NULL) {
if (temp->letter == letter) {
cout << temp->position << " "; // Print positions
found = true;
}
temp = temp->next;
}
if (!found) cout << "No occurrences";
cout << "\n";
}
// Remove all nodes belonging to a chosen name part
void Remove_word(char part) {
// If the list is empty, nothing to do
if (head == NULL) {
return;
}
// Remove matching nodes at the head
while (head != NULL && head->part == part) {
Node* temp = head;
head = head->next;
delete temp;
size--;
}
// If list became empty after removing heads
if (head == NULL) {
return;
}
// Remove matching nodes in the rest of the list
Node* current = head;
while (current->next != NULL) {
if (current->next->part == part) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
size--;
} else {
current = current->next;
}
}
// Reassign positions 1 -> size
Node* temp = head;
int newPos = 1;
while (temp != NULL) {
temp->position = newPos;
newPos++;
temp = temp->next;
}
}
};
// Main program logic
int main() {
Full_Name fn;
string first, middle, last;
// Ask user for names
cout << "Enter first name: ";
cin >> first;
cout << "Enter middle name (or '-' if none): ";
cin >> middle;
cout << "Enter last name: ";
cin >> last;
// Add each name to the linked list
fn.Add_word(first, 'f');
if (middle != "-") fn.Add_word(middle, 'm');
fn.Add_word(last, 'l');
// Print initial state
cout << "\nInitial list:\n";
fn.Print_list();
fn.Print_full_name();
// Add an extra 'a' to the last name (as required)
fn.Add_element('a', 'l');
cout << "\nAfter adding 'a':\n";
fn.Print_list();
fn.Print_full_name();
// Search the list for letter 'a'
cout << "\nSearching for 'a':\n";
fn.Search_element('a');
// Final printing
cout << "\nFinal list:\n";
fn.Print_list();
fn.Print_full_name();
// Remove name example
cout << "\nAfter removing a word:\n";
fn.Remove_word('m');
fn.Print_full_name();
return 0;
}