Skip to content

Commit 10e7696

Browse files
author
Christian Bender
committed
wrote more object oriented
1 parent b9d749a commit 10e7696

File tree

1 file changed

+65
-51
lines changed

1 file changed

+65
-51
lines changed

Data Structures/Linked List/singlylinklist.js

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,72 @@
88

99
//Functions - add, remove, indexOf, elementAt, addAt, removeAt, view
1010

11+
// class LinkedList and constructor
1112
//Creates a LinkedList
12-
function LinkedList(){
13-
//Length of linklist and head is null at start
14-
var length = 0;
15-
var head = null;
13+
var LinkedList = (function () {
1614

15+
function LinkedList() {
16+
//Length of linklist and head is null at start
17+
this.length = 0;
18+
this.head = null;
19+
20+
}
21+
22+
// class node (constructor)
1723
//Creating Node with element's value
18-
var Node = function(element){
19-
this.element = element;
20-
this.next = null;
21-
};
24+
var Node = (function () {
25+
function Node(element) {
26+
this.element = element;
27+
this.next = null;
28+
}
29+
return Node;
30+
}());
2231

2332
//Returns length
24-
this.size = function(){
25-
return length;
33+
LinkedList.prototype.size = function () {
34+
return this.length;
2635
};
2736

2837
//Returns the head
29-
this.head = function(){
30-
return head;
38+
LinkedList.prototype.head = function () {
39+
return this.head;
3140
};
3241

3342
//Creates a node and adds it to linklist
34-
this.add = function(element){
43+
LinkedList.prototype.add = function (element) {
3544
var node = new Node(element);
3645
//Check if its the first element
37-
if(head === null){
38-
head = node;
46+
if (this.head === null) {
47+
this.head = node;
3948
}
4049
else {
41-
var currentNode = head;
50+
var currentNode = this.head;
4251

4352
//Loop till there is node present in the list
44-
while(currentNode.next){
45-
currentNode = currentNode.next;
53+
while (currentNode.next) {
54+
currentNode = currentNode.next;
4655
}
4756

4857
//Adding node to the end of the list
4958
currentNode.next = node;
5059
}
5160
//Increment the length
52-
length++;
61+
this.length++;
5362
};
5463

5564
//Removes the node with the value as param
56-
this.remove = function(element){
57-
var currentNode = head;
65+
LinkedList.prototype.remove = function (element) {
66+
var currentNode = this.head;
5867
var previousNode;
5968

6069
//Check if the head node is the element to remove
61-
if(currentNode.element === element){
62-
head = currentNode.next;
70+
if (currentNode.element === element) {
71+
this.head = currentNode.next;
6372
}
6473
else {
6574

6675
//Check which node is the node to remove
67-
while(currentNode.element !== element) {
76+
while (currentNode.element !== element) {
6877
previousNode = currentNode;
6978
currentNode = currentNode.next;
7079
}
@@ -74,25 +83,25 @@ function LinkedList(){
7483
}
7584

7685
//Decrementing the length
77-
length--;
86+
this.length--;
7887
};
7988

8089
//Return if the list is empty
81-
this.isEmpty = function(){
82-
return length === 0;
90+
LinkedList.prototype.isEmpty = function () {
91+
return this.length === 0;
8392
};
8493

8594
//Returns the index of the element passed as param otherwise -1
86-
this.indexOf = function(element) {
87-
var currentNode = head;
95+
LinkedList.prototype.indexOf = function (element) {
96+
var currentNode = this.head;
8897
var index = -1;
8998

90-
while(currentNode){
99+
while (currentNode) {
91100
index++;
92101

93102
//Checking if the node is the element we are searching for
94-
if(currentNode.element === element){
95-
return index+1;
103+
if (currentNode.element === element) {
104+
return index + 1;
96105
}
97106
currentNode = currentNode.next;
98107
}
@@ -101,34 +110,34 @@ function LinkedList(){
101110
};
102111

103112
//Returns the element at an index
104-
this.elementAt = function(index){
105-
var currentNode = head;
113+
LinkedList.prototype.elementAt = function (index) {
114+
var currentNode = this.head;
106115
var count = 0;
107-
while(count < index){
116+
while (count < index) {
108117
count++;
109118
currentNode = currentNode.next;
110119
}
111120
return currentNode.element;
112121
};
113122

114123
//Adds the element at specified index
115-
this.addAt = function(index, element){
124+
LinkedList.prototype.addAt = function (index, element) {
116125
index--;
117126
var node = new Node(element);
118127

119-
var currentNode = head;
128+
var currentNode = this.head;
120129
var previousNode;
121130
var currentIndex = 0;
122131

123132
//Check if index is out of bounds of list
124-
if(index > length){
133+
if (index > this.length) {
125134
return false;
126135
}
127136

128137
//Check if index is the start of list
129-
if(index === 0){
138+
if (index === 0) {
130139
node.next = currentNode;
131-
head = node;
140+
this.head = node;
132141
}
133142
else {
134143
while (currentIndex < index) {
@@ -143,24 +152,25 @@ function LinkedList(){
143152
}
144153

145154
//Incrementing the length
146-
length++;
155+
this.length++;
156+
return true;
147157
};
148158

149159
//Removes the node at specified index
150-
this.removeAt = function(index) {
160+
LinkedList.prototype.removeAt = function (index) {
151161
index--;
152-
var currentNode = head;
162+
var currentNode = this.head;
153163
var previousNode;
154164
var currentIndex = 0;
155165

156166
//Check if index is present in list
157-
if(index < 0 || index >= length){
167+
if (index < 0 || index >= this.length) {
158168
return null;
159169
}
160170

161171
//Check if element is the first element
162172
if (index === 0) {
163-
head = currentNode.next;
173+
this.head = currentNode.next;
164174
}
165175
else {
166176
while (currentIndex < index) {
@@ -172,21 +182,25 @@ function LinkedList(){
172182
}
173183

174184
//Decrementing the length
175-
length--;
185+
this.length--;
176186
return currentNode.element;
177187
};
178188

179189
//Function to view the LinkedList
180-
this.view = function () {
181-
var currentNode = head;
190+
LinkedList.prototype.view = function () {
191+
var currentNode = this.head;
182192
var count = 0;
183-
while(count < length){
193+
while (count < this.length) {
184194
count++;
185195
console.log(currentNode.element);
186196
currentNode = currentNode.next;
187197
}
188198
};
189-
};
199+
200+
// returns the constructor
201+
return LinkedList;
202+
203+
}());
190204

191205
//Implementation of LinkedList
192206
var linklist = new LinkedList();
@@ -197,7 +211,7 @@ linklist.add(12);
197211
linklist.add(17);
198212
console.log(linklist.size());
199213
console.log(linklist.removeAt(4));
200-
linklist.addAt(4,15);
214+
linklist.addAt(4, 15);
201215
console.log(linklist.indexOf(8));
202216
console.log(linklist.size());
203217
linklist.view();

0 commit comments

Comments
 (0)