-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2.js
More file actions
195 lines (158 loc) · 4.95 KB
/
Copy pathTask2.js
File metadata and controls
195 lines (158 loc) · 4.95 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
// Algorithms Coursework 1
// Task 2
// By Luke Pring (A00012218)
// University of Roehampton London
// Represents a single character node with its position and name part.
class Node {
constructor(position, letter, part) {
this.position = position;
this.letter = letter;
this.part = part;
this.next = null;
}
}
// A singly linked list to store the characters of a name.
class LinkedList {
constructor() {
this.head = null;
}
// Counts and returns the total number of nodes in the list.
length() {
let count = 0;
let current = this.head;
while (current) {
count++;
current = current.next;
}
return count;
}
// Returns the node at the given zero-based index, or null if invalid.
Get_element(pos) {
let current = this.head;
let index = 0;
while (current && index < pos) {
current = current.next;
index++;
}
if (!current || pos < 0) {
console.error("Linked List Error: Invalid position.")
return null;
}
return current;
}
// Creates a new node with the given letter and appends it to the end of the list.
Add_element(letter, part) {
const newNode = new Node(this.length() + 1, letter.toLowerCase(), part);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
// Splits a word into characters and adds each as a node to the list.
Add_word(word, part) {
const wordArray = word.split("");
for (let i = 0; i < wordArray.length; i++) {
this.Add_element(wordArray[i], part);
}
}
// Searches for all occurrences of a letter and returns their positions.
Search_element(letter) {
let current = this.head;
let results = [];
letter = letter.toLowerCase();
while (current) {
if (current.letter === letter) {
results.push(current.position);
}
current = current.next;
}
return results.length > 0 ? results : null;
}
// Prints the entire linked list structure as a formatted string.
Print_list() {
let current = this.head;
let result = "[";
while (current) {
result += "(";
result += current.position;
result += ", ";
result += current.letter;
result += ", ";
result += current.part;
result += ")";
current = current.next;
if (current) {
result += ", ";
}
}
result += "]";
console.log(result);
}
// Reconstructs and prints the full name with proper capitalization and spacing.
Print_full_name() {
let f = "", m = "", l = "";
let current = this.head;
while (current) {
if (current.part === "f") f += current.letter;
else if (current.part === "m") m += current.letter;
else if (current.part === "l") l += current.letter;
current = current.next;
}
const capitalize = (str) => str.length > 0 ? str.charAt(0).toUpperCase() + str.slice(1) : "";
const fullName = [f, m, l]
.map(capitalize)
.filter(part => part.length > 0)
.join(" ");
console.log(fullName);
}
}
const Full_Name = new LinkedList();
// Main execution function to collect user input and demonstrate list operations.
async function main() {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const questions = [
"Please enter your first name: ",
"Please enter your middle name: ",
"Please enter your last name: "
];
function collectNames() {
return new Promise((resolve) => {
const answers = [];
const ask = (i) => {
if (i >= questions.length) {
rl.close();
resolve(answers);
return;
}
rl.question(questions[i], (answer) => {
answers.push(answer);
ask(i + 1);
});
};
ask(0);
});
}
const answers = await collectNames();
console.log("Your answers:", answers);
Full_Name.Add_word(answers[0], "f");
Full_Name.Add_word(answers[1], "m");
Full_Name.Add_word(answers[2], "l");
Full_Name.Print_list();
Full_Name.Print_full_name();
Full_Name.Add_element("a", "l");
Full_Name.Print_list();
Full_Name.Print_full_name();
console.log(Full_Name.Search_element("a"));
Full_Name.Print_list();
Full_Name.Print_full_name();
}
main();