-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: added second implementation with based on build-in arrays list
- Loading branch information
Showing
1 changed file
with
66 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,190 +1,88 @@ | ||
class Node { | ||
constructor(data) { | ||
this.data = data; | ||
this.next = null; | ||
} | ||
} | ||
class List { | ||
#list = []; | ||
|
||
class CircularList { | ||
constructor() { | ||
this.head = null; | ||
this.tail = null; | ||
this.size = 0; | ||
} | ||
length() { | ||
return this.#list.length; | ||
} | ||
|
||
length() { | ||
return this.size; | ||
} | ||
append(data) { | ||
const isString = typeof data; | ||
|
||
append(data) { | ||
const isString = typeof data === 'string'; | ||
if (isString && data.length === 1) { | ||
const node = new Node(data); | ||
if (!this.head) this.head = node; | ||
else this.tail.next = node; | ||
node.next = this.head; | ||
this.tail = node; | ||
this.size++; | ||
} else { | ||
throw new Error ('Error. Wrong input data type, expected type char.'); | ||
} | ||
} | ||
if (data && isString && data.length === 1) { | ||
this.#list.push(data); | ||
} | ||
} | ||
|
||
insert(data, index) { | ||
const isString = typeof data === 'string'; | ||
if (isString && data.length === 1) { | ||
if (index < 0 || index > this.size) throw new Error ('Error. Incorrect index.');; | ||
const node = new Node(data); | ||
if (index === 0) { | ||
node.next = this.head; | ||
this.head = node; | ||
this.tail = node; | ||
} else if (index === this.size) { | ||
this.tail.next = node; | ||
this.tail = node; | ||
node.next = this.head; | ||
} else { | ||
let current = this.head; | ||
let i = 0; | ||
while (i < index - 1) { | ||
current = current.next; | ||
i++; | ||
} | ||
node.next = current.next; | ||
current.next = node; | ||
} | ||
this.size++; | ||
} else { | ||
throw new Error ('Error. Wrong input data type, expected type char.'); | ||
} | ||
} | ||
insert(data, index) { | ||
const isString = typeof data; | ||
|
||
delete(index) { | ||
if (index < 0 || index >= this.size) throw new Error ('Error. Index out of range.') ; | ||
let deletedItem = null; | ||
if (this.size === 1) { | ||
deletedItem = this.head.data; | ||
this.head = null; | ||
this.tail = null; | ||
} else if (index === 0) { | ||
deletedItem = this.head.data; | ||
this.head = this.head.next; | ||
this.tail.next = this.head; | ||
} else { | ||
let current = this.head; | ||
let i = 0; | ||
while (i < index - 1) { | ||
current = current.next; | ||
i++; | ||
} | ||
deletedItem = current.next.data; | ||
current.next = current.next.next; | ||
if (index === this.size - 1) this.tail = current; | ||
} | ||
this.size--; | ||
return deletedItem; | ||
if (data && isString && data.length === 1) { | ||
if (index < 0 || index > this.#list.length) { | ||
throw new Error("Error. Incorrect index."); | ||
} | ||
this.#list.splice(index, 0, data); | ||
} else { | ||
throw new Error("Error. Wrong input data type, expected type char."); | ||
} | ||
} | ||
|
||
deleteAll(data) { | ||
let current = this.head; | ||
let prev = this.tail; | ||
let i = 0; | ||
while (i < this.size) { | ||
if (current.data === data) { | ||
if (i === 0) { | ||
this.head = this.head.next; | ||
this.tail.next = this.head; | ||
prev = this.tail; | ||
} else { | ||
prev.next = current.next; | ||
if (i === this.size - 1) this.tail = prev; | ||
} | ||
this.size--; | ||
i--; | ||
} else { | ||
prev = current; | ||
} | ||
current = current.next; | ||
i++; | ||
} | ||
delete(index) { | ||
if (index < 0 || index >= this.#list.length) { | ||
throw new Error("Error. Index out of range."); | ||
} | ||
return this.#list.splice(index, 1)[0]; | ||
} | ||
|
||
get(index) { | ||
if (index < 0 || index >= this.size) throw new Error('Error. Index out of range.'); | ||
let current = this.head; | ||
let i = 0; | ||
while (i < index) { | ||
current = current.next; | ||
i++; | ||
} | ||
return current.data; | ||
} | ||
deleteAll(data) { | ||
const filteredList = this.#list.filter((element) => element !== data); | ||
this.#list = filteredList; | ||
} | ||
|
||
clone() { | ||
const newList = new CircularList(); | ||
let current = this.head; | ||
let i = 0; | ||
while (i < this.size) { | ||
newList.append(current.data); | ||
current = current.next; | ||
i++; | ||
} | ||
return newList; | ||
get(index) { | ||
if (index < 0 || index >= this.#list.length) { | ||
throw new Error("Error. Index out of range."); | ||
} | ||
return this.#list[index]; | ||
} | ||
|
||
reverse() { | ||
let current = this.head; | ||
let prev = this.tail; | ||
let i = 0; | ||
while (i < this.size) { | ||
const next = current.next; | ||
current.next = prev; | ||
prev = current; | ||
current = next; | ||
i++; | ||
} | ||
this.head = prev; | ||
if (this.size > 0) this.tail = this.head.next; | ||
clone() { | ||
const newList = new List(); | ||
let i = 0; | ||
while (i < this.#list.length) { | ||
newList.append(this.#list[i]); | ||
i++; | ||
} | ||
return newList; | ||
} | ||
|
||
findFirst(data) { | ||
let current = this.head; | ||
let i = 0; | ||
while (i < this.size) { | ||
if (current.data === data) return i; | ||
current = current.next; | ||
i++; | ||
} | ||
return -1; | ||
} | ||
reverse() { | ||
this.#list.reverse(); | ||
} | ||
|
||
findLast(data) { | ||
let current = this.head; | ||
let i = 0; | ||
let lastIndex = -1; | ||
while (i < this.size) { | ||
if (current.data === data) lastIndex = i; | ||
current = current.next; | ||
i++; | ||
} | ||
return lastIndex; | ||
findFirst(data) { | ||
for (let i = 0; i < this.#list.length; i++) { | ||
if (this.#list[i] === data) return i; | ||
} | ||
return -1; | ||
} | ||
|
||
clear() { | ||
this.head = null; | ||
this.tail = null; | ||
this.size = 0; | ||
findLast(data) { | ||
for (let i = this.#list.length - 1; i >= 0; i--) { | ||
if (this.#list[i] === data) return i; | ||
} | ||
return -1; | ||
} | ||
|
||
clear() { | ||
this.#list = []; | ||
} | ||
|
||
extend(list) { | ||
let current = list.head; | ||
let i = 0; | ||
while (i < list.size) { | ||
this.append(current.data); | ||
current = current.next; | ||
i++; | ||
} | ||
extend(list) { | ||
for (let i = 0; i < list.length(); i++) { | ||
this.#list.push(list.get(i)); | ||
} | ||
return this.#list; | ||
} | ||
} | ||
|
||
module.exports = CircularList; | ||
module.exports = List; |