-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListsStackImplementation.html
More file actions
88 lines (74 loc) · 2.23 KB
/
LinkedListsStackImplementation.html
File metadata and controls
88 lines (74 loc) · 2.23 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
<!DOCTYPE html>
<html>
<head>
<title>Linked Lists Stack Implementation</title>
<meta charset="UTF-8">
</head>
<body>
<h2>Linked Lists Stack Implementation</h2>
<p>
A stack implementation of a linked list consists of nodes placed on top of each other.
</p>
<p>
Please open the console to see the output.
</p>
<script type="text/javascript">
//Node Constructor
function Node(next, data) {
// Pointer to the next Node.
this.next = next;
// Data
this.data = data;
}
// Head of the List.
var head = null;
// The push() method pushes an object to the top of the stack.
function push(head, data) {
var newNode = new Node(null, data);
if (!newNode) {
return false;
}
// newNode points to the old head and becomes the new head.
newNode.next = head;
this.head = newNode;
return true;
}
// The pop() method removes and returns an object from the top of the stack.
function pop(head) {
if (head == null) {
return false;
}
this.head = head.next;
return head;
}
// The deletestack function traverses the stack and removes each Node.
function deleteStack(head) {
var next;
while (head) {
next = head.next;
this.head = next;
}
}
// Create the stack.
push(head, "ThirdNode");
push(head, "SecondNode");
push(head, "FirstNode");
//Test
console.log(head);
// Push a new object to the top of the stack.
console.log(push(head, "newNode"));
// head should point to the Node with "newNode" as it's data.
console.log(head);
// Pop the Node with "newNode".
console.log(pop(head));
// head now points to the the Node which has data "FirstNode".
console.log(head);
//Output
//({next:{next:{next:null, data:"ThirdNode"}, data:"SecondNode"}, data:"FirstNode"})
//true
//({next:{next:{next:{next:null, data:"ThirdNode"}, data:"SecondNode"}, data:"FirstNode"}, data:"newNode"})
//({next:{next:{next:{next:null, data:"ThirdNode"}, data:"SecondNode"}, data:"FirstNode"}, data:"newNode"})
//({next:{next:{next:null, data:"ThirdNode"}, data:"SecondNode"}, data:"FirstNode"})
</script>
</body>
</html>