Created
February 15, 2021 05:15
-
-
Save kshitijvarshne1/e10405ddc6f567e66703b176041af708 to your computer and use it in GitHub Desktop.
This file contains 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: 15-Feb-21 | |
* Time: 10:35 AM | |
* File: Execution.java | |
*/ | |
package feb15_21_NK.one; | |
public class Execution { | |
public static void main(String[] args) { | |
StackUsingLinkedList stack = new StackUsingLinkedList(); | |
System.out.println(stack.isEmpty()); | |
stack.push(new Node(5)); | |
System.out.println(stack.isEmpty()); | |
stack.peek(); | |
stack.push(new Node(6)); | |
stack.peek(); | |
//stack.pop(); | |
stack.peek(); | |
stack.print(); | |
} | |
} | |
This file contains 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: 15-Feb-21 | |
* Time: 10:17 AM | |
* File: Node.java | |
*/ | |
package feb15_21_NK.one; | |
public class Node { | |
public int data; | |
public Node next; // self reference structure | |
public Node() { | |
data = 0; | |
next = null; | |
} | |
public Node(int d) { | |
data = d; | |
next = null; | |
} | |
} |
This file contains 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: 15-Feb-21 | |
* Time: 10:20 AM | |
* File: StackUsingLinkedList.java | |
*/ | |
package feb15_21_NK.one; | |
public class StackUsingLinkedList { | |
Node top; | |
public StackUsingLinkedList() { | |
top = null; | |
} | |
public boolean isEmpty() { | |
return top == null; | |
} | |
public boolean isFull() { | |
return false; | |
} | |
public void push(Node newNode) { | |
if (top == null) { | |
top = newNode; | |
return; | |
} | |
newNode.next = top; | |
top = newNode; | |
} | |
public void peek() { | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
return; | |
} | |
System.out.println(top.data); | |
} | |
public void print() { | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
return; | |
} | |
Node temp = top; | |
while (temp != null) { | |
System.out.print(temp.data+" -> " ); | |
temp = temp.next; | |
} | |
} | |
public void pop() { | |
if (isEmpty()) { | |
System.out.println("stack is empty"); | |
return; | |
} | |
top = top.next; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment