Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions Stack/StackLL.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//stack using Linked List
public class StackDS {
public class StackLL {
private static class Node {
int data;
Node next;
Expand All @@ -11,37 +10,37 @@ private static class Node {
}

static class Stack {
public static Node head = null;
public static void push(int data) {
private Node head; // Removed 'public static' from head

public void push(int data) {
Node newNode = new Node(data);

if(head == null) {
if (head == null) {
head = newNode;
} else {
newNode.next = head;
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}

public static boolean isEmpty() {
public boolean isEmpty() {
return head == null;
}

public static int pop() {
if(isEmpty()) {
public int pop() {
if (isEmpty()) {
return -1;
}
Node top = head;
head = head.next;
return top.data;
}

public static int peek() {
if(isEmpty()) {
public int peek() {
if (isEmpty()) {
return -1;
}
Node top = head;
return top.data;
return head.data;
}
}

Expand All @@ -52,9 +51,9 @@ public static void main(String args[]) {
stack.push(3);
stack.push(4);

while(!stack.isEmpty()) {
while (!stack.isEmpty()) {
System.out.println(stack.peek());
stack.pop();
}
}
}
}