package stack;
public class Stack {
Stack pre;
int val;
Stack(int val) {
// TODO Auto-generated constructor stub
this.val = val;
}
public void display(){
System.out.println(val + " ");
}
}
//栈的实现
package stack;
public class StackImpl {
Stack top;
int index = 0;
public void Push(int temp){
Stack a = new Stack(temp);
a.pre = top;
top = a;
index++;
}
public void topValue(int temp){
Stack a = new Stack(temp);
top = a;
top.pre = null;
index = 1;
}
public void init(){
Stack top = null;
}
public void delete(){
if(top!=null){
top = top.pre;
index--;
}
else{
System.out.println("out of range");
}
}
public void display(){
for(int i=0;i<index;i++){
System.out.println(top.val);
top = top.pre;
}
}
}
//main函数
package stack;
public class main {
public static void main(String[] args) {
// TODO Auto-generated constructor stub
StackImpl demo = new StackImpl();
demo.init();
demo.topValue(4);
demo.Push(3);
demo.Push(2);
demo.Push(1);
demo.display();
}
}
java实现栈
最新推荐文章于 2024-07-06 13:29:45 发布