在之前我使用了数组实现栈,比较链表和数组的效率会发现:1、在增加,删除,查找某个数据,数组虽然和链表的时间复杂度都是O(N),但是链表不需要有任何移动数据的操作,增加的效率是显著的,特别是复制时间远远大于比较时间的时候。2链表优越的地方是,链表需要多少内存就可以使用多少内存,并可以扩展到所有可用内存。数组的大小在它创建的时候就一定固定了,开辟太多造成浪费和效率低下,太低又容易造成溢出。向量是一个可变数组,但是只允许固定增量扩展(例如快溢出时候就增加一倍数组容量)效率低。
基于抽象数据类型的思想,对于栈的使用者而言,其实不关心栈是用哪种方式实现(数组或者链表),对于它而言基本的操作无非是
push()
{stack[++top]=data};
pop()
{return stack[--top]};
因此编写了如下代码
- package linklist;
- public class Link {
- public int iData; //data item
- public double dData; //data item
- public Link next; //next link in list
- //.....................................................
- public Link(int id,double dd)
- {
- iData=id;
- dData=dd;
- }
- //.....................................................
- public void displayLink()
- {
- System.out.print("{"+iData+","+dData+"}");
- }
- }//end class Link
- package linklist;
- public class LinkList {
- private Link first; //ref to first link on list
- //.....................................................
- public LinkList()
- {
- first=null; //no item yet
- }
- //.....................................................
- public boolean isEmpty() //true if list is empty
- {
- return (first==null);
- }
- //.....................................................
- public void insertFirst(int id,double dd)
- {
- Link newLink=new Link(id,dd);
- newLink.next=first; //newlink-->old first
- first=newLink; //first==newlink
- }
- //.....................................................
- public Link deleteFirst()
- {
- Link newLink=first;
- first=newLink.next;
- newLink.next=null;
- return newLink;
- }
- //.....................................................
- public void displayList()
- {
- System.out.print("List(first-->last):");
- Link Current=first;
- while(Current!=null)
- {
- Current.displayLink();
- Current=Current.next;
- }
- System.out.println("");
- }
- }
- package linklist;
- public class LinkStack
- {
- private LinkList theList;
- //...............................................................
- public LinkStack() //constructor
- {
- theList=new LinkList();
- }
- //...............................................................
- public void push(int id,double dd) //put item onto stack
- {
- theList.insertFirst(id, dd);
- }
- //...............................................................
- public Link pop()
- {
- return theList.deleteFirst();
- }
- //...............................................................
- public boolean isEmpty()
- {
- return (theList.isEmpty());
- }
- //...............................................................
- public void displayStack()
- {
- System.out.print("Stack(top->bottom):");
- theList.displayList();
- }
- //...............................................................
- } //end class LinkStack
- //
应用类实现如下:
- package linklist;
- public class LinkStackApp {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- LinkStack theStack=new LinkStack();
- theStack.push(20, 2.99);
- theStack.push(40, 4.99);
- theStack.displayStack();
- theStack.push(80, 8.99);
- theStack.push(60, 6.99);
- theStack.displayStack();
- theStack.pop();
- theStack.pop();
- theStack.displayStack();
- }
- }
结果如下:
- Stack(top->bottom):List(first-->last):{40,4.99}{20,2.99}
- Stack(top->bottom):List(first-->last):{60,6.99}{80,8.99}{40,4.99}{20,2.99}
- Stack(top->bottom):List(first-->last):{40,4.99}{20,2.99}
一共有3个类,Link,LinKlist,LinkStack,分表为链表节点,链表,和栈,对于使用者而言,只需要和LinkStack打交道就可以了,使用方法和用数组方式无异。