public class MyStack { private static int length=10; private String[] i=new String[length]; private int count=0; public static void main(String[] args) { MyStack mystack=new MyStack(); mystack.push("a"); mystack.push("b"); mystack.push("c"); mystack.push("d"); mystack.push("e"); mystack.push("f"); mystack.push("g"); System.out.println("添加数据成功"); System.out.println("输出栈内所有数据。。。"); mystack.pop(); System.out.println("输出完毕。。。"); System.out.println(""); System.out.println("栈的长度为:"); int l=mystack.getsize(); System.out.println(l); System.out.println("查看栈顶元素。"); mystack.peek(); } //压入栈 void push(String e) { for(int j=0;j<i.length;j++){ if(i[j]==null){ i[j]=e; break; } } } //弹出栈 void pop(){ for(int j=i.length-1;j>=0;j--){ if(i[j]!=null){ System.out.println(i[j]); } } } //栈长度 int getsize(){ for(int j=i.length-1;j>=0;j--){ if(i[j]!=null){ count++; } } int l=count; count=0; return l; } //查看栈顶数据 void peek(){ for(int j=i.length-1;j>=0;j--){ if(i[j]!=null){ System.out.println(i[j]); break; } } } }
利用数组实现栈的操作
猜你喜欢
转载自qq-24665727.iteye.com/blog/2279181
今日推荐
周排行