Stack Data structure algorithm theory and program in java

0

Stack Data structure algorithm theory and program in java

Stack Data structure program in java



 What Is It?


A stack according to our definition, is a list in which all additions and deletion are made at one end. The operation is started on the element that is pushed into the order first.


In Java, a stack is a data structure that follows the Last In First Out (LIFO) principle. It is implemented as a class, with methods such as push (to add an element to the top of the stack), pop (to remove the top element), and peek (to view the top element without removing it). The Java Collection Framework provides a Stack class, which is a legacy class and is not recommended for use in new code. Instead, the ArrayDeque class or the LinkedList class should be used.

Stack Operations:


DisplayStack()

Push()

Pop()

Peek()

isEmpty()

isFull()

ClearStack()



Stack Program in java:


package stck;

import java.util.Scanner;

class Newstck 

{

Scanner objt= new Scanner(System.in);

static int top=-1;

int[] arr;

public void creatstck()

{

System.out.println("Enter Length of Stack");

int length=objt.nextInt();

arr=new int[length];

}

public void push(int val)

{

top++;

arr[top]=val;

}

public void pop()

{

System.out.println("Poped value is" + arr[top--]);

}

public void peek()

{

System.out.println("Top value is"+ arr[top]);

}

public void displaystack()

{

System.out.println("Your entered values are");

{

for(int i=top;i>=0;i--)

{

System.out.println(arr[i]);

}

}

}

public boolean isEmpty()

{

if(arr[top+1] == 0)

return true;

else

return false;

}

public boolean isFull()

{

if(arr[top] == arr[top])

return true;

else

return false;

}

public void clearstck()

{

for(int i=top; i>=0;i--)

{

arr[i]=0;

}

System.out.println("Stack is cleared");

}

public void displayMenu()

{

System.out.println("Choose any of the following:");

System.out.println("1...................Create Stack");

System.out.println("2...................Display Stack");

System.out.println("3...................PUSH");

System.out.println("4...................POP");

System.out.println("5...................TOP");

System.out.println("6...................isEmpty");

System.out.println("7...................isFull");

System.out.println("8...................Clear Stack");

}

}




Main class:


package stck;

import java.util.Scanner;

public class maincls 

{

public static void main(String[] args)

{

Newstck obj;

obj= new Newstck();

int choice=0;

while(true)

{

obj.displayMenu();

Scanner ob= new Scanner(System.in);

choice=ob.nextInt();

switch(choice)

{

case 1:

obj.creatstck();

break;

case 2:

obj.displaystack();

break;

case 3:

System.out.println("Enter value to push");

int n;

n=ob.nextInt();

obj.push(n);

break;

case 4:

obj.pop();

break;

case 5:

obj.peek();

break;

case 6:

System.out.println(obj.isEmpty());

break;

case 7:

System.out.println(obj.isFull());

break;

case 8:

obj.clearstck();

break;

}

}

}

}


Post a Comment

0Comments
Post a Comment (0)