栈的简单实现

来源:互联网 发布:java可以javac不可以 编辑:程序博客网 时间:2024/05/17 01:09
<span style="font-size:18px;">/** * 栈的简单实现 * @author xxxu * */public class Stack {//栈的结点的个数private int N=0;//头结点private Node first;//定义一个节点private class Node{int item;Node next;}//判断栈是否为空public boolean isEmpty(){if(N==0){return true;}else{return false;}}//栈的大小public int size(){return N;}//压栈public void push(int item){//一开始first是nullNode oldFirst=first;first=new Node();first.item=item;first.next=oldFirst;N++;}//弹出栈public int pop(){int item=first.item;first=first.next;N--;return item;}//遍历栈public void iterator(){Node x=first;while(x!=null){System.out.print(x.item+" ");x=x.next;}System.out.println();}}</span>


0 0
原创粉丝点击