-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackImplementationUsingLinkedListExample.java
More file actions
192 lines (153 loc) · 5.62 KB
/
StackImplementationUsingLinkedListExample.java
File metadata and controls
192 lines (153 loc) · 5.62 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.ds.java;
/**
*Exception to indicate that LinkedList is empty.
*/
class LinkedListEmptyException extends RuntimeException{
public LinkedListEmptyException(){
super();
}
public LinkedListEmptyException(String message){
super(message);
}
}
/**
*Exception to indicate that Stack is empty.
*/
class StackEmptyException extends RuntimeException {
public StackEmptyException(){
super();
}
public StackEmptyException(String message){
super(message);
}
}
/**
*Node class, which holds data and contains next which points to next Node.
*/
class Node3 {
public int data; // data in Node.
public Node3 next; // points to next Node in list.
/**
* Constructor
*/
public Node3(int data){
this.data = data;
}
/**
* Display Node's data
*/
public void displayNode() {
System.out.print( data + " ");
}
}
/**
* LinkedList class
*/
class LinkedList3 {
private Node3 first; // ref to first link on list
/**
* LinkedList constructor
*/
public LinkedList3(){
first = null;
}
/**
* Insert New Node at first position
*/
public void insertFirst(int data) {
Node3 newNode = new Node3(data); //Creation of New Node.
newNode.next = first; //newLink ---> old first
first = newNode; //first ---> newNode
}
/**
* Deletes first Node
*/
public Node3 deleteFirst()
{
if(first==null){ //means LinkedList in empty, throw exception.
throw new LinkedListEmptyException("LinkedList doesn't contain any Nodes.");
}
Node3 tempNode = first; // save reference to first Node in tempNode- so that we could return saved reference.
first = first.next; // delete first Node (make first point to second node)
return tempNode; // return tempNode (i.e. deleted Node)
}
/**
* Display LinkedList
*/
public void displayLinkedList() {
Node3 tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null){ // Executes until we don't find end of list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
}
System.out.println();
}
}
/**
* For implementing stack using using LinkedList- This StackLinkedList
* class internally maintains LinkedList reference in java.
*/
class StackLinkedList{
LinkedList3 linkedList = new LinkedList3(); // creation of Linked List
/**
* Push items in stack, it will put items on top of Stack.
*/
public void push(int value){
linkedList.insertFirst(value);
}
/**
* Pop items in stack, it will remove items from top of Stack.
*/
public void pop() throws StackEmptyException {
try{
linkedList.deleteFirst();
}catch(LinkedListEmptyException llee){
throw new StackEmptyException();
}
}
/**
* Display stack.
*/
public void displayStack() {
System.out.print("Displaying Stack > Top to Bottom : ");
linkedList.displayLinkedList();
}
}
/**
* Main class - To test Stack Implementation Using LinkedList in java
*/
public class StackImplementationUsingLinkedListExample {
public static void main(String[] args) {
StackLinkedList stackLinkedList=new StackLinkedList();
System.out.println("INSERTING AT FIRST (TOP) IN STACK IMPLEMENTED USING LINKED LIST ");
stackLinkedList.push(39); //push node.
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
stackLinkedList.push(71); //push node.
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
stackLinkedList.push(11); //push node.
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
stackLinkedList.push(76); //push node.
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
System.out.println("\nDELETING FROM FIRST (TOP) FROM STACK IMPLEMENTED USING LINKED LIST ");
stackLinkedList.pop(); //pop Node
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
stackLinkedList.pop(); //pop Node
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
stackLinkedList.pop(); //pop Node
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
stackLinkedList.pop(); //pop Node
stackLinkedList.displayStack(); // display STACK IMPLEMENTED USING LINKED LIST in java
}
}
/*OUTPUT
INSERTING AT FIRST (TOP) IN STACK IMPLEMENTED USING LINKED LIST
Displaying Stack > Top to Bottom : 39
Displaying Stack > Top to Bottom : 71 39
Displaying Stack > Top to Bottom : 11 71 39
Displaying Stack > Top to Bottom : 76 11 71 39
DELETING FROM FIRST (TOP) FROM STACK IMPLEMENTED USING LINKED LIST
Displaying Stack > Top to Bottom : 11 71 39
Displaying Stack > Top to Bottom : 71 39
Displaying Stack > Top to Bottom : 39
Displaying Stack > Top to Bottom :
*/