-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathStackUsingLinkedList.java
More file actions
72 lines (62 loc) · 1.54 KB
/
StackUsingLinkedList.java
File metadata and controls
72 lines (62 loc) · 1.54 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
public class Stack {
// ArrayList<Integer> al = new ArrayList<>();
// int array [];
Node head;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
// Push element into the stack
public void push(int item) {
Node node = new Node(item);
if (head == null) {
head = node;
return;
}
node.next = head;
head = node;
}
//
public int pop() {
if (isEmpty()) {
System.out.println("Overflow Error");
return -1;
}
Node top = head;
head = head.next;
return top.data;
}
public int peek() {
if (isEmpty()) {
System.out.println("Overflow Error");
return -1;
}
// return head.data;
Node top = head;
return top.data;
}
public boolean isEmpty() {
return head == null;
}
public static void main(String[] args) {
Stack st = new Stack();
st.push(30);
st.push(40);
st.push(50);
st.push(60);
// System.out.println(st.peek());
// System.out.println(st.peek());
// System.out.println(st.peek());
// System.out.println(st.peek());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.pop());
System.out.println(st.peek());
}
}