Merge pull request #49 from christianbender/changed_linklist

wrote more object oriented
This commit is contained in:
Christian Bender
2018-03-30 18:14:29 +02:00
committed by GitHub

View File

@ -8,63 +8,72 @@
//Functions - add, remove, indexOf, elementAt, addAt, removeAt, view //Functions - add, remove, indexOf, elementAt, addAt, removeAt, view
// class LinkedList and constructor
//Creates a LinkedList //Creates a LinkedList
function LinkedList(){ var LinkedList = (function () {
//Length of linklist and head is null at start
var length = 0;
var head = null;
function LinkedList() {
//Length of linklist and head is null at start
this.length = 0;
this.head = null;
}
// class node (constructor)
//Creating Node with element's value //Creating Node with element's value
var Node = function(element){ var Node = (function () {
this.element = element; function Node(element) {
this.next = null; this.element = element;
}; this.next = null;
}
return Node;
}());
//Returns length //Returns length
this.size = function(){ LinkedList.prototype.size = function () {
return length; return this.length;
}; };
//Returns the head //Returns the head
this.head = function(){ LinkedList.prototype.head = function () {
return head; return this.head;
}; };
//Creates a node and adds it to linklist //Creates a node and adds it to linklist
this.add = function(element){ LinkedList.prototype.add = function (element) {
var node = new Node(element); var node = new Node(element);
//Check if its the first element //Check if its the first element
if(head === null){ if (this.head === null) {
head = node; this.head = node;
} }
else { else {
var currentNode = head; var currentNode = this.head;
//Loop till there is node present in the list //Loop till there is node present in the list
while(currentNode.next){ while (currentNode.next) {
currentNode = currentNode.next; currentNode = currentNode.next;
} }
//Adding node to the end of the list //Adding node to the end of the list
currentNode.next = node; currentNode.next = node;
} }
//Increment the length //Increment the length
length++; this.length++;
}; };
//Removes the node with the value as param //Removes the node with the value as param
this.remove = function(element){ LinkedList.prototype.remove = function (element) {
var currentNode = head; var currentNode = this.head;
var previousNode; var previousNode;
//Check if the head node is the element to remove //Check if the head node is the element to remove
if(currentNode.element === element){ if (currentNode.element === element) {
head = currentNode.next; this.head = currentNode.next;
} }
else { else {
//Check which node is the node to remove //Check which node is the node to remove
while(currentNode.element !== element) { while (currentNode.element !== element) {
previousNode = currentNode; previousNode = currentNode;
currentNode = currentNode.next; currentNode = currentNode.next;
} }
@ -74,25 +83,25 @@ function LinkedList(){
} }
//Decrementing the length //Decrementing the length
length--; this.length--;
}; };
//Return if the list is empty //Return if the list is empty
this.isEmpty = function(){ LinkedList.prototype.isEmpty = function () {
return length === 0; return this.length === 0;
}; };
//Returns the index of the element passed as param otherwise -1 //Returns the index of the element passed as param otherwise -1
this.indexOf = function(element) { LinkedList.prototype.indexOf = function (element) {
var currentNode = head; var currentNode = this.head;
var index = -1; var index = -1;
while(currentNode){ while (currentNode) {
index++; index++;
//Checking if the node is the element we are searching for //Checking if the node is the element we are searching for
if(currentNode.element === element){ if (currentNode.element === element) {
return index+1; return index + 1;
} }
currentNode = currentNode.next; currentNode = currentNode.next;
} }
@ -101,10 +110,10 @@ function LinkedList(){
}; };
//Returns the element at an index //Returns the element at an index
this.elementAt = function(index){ LinkedList.prototype.elementAt = function (index) {
var currentNode = head; var currentNode = this.head;
var count = 0; var count = 0;
while(count < index){ while (count < index) {
count++; count++;
currentNode = currentNode.next; currentNode = currentNode.next;
} }
@ -112,23 +121,23 @@ function LinkedList(){
}; };
//Adds the element at specified index //Adds the element at specified index
this.addAt = function(index, element){ LinkedList.prototype.addAt = function (index, element) {
index--; index--;
var node = new Node(element); var node = new Node(element);
var currentNode = head; var currentNode = this.head;
var previousNode; var previousNode;
var currentIndex = 0; var currentIndex = 0;
//Check if index is out of bounds of list //Check if index is out of bounds of list
if(index > length){ if (index > this.length) {
return false; return false;
} }
//Check if index is the start of list //Check if index is the start of list
if(index === 0){ if (index === 0) {
node.next = currentNode; node.next = currentNode;
head = node; this.head = node;
} }
else { else {
while (currentIndex < index) { while (currentIndex < index) {
@ -143,24 +152,25 @@ function LinkedList(){
} }
//Incrementing the length //Incrementing the length
length++; this.length++;
return true;
}; };
//Removes the node at specified index //Removes the node at specified index
this.removeAt = function(index) { LinkedList.prototype.removeAt = function (index) {
index--; index--;
var currentNode = head; var currentNode = this.head;
var previousNode; var previousNode;
var currentIndex = 0; var currentIndex = 0;
//Check if index is present in list //Check if index is present in list
if(index < 0 || index >= length){ if (index < 0 || index >= this.length) {
return null; return null;
} }
//Check if element is the first element //Check if element is the first element
if (index === 0) { if (index === 0) {
head = currentNode.next; this.head = currentNode.next;
} }
else { else {
while (currentIndex < index) { while (currentIndex < index) {
@ -172,21 +182,25 @@ function LinkedList(){
} }
//Decrementing the length //Decrementing the length
length--; this.length--;
return currentNode.element; return currentNode.element;
}; };
//Function to view the LinkedList //Function to view the LinkedList
this.view = function () { LinkedList.prototype.view = function () {
var currentNode = head; var currentNode = this.head;
var count = 0; var count = 0;
while(count < length){ while (count < this.length) {
count++; count++;
console.log(currentNode.element); console.log(currentNode.element);
currentNode = currentNode.next; currentNode = currentNode.next;
} }
}; };
};
// returns the constructor
return LinkedList;
}());
//Implementation of LinkedList //Implementation of LinkedList
var linklist = new LinkedList(); var linklist = new LinkedList();
@ -197,7 +211,7 @@ linklist.add(12);
linklist.add(17); linklist.add(17);
console.log(linklist.size()); console.log(linklist.size());
console.log(linklist.removeAt(4)); console.log(linklist.removeAt(4));
linklist.addAt(4,15); linklist.addAt(4, 15);
console.log(linklist.indexOf(8)); console.log(linklist.indexOf(8));
console.log(linklist.size()); console.log(linklist.size());
linklist.view(); linklist.view();