数据结构之栈

来源:互联网 发布:2个excel表格数据比对 编辑:程序博客网 时间:2024/05/21 02:22

数据结构之栈:

package stack;/** * @packagname:stack * @classname:StackTest * @date:2017/1/24 * @author:cullinans * @des:栈的一系列操作 */public class StackList<T> {    public int MAXSIZE;//定义一个栈的值    public T[] data;    public int top;   //构造函数,初始化元素    public StackList(int MAXSIZE, T[] data, int top) {        this.MAXSIZE = MAXSIZE;        this.data = data;        this.top = top;    }//入栈    public int push(StackList<T> st, T data){        if(st.top==st.MAXSIZE-1){            return -1;        }        st.data[top]=data;        st.top++;        return 0;    }    //出栈    public T pop(StackList<T> st,T data){        if(st.top==-1){            return null;        }        data=st.data[st.top];        top--;        return data;    }    //打印栈    public void print(StackList<T> t){        T i=null;       while(t.top!=-1){           System.out.println(t.pop(t,i));       }    }     public static void main(String[] args){        String[] eles=new String[]{"a","b","c","d"};        StackList<String> stacklist=new StackList<String>(4,eles,0);        for(int i=0;i<stacklist.MAXSIZE-1;i++){            stacklist.push(stacklist,eles[i]);        }        stacklist.print(stacklist);    }}
1 0
原创粉丝点击