基于java的数据结构学习手记6--栈的单链表实现法

     在之前我使用了数组实现栈,比较链表和数组的效率会发现:1、在增加,删除,查找某个数据,数组虽然和链表的时间复杂度都是O(N),但是链表不需要有任何移动数据的操作,增加的效率是显著的,特别是复制时间远远大于比较时间的时候。2链表优越的地方是,链表需要多少内存就可以使用多少内存,并可以扩展到所有可用内存。数组的大小在它创建的时候就一定固定了,开辟太多造成浪费和效率低下,太低又容易造成溢出。向量是一个可变数组,但是只允许固定增量扩展(例如快溢出时候就增加一倍数组容量)效率低。

    基于抽象数据类型的思想,对于栈的使用者而言,其实不关心栈是用哪种方式实现(数组或者链表),对于它而言基本的操作无非是

push()

{stack[++top]=data};

pop()

{return stack[--top]};

因此编写了如下代码

Code:
  1. package linklist;  
  2.   
  3. public class Link {  
  4.     public int iData;      //data item  
  5.     public double dData;    //data item  
  6.     public Link next;      //next link in list  
  7. //.....................................................  
  8.     public Link(int id,double dd)  
  9.     {  
  10.         iData=id;  
  11.         dData=dd;     
  12.     }  
  13. //.....................................................   
  14. public void displayLink()  
  15. {  
  16.     System.out.print("{"+iData+","+dData+"}");    
  17. }  
  18. }//end class Link  
Code:
  1. package linklist;  
  2.   
  3. public class LinkList {  
  4.     private Link first;     //ref to first link on list  
  5. //.....................................................  
  6.     public LinkList()  
  7.     {  
  8.         first=null;  //no item yet  
  9.     }  
  10. //.....................................................  
  11.     public boolean isEmpty()  //true if list is empty  
  12.     {  
  13.         return (first==null);  
  14.     }  
  15. //.....................................................    
  16.     public void insertFirst(int id,double dd)  
  17.     {  
  18.         Link newLink=new Link(id,dd);  
  19.         newLink.next=first;  //newlink-->old first  
  20.         first=newLink;       //first==newlink  
  21.     }  
  22. //.....................................................  
  23.     public Link deleteFirst()  
  24.     {  
  25.         Link newLink=first;  
  26.         first=newLink.next;  
  27.         newLink.next=null;  
  28.         return newLink;  
  29.     }  
  30. //.....................................................  
  31.      public void displayList()  
  32.      {  
  33.          System.out.print("List(first-->last):");  
  34.          Link Current=first;  
  35.          while(Current!=null)  
  36.          {  
  37.              Current.displayLink();  
  38.              Current=Current.next;  
  39.                
  40.          }  
  41.          System.out.println("");  
  42.      }  
  43.   
  44. }  
Code:
  1. package linklist;  
  2.   
  3. public class LinkStack   
  4. {  
  5.     private LinkList theList;  
  6. //...............................................................  
  7.     public LinkStack()              //constructor  
  8.     {  
  9.         theList=new LinkList();  
  10.     }  
  11. //...............................................................  
  12.     public void push(int id,double dd) //put item onto stack  
  13.     {  
  14.         theList.insertFirst(id, dd);  
  15.     }  
  16. //...............................................................  
  17.     public Link pop()  
  18.     {  
  19.         return theList.deleteFirst();  
  20.     }  
  21. //...............................................................     
  22.     public boolean isEmpty()  
  23.     {  
  24.         return (theList.isEmpty());  
  25.     }  
  26. //...............................................................     
  27.     public void displayStack()  
  28.     {  
  29.         System.out.print("Stack(top->bottom):");  
  30.         theList.displayList();  
  31.     }  
  32. //...............................................................  
  33. }  //end class LinkStack  
  34. //  

应用类实现如下:

Code:
  1. package linklist;  
  2.   
  3. public class LinkStackApp {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.         LinkStack theStack=new LinkStack();  
  11.         theStack.push(202.99);  
  12.         theStack.push(404.99);  
  13.           
  14.         theStack.displayStack();  
  15.           
  16.         theStack.push(808.99);  
  17.         theStack.push(606.99);  
  18.           
  19.         theStack.displayStack();  
  20.           
  21.         theStack.pop();  
  22.         theStack.pop();  
  23.           
  24.         theStack.displayStack();  
  25.     }  
  26.   
  27. }  

结果如下:

Code:
  1. Stack(top->bottom):List(first-->last):{40,4.99}{20,2.99}  
  2. Stack(top->bottom):List(first-->last):{60,6.99}{80,8.99}{40,4.99}{20,2.99}  
  3. Stack(top->bottom):List(first-->last):{40,4.99}{20,2.99}  

 

 

一共有3个类,Link,LinKlist,LinkStack,分表为链表节点,链表,和栈,对于使用者而言,只需要和LinkStack打交道就可以了,使用方法和用数组方式无异。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值