【算法】【JAVA】栈的实现

 

 

 1 class Node{
 2     private int data;
 3     private Node previous;
 4     public Node(){}
 5     public int getData(){
 6         return data;
 7     }
 8     public void setData(int data){
 9         this.data=data;
10     }
11     public Node getPrevious(){
12         return previous;
13     }
14     public void setPrevious(Node previous){
15         this.previous=previous;
16     }
17 }
18 class Stack1{
19     private Node top;
20     public Stack1(int data){
21         top = new Node();
22         top.setData(data);
23         top.setPrevious(null);
24     }
25     
26     public void push(int data){
27         Node temp = new Node();
28         temp.setData(data);
29         temp.setPrevious(top);
30         top = temp;
31     }
32     public int pop(){
33         int popnum=0;
34         popnum = top.getData();
35         top=top.getPrevious();
36         return popnum;
37     }
38     public boolean isempty(){
39         if( top==null) return false;
40         else return true;
41     }
42 }
43 public class Stack {
44     public static void main(String[] args){
45         Stack1 s = new Stack1(0);
46         for(int i=0;i<10;i++){
47             s.push(i);
48         }
49         while(!s.isempty()){
50             System.out.println(s.pop()+" ");
51         }
52     }
53 
54 }
posted @ 2012-09-15 20:35  Sayary  阅读(141)  评论(0)    收藏  举报