-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathds.js
141 lines (138 loc) · 2.95 KB
/
ds.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
// 1.stack C style
// 2.stack JS style
// 3.Queue C style
// 4.Queue JS style
console.log('\n <==================>')
console.log('Example of C style Stack')
// Build stack('C' style) using linked list.
// we do not use pointer specific syntax because objects are reference.
var Stack = function(){
this.node = function(data){
this.data = data || {}
this.next = undefined
}
this.head = undefined
this.show = function (argument) {
if(this.isEmpty()){
console.log('Empty List')
return
}
var traveller = this.head
console.log('Elements in the list')
while(traveller){
console.log(traveller.data)
traveller = traveller.next
}
}
this.push = function(item){
if(!this.head)
this.head = new this.node(item)
else{
var traveller = this.head
while(traveller.next)
traveller = traveller.next
traveller.next = new this.node(item)
}
},
this.isEmpty = function(){
return this.head === undefined
},
this.pop = function(item){
if(this.isEmpty())
return
var traveller, prev
traveller = prev = this.head
while(traveller.next){
prev = traveller
traveller = traveller.next
}
console.log('last object is->', traveller.data)
prev.next = undefined
}
}
var s1 = new Stack()
s1.push(1)
s1.push(2)
s1.push(3)
s1.push(4)
s1.push(5)
s1.show()
s1.pop()
s1.show()
s1.push(5)
s1.push(6)
s1.show()
s1.pop()
s1.show()
console.log('\n <==================>')
console.log('Example of JS style Stack')
// Build stack('JS' style)
var JSStack = []
JSStack.push(1)
JSStack.push(2)
console.log(JSStack)
JSStack.pop()
console.log(JSStack)
JSStack.push(3)
console.log(JSStack)
console.log('\n <==================>')
console.log('Example of C style Queue')
var Q = function() {
this.node = function(data){
this.data = data || {}
this.next = undefined
}
this.head = undefined
this.end = undefined
this.show = function (argument) {
if(this.isEmpty()){
console.log('Empty List')
return
}
var traveller = this.head
console.log('Elements in the list')
while(traveller){
console.log(traveller.data)
traveller = traveller.next
}
}
this.add = function(item){
if(!this.head){
this.head = new this.node(item)
this.end = this.head
}
else{
this.end.next = new this.node(item)
this.end = this.end.next
}
},
this.isEmpty = function(){
return this.head === 'undefined'
}
this.remove = function(item){
if(this.isEmpty())
return
console.log('First object is->', this.head.data)
var newHead = this.head.next
delete this.head
this.head = newHead
}
}
var q1 = new Q()
q1.add(1)
q1.add(2)
q1.add(3)
q1.show()
q1.add(4)
q1.show()
q1.remove()
q1.show()
console.log('\n <==================>')
console.log('Example of JS style Queue')
// Build queue('JS' style)
var JSQ = []
JSQ.push(10)
JSQ.push(40)
console.log("list is", JSQ)
var i = JSQ.shift()
console.log(i)