1010
1111// class LinkedList and constructor
1212// Creates a LinkedList
13- var LinkedList = ( function ( ) {
13+ const LinkedList = ( function ( ) {
1414 function LinkedList ( ) {
1515 // Length of linklist and head is null at start
1616 this . length = 0
@@ -19,7 +19,7 @@ var LinkedList = (function () {
1919
2020 // class node (constructor)
2121 // Creating Node with element's value
22- var Node = ( function ( ) {
22+ const Node = ( function ( ) {
2323 function Node ( element ) {
2424 this . element = element
2525 this . next = null
@@ -39,12 +39,12 @@ var LinkedList = (function () {
3939
4040 // Creates a node and adds it to linklist
4141 LinkedList . prototype . add = function ( element ) {
42- var node = new Node ( element )
42+ const node = new Node ( element )
4343 // Check if its the first element
4444 if ( this . head === null ) {
4545 this . head = node
4646 } else {
47- var currentNode = this . head
47+ let currentNode = this . head
4848
4949 // Loop till there is node present in the list
5050 while ( currentNode . next ) {
@@ -60,8 +60,8 @@ var LinkedList = (function () {
6060
6161 // Removes the node with the value as param
6262 LinkedList . prototype . remove = function ( element ) {
63- var currentNode = this . head
64- var previousNode
63+ let currentNode = this . head
64+ let previousNode
6565
6666 // Check if the head node is the element to remove
6767 if ( currentNode . element === element ) {
@@ -88,8 +88,8 @@ var LinkedList = (function () {
8888
8989 // Returns the index of the element passed as param otherwise -1
9090 LinkedList . prototype . indexOf = function ( element ) {
91- var currentNode = this . head
92- var index = - 1
91+ let currentNode = this . head
92+ let index = - 1
9393
9494 while ( currentNode ) {
9595 index ++
@@ -106,8 +106,8 @@ var LinkedList = (function () {
106106
107107 // Returns the element at an index
108108 LinkedList . prototype . elementAt = function ( index ) {
109- var currentNode = this . head
110- var count = 0
109+ let currentNode = this . head
110+ let count = 0
111111 while ( count < index ) {
112112 count ++
113113 currentNode = currentNode . next
@@ -118,11 +118,11 @@ var LinkedList = (function () {
118118 // Adds the element at specified index
119119 LinkedList . prototype . addAt = function ( index , element ) {
120120 index --
121- var node = new Node ( element )
121+ const node = new Node ( element )
122122
123- var currentNode = this . head
124- var previousNode
125- var currentIndex = 0
123+ let currentNode = this . head
124+ let previousNode
125+ let currentIndex = 0
126126
127127 // Check if index is out of bounds of list
128128 if ( index > this . length ) {
@@ -153,9 +153,9 @@ var LinkedList = (function () {
153153 // Removes the node at specified index
154154 LinkedList . prototype . removeAt = function ( index ) {
155155 index --
156- var currentNode = this . head
157- var previousNode
158- var currentIndex = 0
156+ let currentNode = this . head
157+ let previousNode
158+ let currentIndex = 0
159159
160160 // Check if index is present in list
161161 if ( index < 0 || index >= this . length ) {
@@ -181,8 +181,8 @@ var LinkedList = (function () {
181181
182182 // Function to view the LinkedList
183183 LinkedList . prototype . view = function ( ) {
184- var currentNode = this . head
185- var count = 0
184+ let currentNode = this . head
185+ let count = 0
186186 while ( count < this . length ) {
187187 count ++
188188 console . log ( currentNode . element )
@@ -195,7 +195,7 @@ var LinkedList = (function () {
195195} ( ) )
196196
197197// Implementation of LinkedList
198- var linklist = new LinkedList ( )
198+ const linklist = new LinkedList ( )
199199linklist . add ( 2 )
200200linklist . add ( 5 )
201201linklist . add ( 8 )
0 commit comments