In: Computer Science
please follow this pseudocode
1) Create class (any name you want)ex: aS
2) Declaring private integer array empty one ex: name arr[]
3) Declare private integer variables top, size and len (my
example)
4) Declare constructor for the class with the same name and
inside of that
public aS declare (parameter ex: int n)
5) Inside of that constructor method write command where:
size is equal to n
len is equal to zero
array (name) is equal to new integer[size]
and
top is equal minus 1
6)
Declare function to check if Stack is empty
In this function called public you should declare boolean (any name
you want)
Which return top is equal minus 1
7)
Declare function to check if stack is full
Again another public boolean (any name you want)
Which return top is equal to size minus 1
8)
Declare function to get the size of the stack
This is public integer (any name you want)
Which return len
9)
Declare function to check the top element of the stack.
This is public integer peek
Which calls the function to see if is empty (we created
above)
You can write underneath
throw new NoSuchElementException("Underflow Exception");
then
return name of your array[top]
10)
Declare function to add an element to the stack
Called public void push with parameters integer i
Condition:
if top + 1 is greater equal to size then you can write again
throw new NoSuchElementException("Underflow Exception");
if top + 1 is less equal to size
name of your array [increment top] equal to i
len increment
11)
Declare function to delete an element from the stack
This is public integer pop
Which calls the function to see if is empty (we created
above)
You can write underneath
throw new NoSuchElementException("Underflow Exception");
then
increment len
then
return name of array[decrement top]
12)
Declare function to display the stack
This is public void display
system print out Stack: =
Condition
if len is equal to zero
then
system print out empty
return only
close the brackets
then
create for loop where int i is equal to top and i is greater equal
to zero and i decrement
inside of that loop
system print name of array[i]+" "
13)
Scanner scan = new Scanner(System.in);
System.out.println("Enter Size of Stack ");
int n = scan.nextInt();
aS s = new aS(n);
char ch;
do{
System.out.println("\nStack Operations");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Check if is empty");
System.out.println("5. Check if is full");
System.out.println("6. Get the Size of Stack”);
int choice = scan.nextInt();
switch (choice)
// java
// if you have any problem let me know i will try to help you. Thank you.
import java.util.NoSuchElementException;
import java.util.Scanner;
class aS{
private int arr[];
private int top;
private int size;
private int len=0;
public aS(int n)
{
size=n;
len=0;
arr=new int[size];
top=-1;
}
public boolean isEmpty()
{
return top==-1;
}
public boolean isFull()
{
return top==size-1;
}
public int size()
{
return len;
}
public int peek()
{
if(isEmpty())
throw new NoSuchElementException("Underflow Exception");
return arr[top];
}
public void push(int value)
{
if(isFull())
throw new NoSuchElementException("Underflow Exception");
top++;
arr[top]=value;
len++;
}
public int pop()
{
if(isEmpty())
throw new NoSuchElementException("Overflow Exception");
int temp= arr[top];
top--;
len--;
return temp;
}
public void display()
{
System.out.print("[");
for(int i=top;i>=0;i--)
{
System.out.print(arr[i]+" ");
}
System.out.println("]");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Size of Stack ");
int n = sc.nextInt();
aS s = new aS(n);
char ch='n';
do{
System.out.println("\nStack Operations");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Check if is empty");
System.out.println("5. Check if is full");
System.out.println("6. Get the Size of Stack");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the value to push: ");
int val=sc.nextInt();
s.push(val);
s.display();
break;
case 2:
s.pop();
s.display();
break;
case 3:
System.out.println("Peek element is : "+s.peek());
break;
case 4:
System.out.println("Is stack empty?"+s.isEmpty());
break;
case 5:
System.out.println("Is stack full?"+s.isFull());
break;
case 6:
System.out.println("size of stack: "+s.size());
break;
}
System.out.print("Do you want to perform more operation(y/n): ");
ch=sc.next().charAt(0);
}while(ch=='y'||ch=='Y');
}
}