-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
893988f
commit 2bab08d
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package Java_DSA.Stack; | ||
|
||
import java.util.EmptyStackException; | ||
import java.util.Scanner; | ||
|
||
public class StackLL { | ||
private Node top; | ||
|
||
private class Node{ | ||
int data; | ||
Node next; | ||
public Node(int data){ | ||
this.data = data; | ||
this.next = null; | ||
} | ||
} | ||
public boolean isEmpty(){ | ||
return top == null; | ||
} | ||
|
||
public void display(){ | ||
if(isEmpty()){ | ||
System.out.println("Stack is Empty."); | ||
return; | ||
} | ||
Node ptr = top; | ||
while(ptr != null){ | ||
System.out.println(ptr.data); | ||
ptr = ptr.next; | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public void push(int data){ | ||
Node temp = new Node(data); | ||
if(isEmpty()){ | ||
top = temp; | ||
return; | ||
} | ||
temp.next = top; | ||
top = temp; | ||
} | ||
|
||
public int pop(){ | ||
if (isEmpty()){ | ||
System.out.println("Stack is Empty,No element to pop."); | ||
throw new EmptyStackException(); | ||
} | ||
int data = top.data; | ||
top = top.next; | ||
return data; | ||
} | ||
|
||
public int peek(){ | ||
if(isEmpty()){ | ||
System.out.println("Stack is Empty."); | ||
throw new EmptyStackException(); | ||
} | ||
return top.data; | ||
} | ||
|
||
public int calSize(){ | ||
int size = 0; | ||
if(isEmpty()){ | ||
return 0; | ||
} | ||
Node ptr = top; | ||
while(ptr != null){ | ||
size++; | ||
ptr = ptr.next; | ||
} | ||
return size; | ||
} | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
StackLL stack = new StackLL(); | ||
boolean flag = true; | ||
System.out.println("1)Push.\n2)Pop.\n3)Peek.\n4)Display.\n5)Calculate size.\n6)Exit."); | ||
while(flag){ | ||
System.out.println("Enter your choice : "); | ||
int choice = sc.nextInt(); | ||
switch (choice){ | ||
case 1->{ | ||
System.out.println("Enter data to push : "); | ||
int value = sc.nextInt(); | ||
stack.push(value); | ||
} | ||
case 2->{ | ||
System.out.println("Popped element is : "+stack.pop()); | ||
} | ||
case 3->{ | ||
System.out.println("Peek of stack is : "+stack.peek()); | ||
} | ||
case 4->{ | ||
stack.display(); | ||
} | ||
case 5->{ | ||
System.out.println("Size of the stack is : "+stack.calSize()); | ||
} | ||
case 6->{ | ||
flag = false; | ||
} | ||
default -> { | ||
System.out.println("Invalid choice..!!"); | ||
} | ||
} | ||
} | ||
} | ||
} |