Dsa Stack2 Algorithm
Dsa Stack2 Algorithm
import java.util.*;
class Main{
int p=sc.nextInt();
for(int i=0;i<p;i++){
int q=sc.nextInt();
o.push(q);
o.display();
o.pop();
o.pop();
o.display();
class StackUsingLinkedlist {
int data;
Node next;
Node top;
{
Node temp = new Node();
if (temp == null) {
System.out.print("\nHeap Overflow");
return;
temp.data = x;
temp.next = top;
top = temp;
if (!isEmpty()) {
return top.data;
else {
System.out.println("Stack is empty");
return -1;
if (top == null) {
System.out.println("Stack is empty");
return;
top = (top).next;
{
if (top == null) {
System.out.println("Stack is empty");
System.exit(1);
else {
System.out.print(temp.data);
temp = temp.next;
if(temp != null)
System.out.println();
Algorithm:
1.take user input for number of elements needed to be
added in stack
2.take those elements from stack
3.push those elements into stack one top on other
4.can display the elements present in the stack
5.we can use peek() method to print the top most
element in the stock
6.pop() to remove the top element and pointing top to
the below element
Algorithm:
1.create an array with a length of 9
2.using switch case 1.push() 2.pop() 3.show() 4.Exit()
3.call the function inside the cases
4.for push if maximum length is not reached element is added
4.for pop if array is not empty top is decremented
5.for display loop is transversed from top to 0 and printed