Repository navigation
Expand file tree
/
Copy pathStack Using Linked List
More file actions
97 lines (81 loc) · 2.39 KB
/
Copy pathStack Using Linked List
File metadata and controls
97 lines (81 loc) · 2.39 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
package DataS;
class LinkedListStack {
private Node top;
LinkedListStack() {
this.top = null;
}
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Push operation: Add an element to the stack
public void push(int data) {
Node newNode = new Node(data);
if (top == null) {
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
}
// Pop operation: Remove the top element from the stack
public int pop() {
if (top == null) {
System.out.println("Stack is empty");
return -1;
}
int data = top.data;
top = top.next;
return data;
}
// Peek operation: Get the top element without removing it
public int peek() {
if (top == null) {
System.out.println("Stack is empty");
return -1;
}
return top.data;
}
// Check if the stack is empty
public boolean isEmpty() {
return top == null;
}
// Display the stack elements
public void display() {
if (top == null) {
System.out.println("Stack is empty");
return;
}
Node temp = top;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedListStack stack = new LinkedListStack();
// Test push operation
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Stack after pushing 10, 20, 30:");
stack.display(); // Output: 30 20 10
// Test pop operation
System.out.println("Popped element: " + stack.pop()); // Output: 30
System.out.println("Stack after pop operation:");
stack.display(); // Output: 20 10
// Test peek operation
System.out.println("Top element: " + stack.peek()); // Output: 20
// Test isEmpty operation
System.out.println("Is stack empty? " + stack.isEmpty()); // Output: false
// Pop remaining elements
stack.pop();
stack.pop();
System.out.println("Is stack empty after popping all elements? " + stack.isEmpty()); // Output: true
}
}