Created
March 8, 2021 14:24
-
-
Save incognitokshitij/e30771a3af334251a2cdb644132beb8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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(); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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