-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack Link List
More file actions
95 lines (80 loc) · 2.92 KB
/
Stack Link List
File metadata and controls
95 lines (80 loc) · 2.92 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
package DataS;
// Stack class using a linked list
public class StackLinkList {
private Node top; // Top of the stack
// Node class to represent each element in the linked list
class Node {
int data; // Data to be stored in the node
Node next; // Pointer to the next node in the list
// Constructor to initialize the node with given data
Node(int data) {
this.data = data;
this.next = null;
}
}
// Constructor to initialize an empty stack
public StackLinkList() {
this.top = null;
}
// Method to check if the stack is empty
public boolean isEmpty() {
return top == null;
}
// Method to push a new element onto the stack
public void push(int data) {
Node newNode = new Node(data); // Create a new node with the given data
newNode.next = top; // Set the next of new node as top
top = newNode; // Move top to point to the new node
System.out.println("Pushed " + data + " onto the stack.");
}
// Method to pop the top element from the stack
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty. Cannot pop.");
return -1; // Return -1 or throw exception for empty stack scenario
}
int poppedData = top.data; // Get the data from the top node
top = top.next; // Move top to point to the next node
System.out.println("Popped " + poppedData + " from the stack.");
return poppedData;
}
// Method to peek at the top element of the stack without removing it
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty. Cannot peek.");
return -1; // Return -1 or throw exception for empty stack scenario
}
return top.data; // Return data of the top node
}
// Method to print all elements currently in the stack
public void printStack() {
if (isEmpty()) {
System.out.println("Stack is empty.");
return;
}
System.out.println("Elements in the stack:");
Node current = top;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
// Main method to demonstrate the stack operations
public static void main(String[] args) {
StackLinkList stack = new StackLinkList();
// Pushing elements onto the stack
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
// Printing the stack
stack.printStack();
// Popping elements from the stack
stack.pop();
stack.pop();
// Printing the stack after popping
stack.printStack();
// Peeking at the top element
System.out.println("Top element of the stack: " + stack.peek());
}
}