import java.io.*;
import java.util.Scanner;
public class MyStack
{
private long[] StackArray;
private int maxSize;
private int top;
MyStack(int s){
maxSize=s;
top=-1;
StackArray = new long[maxSize];
}
public void push(long j){
if(isFull()){
System.out.println("Stack Is Full");
}
StackArray[++top]=j;
}
public void pop(){
if(isEmpty()){
System.out.println("Stack is Empty");
return;
}
System.out.println(StackArray[top]);
top=top-1;
}
public void peek(){
if(isEmpty()){
System.out.println("Stack is Empty");
return;
}
System.out.println(StackArray[top]);
}
public boolean isEmpty(){
if(top==-1)
return true;
else
return false;
}
public boolean isFull(){
return (top==maxSize-1);
}
public static void main(String a[]) throws Exception
{
System.out.println("Welcome to the MyStack project!");
MyStack stack = new MyStack(10);
Scanner input = new Scanner(System.in);
int choice=0;
long element=0;
while(choice!=5){
System.out.print("Enter your Choice \n 1. Push the element \n 2. pop the element 3. get the top element\n 5. To quit the program\n");
choice = input.nextInt();
System.out.println();
switch(choice){
case 1:
element= input.nextLong();
stack.push(element);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 5:
System.exit(0);
default:
System.out.println("Wrong Input ! Re-Enter!!");
}
}
}
}
import java.util.Scanner;
public class MyStack
{
private long[] StackArray;
private int maxSize;
private int top;
MyStack(int s){
maxSize=s;
top=-1;
StackArray = new long[maxSize];
}
public void push(long j){
if(isFull()){
System.out.println("Stack Is Full");
}
StackArray[++top]=j;
}
public void pop(){
if(isEmpty()){
System.out.println("Stack is Empty");
return;
}
System.out.println(StackArray[top]);
top=top-1;
}
public void peek(){
if(isEmpty()){
System.out.println("Stack is Empty");
return;
}
System.out.println(StackArray[top]);
}
public boolean isEmpty(){
if(top==-1)
return true;
else
return false;
}
public boolean isFull(){
return (top==maxSize-1);
}
public static void main(String a[]) throws Exception
{
System.out.println("Welcome to the MyStack project!");
MyStack stack = new MyStack(10);
Scanner input = new Scanner(System.in);
int choice=0;
long element=0;
while(choice!=5){
System.out.print("Enter your Choice \n 1. Push the element \n 2. pop the element 3. get the top element\n 5. To quit the program\n");
choice = input.nextInt();
System.out.println();
switch(choice){
case 1:
element= input.nextLong();
stack.push(element);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 5:
System.exit(0);
default:
System.out.println("Wrong Input ! Re-Enter!!");
}
}
}
}
Comments
Post a Comment