-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
77 lines (62 loc) · 1.32 KB
/
Copy pathStack.java
File metadata and controls
77 lines (62 loc) · 1.32 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
import java.util.*;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
class Stack {
Node head;
public boolean isEmpty() {
return head == null;
}
public void push(int data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
public int pop() {
if (head == null) {
return -1;
}
int x = head.data;
head = head.next;
return x;
}
public int peek() {
if (head == null) {
return -1;
}
return head.data;
}
public static void main(String[] args) {
Scanner ui = new Scanner(System.in);
Stack s = new Stack();
s.push(4);
s.push(9);
s.push(12);
s.push(54);
s.push(45);
while (!s.isEmpty()) {
System.out.print(s.peek() + " ");
s.pop();
}
System.out.println();
Stack s2 = new Stack();
s2.push(3);
s2.push(5);
s2.push(21);
s2.push(98);
s2.push(43);
while (!s2.isEmpty()) {
System.out.print(s2.peek() + " ");
s2.pop();
}
ui.close();
}
}