-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkList2.js
More file actions
101 lines (88 loc) · 2 KB
/
Copy pathlinkList2.js
File metadata and controls
101 lines (88 loc) · 2 KB
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
// SLL contructor
function SLL(){
this.head = null;
}
//SLLNode contructor
function SLLNode(val){
this.val = val;
this.next = null;
}
//creating an instance of SLL this our single Link List
var mySLL = new SLL();
console.log(mySLL)
// //push method: this add a node to list instance
// SLL.prototype.push = function(val) {
// var newNode = new SLLNode(val);
// if(!this.head) {
// this.head = newNode;
// return this;
// }
// var runner = this.head;
// while(runner.next){
// runner = runner.next
// }
// runner.next = newNode
// return this;
// }
// //head is null calling push
// console.log(mySLL.push(4))
// //now we have one node in our list! now we add 8;
// console.log(mySLL.push(8))
// console.log(mySLL.push(10))
// //pop method: remove a node from the end of the list:
// SLL.prototype.pop = function () {
// if(!this.head) {
// return this;
// }
// if(!this.head.next) {
// this.head = null;
// return this;
// }
// var runner = this.head;
// //thej version:
// // while(runner.next.next) {
// // runner = runner.next
// // }
// // runner.next = null;
// // return this;
// //Havisha version
// while(runner.next){
// if(!runner.next.next){
// runner.next = null;
// return this;
// }
// runner = runner.next;
// }
// }
// //popping two values fron the back
// mySLL.pop().pop()
// // mySLL.pop()
// console.log(mySLL)
// //shift remove a node from the front
// SLL.prototype.shift = function (){
// if(!this.head) {
// return this;
// }
// this.head = this.head.next;
// return this;
// }
// console.log(mySLL)
// console.log(mySLL.push(10))
// console.log(mySLL.push(8))
// console.log(mySLL.push(9))
// console.log(mySLL.push(2))
// console.log(mySLL)
// mySLL.shift()
// mySLL.shift()
// // console.log(mySLL)
//unshift : add value to the front of the link List :
mySLL.prototype.unshift = function (val) {
var newNode = new SLLNode(val);
if(!this.head){
this.head = newNode;
return this;
}
newNode.next = this.head;
this.head = newNode;
return this;
}