-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLinkedlist.java
More file actions
56 lines (43 loc) · 875 Bytes
/
StackLinkedlist.java
File metadata and controls
56 lines (43 loc) · 875 Bytes
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
package com.sai;
import java.util.EmptyStackException;
public class StackLinkedlist {
ArrayNode top = null;
public void push(int data) {
ArrayNode newNode = new ArrayNode(data);
newNode.next = top;
top = newNode;
}
public void pop() {
if (top == null) {
return;
}
System.out.println("Element popped is" + top.data);
top = top.next;
}
public void display() {
ArrayNode dummy = top;
while (dummy.next != null) {
System.out.println(dummy.data);
dummy = dummy.next;
}
System.out.println(dummy.data);
}
public static void main(String[] args) {
StackLinkedlist sl = new StackLinkedlist();
sl.push(1);
sl.push(2);
sl.push(3);
sl.push(4);
sl.display();
sl.pop();
System.out.println("After popping");
sl.display();
}
}
class ArrayNode {
int data;
ArrayNode next;
ArrayNode(int data) {
this.data = data;
}
}