Java链栈

来源:互联网 发布:进销存软件免费版 编辑:程序博客网 时间:2024/06/04 18:22
package com.lxm.customDataStructure;public class LinkStack<T>{    class Node<T>{        T data;        Node next;        Node(T data)        {               this.data=data;            this.next = null;        }    }    Node<T> head;    public LinkStack(T data)    {        this.head = null;    }    public boolean isEmpty()    {        return this.head==null;    }    public void push(T e)    {        Node temp = new Node(e);        temp.next = this.head;        this.head = temp;    }    public T pop()    {        if(this.isEmpty()) return null;        Node temp = this.head;        this.head  = temp.next;        return (T) temp.data;    }    public static void main(String[] args)    {        LinkStack<Integer> S = new LinkStack<Integer>(0);        for(int i=0;i<10;i++)        {            S.push(i+1);        }        while(S.isEmpty()==false)        {            System.out.print(S.pop()+"\t");        }        System.out.println();    }}
0 0
原创粉丝点击