Tweet
/* Program to implement stack */
import java.io.*;
public class stack
{
int top=-1;
void push(int a[],int n) throws IOException
{
int val;
System.out.println("Enter value to push : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
val=Integer.parseInt(br.readLine());
if(top < n-1)
a[++top]=val;
else
System.out.println("\nStack overflow.... \n");
}
void pop(int a[])
{
int val;
if(top==-1)
System.out.print("\nStack Underflow...\n");
else
{
val=a[top];
System.out.print("\nDeleted element : " + val);
top--;
}
}
void display(int a[])
{
int i;
if(top==-1)
System.out.print("\nStack Underflow ...\n");
else
{
System.out.print("\nStack : ");
for(i=top;i>=0;i--)
System.out.print(a[i] + " ");
}
}
public void main(String args[]) throws IOException
{
System.out.println("Program to implement stack : \n");
int n,ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter array capacity : ");
n=Integer.parseInt(br.readLine());
int [] a;
a=new int[n];
do
{
System.out.println("[1] Push [2] Pop [3] Display [4] Exit\n");
System.out.print("\nEnter your choice : ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br1.readLine());
switch(ch)
{
case 1:
push(a,n);
display(a);
break;
case 2:
pop(a);
display(a);
break;
case 3:
display(a);
break;
case 4:
break;
default :
System.out.println("Enter correct choice...");
break;
}
}while(ch!=4);
}
}
Stack using array in Java
Posted by
LAHAUL SETH
~
Stack using array in Java
2011-12-07T09:46:00+05:30
LAHAUL SETH
Java
|
Stack Implementation
|
Comments
Stack using array in Java
2011-12-07T09:46:00+05:30
LAHAUL SETH
Java
|
Stack Implementation
|