-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-lists.js
More file actions
166 lines (149 loc) · 5.3 KB
/
linked-lists.js
File metadata and controls
166 lines (149 loc) · 5.3 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
class Node {
constructor(element) {
this.element = element;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
//insertion of element
insertNode(element) {
const newNode = new Node(element); //creating a node
//checking if the list is empty, if empty - make the initial element the head
if (!this.head) {
this.head = newNode;
displayText(`You inserted element ${element} into the linked list. Click display to see.`);
} else { //adding the next elements to the list
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode; //inserting the new element to the current last node
displayText(`You inserted element ${element} into the linked list. Click display to see.`);
}
}
//deletion of element
deleteNode(element) {
//checking if the list is empty, return if it's empty and checking if it's the head
if (!this.head || this.head.element === element) {
this.head = this.head ? this.head.next : null;
displayText(`You deleted element ${element} from the linked list. Click display to see.`);
return;
}
// traversing the list to find the specific element if it's not in the head
let current = this.head;
while (current.next) {
if (current.next.element === element) {
current.next = current.next.next;
displayText(`You deleted element ${element} from the linked list. Click display to see.`);
return;
}
current = current.next;
}
}
//reversing the order of the list
reverse() {
let prev = null;
let current = this.head;
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
displayText(`You just reversed the order of the linked list. Click display to see.`);
}
//removing duplicates from the linked list
removeDuplicates() {
let current = this.head;
let prev = null;
let noDuplicates = {};
//checking if the element has duplicates
while (current) {
if (noDuplicates[current.element]) { //if it has duplicates update the prev node/pointer
prev.next = current.next;
} else { //if it doesn't have duplicates, it will be return as true
noDuplicates[current.element] = true;
prev = current;
}
current = current.next;
}
}
// Function to display the linked list
display() {
let current = this.head;
let listContainer = document.getElementById('list-container');
listContainer.innerHTML = '';
let headNode = '<div class="node">head</div>'; //adding the head
listContainer.innerHTML += headNode;
let headArrow = '<div class="arrow">→</div>'; //adding the arrows/poitners
listContainer.innerHTML += headArrow;
//
while (current) {
let nodeElement = `<div class="node">${current.element}</div>`; //inserting the elements
listContainer.innerHTML += nodeElement;
let arrowElement = '<div class="arrow">→</div>'; //adding arrow after inserting an element
listContainer.innerHTML += arrowElement;
current = current.next;
}
let nullNode = '<div class="node">null</div>'; //adding the null
listContainer.innerHTML += nullNode;
}
}
// create an instance of a linked list
const linkedList = new LinkedList();
// creating an empty linked list
function createEmptyList() {
clearMessage();
let listContainer = document.getElementById('list-container');
listContainer.innerHTML = '';
linkedList.head = null;
}
// inserting an element
function insertElement() {
const insertElementInput = document.getElementById('insertElementInput');
const element = insertElementInput.value.trim();
if (element !== '') {
linkedList.insertNode(element);
}
insertElementInput.value = '';
}
// deleting an element
function deleteElement() {
const deleteElementInput = document.getElementById('deleteElementInput');
const element = deleteElementInput.value.trim();
if (element !== '') {
linkedList.deleteNode(element);
}
deleteElementInput.value = '';
}
// function to be called when reversing an element
function reverseList() {
linkedList.reverse();
}
//function to be called to remove duplicates
function removeDuplicates() {
linkedList.removeDuplicates();
}
//function to be called to display the linked list
function displayLinkedList() {
linkedList.display();
}
//function for displaying texts
function displayText(message) {
clearMessage();
const msgText = document.createElement('p');
msgText.textContent = message;
document.getElementById('response').appendChild(msgText);
}
//function for clearing message
function clearMessage() {
document.getElementById('response').innerHTML = '';
}
//function to return to the main menu
function returnMenu() {
window.location.href="index.html";
}