11
11
// console.log(n1);
12
12
13
13
class Node {
14
- constructor ( data , next = null ) {
15
- this . data = data ;
16
- this . next = next ;
17
- }
14
+ constructor ( data , next = null ) {
15
+ this . data = data ;
16
+ this . next = next ;
17
+ }
18
18
}
19
19
20
20
const n1 = new Node ( 100 ) ;
21
21
22
- // console.log(n1);
23
-
24
22
class LinkedList {
25
- constructor ( ) {
26
- this . head = null ;
27
- this . size = 0 ;
28
- }
29
-
30
- // Search first node with key k
31
- listSearch ( data ) {
32
- if ( ! this . head ) return null ;
33
- let current = this . head ;
34
- while ( current !== null && current . data !== data ) {
35
- current = current . next ;
36
- }
37
-
38
- return current ;
39
- }
40
-
41
- // Insert first node
42
- insertFirst ( data ) {
43
- this . head = new Node ( data , this . head ) ;
44
- this . size ++ ;
45
- }
46
-
47
- // Insert last node
48
- insertLast ( data ) {
49
- let node = new Node ( data ) ;
50
- let current ;
51
-
52
- // If empty, make this node as the head
53
- if ( ! this . head ) {
54
- this . head = node ;
55
- } else {
56
- current = this . head ;
57
- while ( current . next ) {
58
- current = current . next ;
59
- }
60
-
61
- current . next = node ;
62
- }
63
-
64
- this . size ++ ;
65
- }
66
-
67
- // Insert at index
68
- insertIndex ( data , index ) {
69
- // If index is out of range
70
- if ( index > 0 && index > this . size ) return ;
71
-
72
- // If index = 0
73
- if ( index === 0 ) {
74
- this . insertFirst ( data ) ;
75
- return ;
76
- }
77
-
78
- const node = new Node ( data ) ;
79
- let current , previous ;
80
-
81
- // Set current to first
82
- current = this . head ;
83
- let count = 0 ;
84
-
85
- while ( count < index ) {
86
- previous = current ; // Node before the index
87
- count ++ ;
88
- current = current . next ; // Node after the index
89
- }
90
-
91
- node . next = current ;
92
- previous . next = node ;
93
-
94
- this . size ++ ;
95
- }
96
-
97
- // Get at index
98
- getAt ( index ) {
99
- let current = this . head ;
100
- let count = 0 ;
101
-
102
- while ( current ) {
103
- if ( count === index ) {
104
- console . log ( current . data ) ;
105
- }
106
- count ++ ;
107
- current = current . next ;
108
- }
109
-
110
- return null ;
111
- }
112
-
113
- // Remove at index
114
- removeAt ( index ) {
115
- if ( index > 0 && index > this . size ) return ;
116
-
117
- let current = this . head ;
118
- let previous ;
119
- let count = 0 ;
120
-
121
- // Remove first node
122
- if ( index === 0 ) {
123
- this . head = current . next ;
124
- } else {
125
- while ( count < index ) {
126
- count ++ ;
127
- previous = current ;
128
- current = current . next ;
129
- }
130
-
131
- previous . next = current . next ;
132
- }
133
-
134
- this . size -- ;
135
- }
136
-
137
- // Clear the list
138
- clearList ( ) {
139
- this . head = null ;
140
- this . size = 0 ;
141
- }
142
-
143
- // Print the list data
144
- printListData ( ) {
145
- let current = this . head ;
146
-
147
- while ( current ) {
148
- console . log ( current . data ) ;
149
- current = current . next ;
150
- }
151
- }
23
+ constructor ( ) {
24
+ this . head = null ;
25
+ this . size = 0 ;
26
+ }
27
+
28
+ // Search first node with key k
29
+ listSearch ( data ) {
30
+ if ( ! this . head ) return null ;
31
+ let current = this . head ;
32
+ while ( current !== null && current . data !== data ) {
33
+ current = current . next ;
34
+ }
35
+
36
+ return current ;
37
+ }
38
+
39
+ // Insert first node
40
+ insertFirst ( data ) {
41
+ this . head = new Node ( data , this . head ) ;
42
+ this . size ++ ;
43
+ }
44
+
45
+ // Insert last node
46
+ insertLast ( data ) {
47
+ let node = new Node ( data ) ;
48
+ let current ;
49
+
50
+ // If empty, make this node as the head
51
+ if ( ! this . head ) {
52
+ this . head = node ;
53
+ } else {
54
+ current = this . head ;
55
+ while ( current . next ) {
56
+ current = current . next ;
57
+ }
58
+
59
+ current . next = node ;
60
+ }
61
+
62
+ this . size ++ ;
63
+ }
64
+
65
+ // Insert at index
66
+ insertIndex ( data , index ) {
67
+ // If index is out of range
68
+ if ( index > 0 && index > this . size ) return ;
69
+
70
+ // If index = 0
71
+ if ( index === 0 ) {
72
+ this . insertFirst ( data ) ;
73
+ return ;
74
+ }
75
+
76
+ const node = new Node ( data ) ;
77
+ let current , previous ;
78
+
79
+ // Set current to first
80
+ current = this . head ;
81
+ let count = 0 ;
82
+
83
+ while ( count < index ) {
84
+ previous = current ; // Node before the index
85
+ count ++ ;
86
+ current = current . next ; // Node after the index
87
+ }
88
+
89
+ node . next = current ;
90
+ previous . next = node ;
91
+
92
+ this . size ++ ;
93
+ }
94
+
95
+ // Get at index
96
+ getAt ( index ) {
97
+ let current = this . head ;
98
+ let count = 0 ;
99
+
100
+ while ( current ) {
101
+ if ( count === index ) {
102
+ console . log ( current . data ) ;
103
+ return current . data ;
104
+ }
105
+ count ++ ;
106
+ current = current . next ;
107
+ }
108
+
109
+ return null ;
110
+ }
111
+
112
+ // Remove at index
113
+ removeAt ( index ) {
114
+ if ( index > 0 && index > this . size ) return ;
115
+
116
+ let current = this . head ;
117
+ let previous ;
118
+ let count = 0 ;
119
+
120
+ // Remove first node
121
+ if ( index === 0 ) {
122
+ this . head = current . next ;
123
+ } else {
124
+ while ( count < index ) {
125
+ count ++ ;
126
+ previous = current ;
127
+ current = current . next ;
128
+ }
129
+
130
+ previous . next = current . next ;
131
+ }
132
+
133
+ this . size -- ;
134
+ }
135
+
136
+ // Clear the list
137
+ clearList ( ) {
138
+ this . head = null ;
139
+ this . size = 0 ;
140
+ }
141
+
142
+ // Print the list data
143
+ printListData ( ) {
144
+ let current = this . head ;
145
+
146
+ while ( current ) {
147
+ // console.log(current.data);
148
+ current = current . next ;
149
+ }
150
+ }
152
151
}
153
152
153
+ module . exports = LinkedList ;
154
+
154
155
const ll = new LinkedList ( ) ;
155
156
ll . insertFirst ( 300 ) ;
156
157
ll . insertFirst ( 100 ) ;
@@ -166,5 +167,5 @@ ll.removeAt(11);
166
167
// ll.clearList();
167
168
ll . printListData ( ) ;
168
169
ll . getAt ( 10 ) ;
169
- console . log ( JSON . stringify ( ll , 0 , 2 ) ) ;
170
- console . log ( JSON . stringify ( ll . listSearch ( 500 ) , 0 , 2 ) ) ;
170
+ // console.log(JSON.stringify(ll, 0, 2));
171
+ // console.log(JSON.stringify(ll.listSearch(500), 0, 2));
0 commit comments