-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsinglyLinkedList.js
148 lines (142 loc) · 4.09 KB
/
singlyLinkedList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const Node = require("./node");
class SinglyLinkedList{
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
//insert at the beginning
insertAtbeginning(data){
this.length+=1;
if(this.head==null){
this.head = new Node(data);
this.tail = this.head;
return this.head;
}
const newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
return newNode;
}
//insert at the end
insertAtend(data){
this.length+=1;
if(this.head==null){
this.head = new Node(data);
this.tail = this.head;
return this.head;
}
const newNode = new Node(data);
this.tail.next = newNode;
this.tail = this.tail.next
return this.tail;
}
//delete a node {
//at end
deletedAtEnd(){
if(this.isEmpty()){
console.log("list is already empty");
return "list is already empty!"
}
let temp = this.head;
let deletedNode = this.tail;
let cnt = 1;
while(cnt<this.length-1){
cnt++;
temp = temp.next;
}
temp.next = null;
this.tail = temp;
temp = null;
this.length-=1;
return deletedNode;
}
//at beginning
deleteAtbeginning(){
if(this.isEmpty()){
console.log("list is already empty");
return "list is already empty!"
}
let deletedNode = this.head;
this.head =this.head.next;
deletedNode.next = null;
this.length-=1;
return deletedNode;
}
//at indexAt
deleteAtPosition(index){
if(this.isEmpty()){
console.log("list is already empty");
return "list is already empty!"
}
if(index<=0 || index>this.length){
console.log("Invalied Position!");
return "Invalied position!"
}
if(index==1){
this.length-=1;
return this.deleteAtbeginning();
}
if(index==this.length){
this.length-=1;
return this.deletedAtEnd();
}
let curr = this.head.next;
let prev = this.head;
let cnt = 1;
while(cnt++<index-1){
prev = curr;
curr= curr.next;
}
let deleteNode = curr;
prev.next = curr.next;
curr.next = null;
prev = null;
curr = null;
this.length-=1;
return [deleteNode,this.head];
}
// }
//print a linkedlist
printList(){
if(this.head==null){
console.log('List Is empty!');
}
let temp = this.head;
while(temp!=null){
console.log(temp.val);
temp = temp.next;
}
}
//using 1 base indexing
//search for a node
search(target){
if(this.isEmpty()){
console.log("List is empty so not possibble to search!");
}
if(this.tail.val==target){
console.log("your searched value present at index:",this.length);
return true;
}
let temp = this.head;
let count = 0;
while(temp!=null){
count++
if(temp.val==target){
console.log("your searched value present at index:",count);
return true;
}
temp = temp.next
}
console.log("your searched value not present in entire list:");
return false;
}
isEmpty(){
return this.head==null?true:false;
}
showSize(){
console.log(this.length)
return this.length;
}
}
module.exports = SinglyLinkedList;