标签:print val show insert 头插法 head 数据 [] col
/*链式栈 用单链表实现的栈 * * */ package com.tulun; public class TestMl2 { public static void main(String[] args) { // TODO Auto-generated method stub LinkStack link=new LinkStack(); for(int i=0;i<10;i++){ link.insertHead(i); } link.show(); link.pop(); link.show(); int a=link.getTop(); System.out.println(a); } } class LinkStack{ class Entry{ int data; Entry next; public Entry(){ this.data = -1; this.next = null; } public Entry(int data){ this.data=data; this.next=null; } } private Entry head=null; public LinkStack(){ this.head=new Entry(); } //入栈 利用头插法方便一点 public void insertHead(int val){ Entry entry=new Entry(val); entry.next=head.next; head.next=entry; } //出栈 只需出头结点的后的第一个数据节点 public void pop(){ Entry cur=head.next; if(cur==null){ return; } head.next=cur.next; cur=null; return; } //得到栈顶元素 public int getTop(){ return this.head.next.data; } public void show(){ Entry cur=this.head.next; while(cur!=null){ System.out.println(cur.data); cur=cur.next; } System.out.println(); } }
标签:print val show insert 头插法 head 数据 [] col
原文地址:https://www.cnblogs.com/ioio2/p/9011623.html