mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 06:20:42 -05:00
fix: swap head/tail
This commit is contained in:
parent
7009e7c2bb
commit
ba9619ae97
1 changed files with 32 additions and 28 deletions
|
@ -1,15 +1,15 @@
|
||||||
class LinkedList {
|
class LinkedList {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.size = 0;
|
this.size = 0;
|
||||||
this.head = null;
|
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
|
this.head = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
getHead() {
|
getHead() {
|
||||||
return this.head;
|
return this.tail;
|
||||||
}
|
}
|
||||||
getTail() {
|
getTail() {
|
||||||
return this.tail;
|
return this.head;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSize() {
|
getSize() {
|
||||||
|
@ -18,15 +18,15 @@ class LinkedList {
|
||||||
|
|
||||||
append(value) {
|
append(value) {
|
||||||
let newNode = new Node(value);
|
let newNode = new Node(value);
|
||||||
if (this.tail == null) {
|
|
||||||
this.tail = newNode;
|
|
||||||
} else {
|
|
||||||
const temp = this.tail;
|
|
||||||
this.tail = newNode;
|
|
||||||
this.tail.nextNode = temp;
|
|
||||||
}
|
|
||||||
if (this.head == null) {
|
if (this.head == null) {
|
||||||
this.head = newNode;
|
this.head = newNode;
|
||||||
|
} else {
|
||||||
|
const temp = this.head;
|
||||||
|
this.head = newNode;
|
||||||
|
this.head.nextNode = temp;
|
||||||
|
}
|
||||||
|
if (this.tail == null) {
|
||||||
|
this.tail = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.size += 1;
|
this.size += 1;
|
||||||
|
@ -34,15 +34,15 @@ class LinkedList {
|
||||||
|
|
||||||
prepend(value) {
|
prepend(value) {
|
||||||
let newNode = new Node(value);
|
let newNode = new Node(value);
|
||||||
if (this.head == null) {
|
if (this.tail == null) {
|
||||||
this.head = newNode;
|
this.tail = newNode;
|
||||||
if (this.tail == null) {
|
if (this.head == null) {
|
||||||
this.tail = newNode;
|
this.head = newNode;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const temp = this.head;
|
const temp = this.tail;
|
||||||
temp.nextNode = newNode;
|
temp.nextNode = newNode;
|
||||||
this.head = newNode;
|
this.tail = newNode;
|
||||||
}
|
}
|
||||||
this.size += 1;
|
this.size += 1;
|
||||||
}
|
}
|
||||||
|
@ -58,14 +58,14 @@ class LinkedList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodeAtIndex(index, 0, this.tail);
|
return nodeAtIndex(index, 0, this.head);
|
||||||
}
|
}
|
||||||
pop() {
|
pop() {
|
||||||
if (this.tail.nextNode == null) {
|
if (this.head.nextNode == null) {
|
||||||
this.tail = null;
|
|
||||||
this.head = null;
|
this.head = null;
|
||||||
|
this.tail = null;
|
||||||
} else {
|
} else {
|
||||||
this.tail = this.tail.nextNode;
|
this.head = this.head.nextNode;
|
||||||
}
|
}
|
||||||
this.size -= 1;
|
this.size -= 1;
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ class LinkedList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return searchNodes(this.tail, value);
|
return searchNodes(this.head, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
find(value) {
|
find(value) {
|
||||||
|
@ -96,13 +96,13 @@ class LinkedList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return searchNodes(this.tail, value, index);
|
return searchNodes(this.head, value, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
insertAt(value, index) {
|
insertAt(value, index) {
|
||||||
let previousNode = null;
|
let previousNode = null;
|
||||||
let newNode = new Node(value);
|
let newNode = new Node(value);
|
||||||
let currentNode = this.tail;
|
let currentNode = this.head;
|
||||||
let count = 0;
|
let count = 0;
|
||||||
|
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
|
@ -131,7 +131,7 @@ class LinkedList {
|
||||||
|
|
||||||
removeAt(index) {
|
removeAt(index) {
|
||||||
let previousNode = null;
|
let previousNode = null;
|
||||||
let currentNode = this.tail;
|
let currentNode = this.head;
|
||||||
let count = 0;
|
let count = 0;
|
||||||
|
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
|
@ -158,7 +158,7 @@ class LinkedList {
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
let nodes = [];
|
let nodes = [];
|
||||||
let current = this.tail;
|
let current = this.head;
|
||||||
|
|
||||||
while (current != null) {
|
while (current != null) {
|
||||||
nodes.push(current.value);
|
nodes.push(current.value);
|
||||||
|
@ -203,9 +203,13 @@ list.toString();
|
||||||
list.removeAt(6);
|
list.removeAt(6);
|
||||||
console.log("Removing element 6");
|
console.log("Removing element 6");
|
||||||
list.toString();
|
list.toString();
|
||||||
|
console.log('removing inserted elements')
|
||||||
list.removeAt(1);
|
list.removeAt(1);
|
||||||
list.removeAt(3);
|
list.removeAt(3);
|
||||||
list.removeAt(0);
|
list.toString();
|
||||||
list.removeAt(3);
|
console.log("Removing element 1");
|
||||||
console.log("Removing element 1");
|
list.removeAt(0);
|
||||||
|
list.toString();
|
||||||
|
console.log("Removing element Deez Nuts");
|
||||||
|
list.removeAt(3);
|
||||||
list.toString();
|
list.toString();
|
||||||
|
|
Loading…
Reference in a new issue