Stack实现

链表式:插入头部构造链表

代码如下:

  1. package nuaa.ds;  
  2.   
  3. import java.util.NoSuchElementException;  
  4.   
  5. //插入头部的链表实现Stack  
  6. public class LinkedStack<T> {  
  7.     private Node<T> top;  
  8.   
  9.     private int size;//current size  
  10.     public void push(T t){  
  11.         if(null==t){  
  12.             throw new IllegalArgumentException();  
  13.         }  
  14.         if(size==0){  
  15.             top = new Node<T>(t);  
  16.             size++;  
  17.         }else{  
  18.             Node<T> p = new Node<T>(t);  
  19.             p.next = top;  
  20.             top = p;  
  21.             size++;  
  22.         }  
  23.           
  24.     }  
  25.       
  26.     public T pop(){  
  27.         if(size==0){  
  28.             throw new NoSuchElementException();  
  29.         }  
  30.         Node<T> p = top;  
  31.         top = top.next;  
  32.         size--;  
  33.         return p.t;  
  34.     }  
  35.       
  36.     public LinkedStack(){  
  37.         this.size = 0;  
  38.     }  
  39.     public int size(){  
  40.         return this.size;  
  41.     }  
  42.     public boolean isEmpty(){  
  43.         return size==0 ? true : false;  
  44.     }  
  45.     class Node<U>{  
  46.         U t;  
  47.         Node<U> next;  
  48.         public Node(U t) {  
  49.             super();  
  50.             this.t = t;  
  51.         }  
  52.           
  53.     }  
  54. }  

动态数组式:每次满了就double size,没什么含金量

代码如下:

  1. package nuaa.ds;  
  2.   
  3. public class Stack<T>{  
  4.     //top指示Stack最上面元素  
  5.     //Stack顶可以是数组最低项也可以是最高项  
  6.     //集合类存放的是引用或者原始类型  
  7.     //链表实现一定得头插入  
  8.     //括号匹配,左入栈,右对比然后出栈  
  9.     private int size;//总大小  
  10.     private int top;//栈顶下标  
  11.     private Object[] arrays;   
  12.       
  13.     public boolean push(T a){  
  14.         if(this.top==this.size-1){  
  15.             this.size = this.size*2;  
  16.             Object[] orignal = arrays;  
  17.             arrays = new Object[this.size];  
  18.             for(int i=0;i<size/2;i++)  
  19.                 arrays[i] = orignal[i];  
  20.             }  
  21.         top = top+1;  
  22.         arrays[top] = a;  
  23.         return true;  
  24.         }  
  25.       
  26.     public T pop(){  
  27.         if(top==-1)  
  28.             return null;  
  29.             top = top-1;  
  30.         return (T)arrays[top+1];  
  31.         }  
  32.     public T peek(){  
  33.         if(top==-1)  
  34.             return null;  
  35.           
  36.         return (T)arrays[top+1];  
  37.         }  
  38.     public boolean isEmpty(){  
  39.         return top==-1 ? true : false;  
  40.     }  
  41.     public Stack(){  
  42.         this(2);  
  43.         }  
  44.     public  Stack(int size){  
  45.         this.size = size;  
  46.         this.top = -1;  
  47.         arrays =  new Object[this.size];  
  48.         }  
  49.     public int size(){  
  50.         return this.top+1;  
  51.         }  
  52.       
  53.     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值