-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeStack.java
More file actions
88 lines (70 loc) · 2.01 KB
/
NodeStack.java
File metadata and controls
88 lines (70 loc) · 2.01 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
public class NodeStack<T> {
// Node class to represent each element in the stack
private static class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
}
}
private Node<T> top; // Top of the stack
private int size;
// Push an element onto the stack
public void push(T data) {
Node newNode = new Node(data);
newNode.next = this.top;
this.top = newNode;
this.size++;
}
// Pop the top element off the stack
public T pop() {
if (!isEmpty()) {
T result = this.top.data;
this.top = this.top.next;
this.size--;
return result;
}
return null;
}
// Peek at the top element of the stack without removing it
public T peek() {
if (isEmpty()) {
return null;
} else {
return this.top.data;
}
}
// Check if the stack is empty
public boolean isEmpty() {
return this.size == 0;
}
// Size of the stack
public int size() {
int count = 0;
Node<T> current = top;
while (current != null) {
count++;
current = current.next;
}
return count;
}
// Clear the stack
public void clear() {
top = null;
}
public void swapStacks(LinkedList<T> s1, LinkedList<T> s2) {
}
public static void main(String[] args) {
NodeStack<Integer> stack = new NodeStack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("Peek: " + stack.peek()); // 3
System.out.println("Size: " + stack.size()); // 3
System.out.println("Pop: " + stack.pop()); // 3
System.out.println("Peek after pop: " + stack.peek()); // 2
System.out.println("Size after pop: " + stack.size()); // 2
stack.clear();
System.out.println("Is empty after clear: " + stack.isEmpty()); // true
}
}