stackarray.java
来自「many simple java programs like classic a」· Java 代码 · 共 81 行
JAVA
81 行
import java.io.*;
import java.util.Scanner;
class stack
{
int a[],top,count;
public stack(int x)
{
a=new int[x];
top=-1;
count=0;
}
public void push(int x)
{
if(top>a.length)
System.out.println("Stack overflow!");
else
{
top++;
a[top]=x;
count++;
}
}
public void pop()
{
if(top>-1)
{
System.out.println("Element "+a[top]+" deleted");
top--;
count--;
}
else
System.out.println("Stack under flow!");
}
public void status()
{
System.out.println("Stack contains "+count+" elements\nThey are:");
for(int i=top;i>-1;i--)
System.out.println(a[i]);
}
}
public class stackarray
{
public static void main(String args[])
{
int ch;
Scanner s=new Scanner(System.in);
System.out.print("Enter size of stack:");
stack st=new stack(s.nextInt());
do
{
System.out.println("Choose and option: \n1. PUSH\n2. POP\n3. STATUS\n4. EXIT");
System.out.print("\n\nEnter ur choice:");
ch=s.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter the value to be entered :");
st.push(s.nextInt());
break;
case 2:
st.pop();
break;
case 3:
st.status();
break;
case 4:
break;
default:
System.out.println("Invalid choice!");
}
}
while(ch!=4);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?