Skip to content

Instantly share code, notes, and snippets.

@incognitokshitij
Created March 8, 2021 14:24
Show Gist options
  • Select an option

  • Save incognitokshitij/e30771a3af334251a2cdb644132beb8f to your computer and use it in GitHub Desktop.

Select an option

Save incognitokshitij/e30771a3af334251a2cdb644132beb8f to your computer and use it in GitHub Desktop.
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 04-Mar-21
* Time: 10:06 PM
* File: Main.java
*/
package March.mar04_21_NK.two;
public class Main {
public static void main(String[] args) {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(new Node(4));
stack.push(new Node(5));
stack.peek();
stack.pop();
stack.peek();
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 04-Mar-21
* Time: 9:20 PM
* File: Node.java
*/
package March.mar04_21_NK.two;
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 04-Mar-21
* Time: 9:20 PM
* File: StackUsingLinkedList.java
*/
package March.mar04_21_NK.two;
public class StackUsingLinkedList {
public Node top;
public StackUsingLinkedList() {
this.top = null;
}
public boolean isEmpty() {
return top == null;
}
public void push(Node newNode) {
if (isEmpty()) {
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
}
public void peek() {
if (isEmpty()) {
System.out.println("Empty");
} else {
System.out.println(top.data);
}
}
public void pop() {
if (isEmpty()) {
System.out.println("Empty");
} else {
System.out.println(top.data);
top = top.next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment