顺序栈和链式栈


顺序栈

采用顺序存储结构的栈

push()、pop()、peek()方法的时间复杂度为O(1),当需要扩充栈容量时push()方法的时间复杂度为O(n)

[java]  view plain copy print ?
  1. package com.lzw.demo.StackTest;  
  2.   
  3. public interface SStack<T> {    //栈接口,栈抽象数据类型  
  4.     boolean isEmpty();          //判断是否是空战  
  5.     void push(T x);             //元素 x 入栈  
  6.     T pop();                    //出栈,返回当前栈顶元素  
  7.     T peek();                   //取出栈顶元素,未出栈  
  8. }  

[java]  view plain copy print ?
  1. package com.lzw.demo.StackTest;  
  2.   
  3. /**  
  4.  * Title: 顺序栈 
  5.  * Description: 顺序栈类,实现栈接口 
  6.  * @author Mr Lv  
  7.  * @date 2011-11-26  
  8.  */  
  9. public class SeqStack<T> implements SStack<T> {  
  10.       
  11.     private Object [] element; //存储栈数据元素的数组  
  12.     private int top;          //栈顶元素的下标  
  13.       
  14.     /** 
  15.      * 构造容量为size的空栈 
  16.      * @param size 
  17.      */  
  18.     public SeqStack(int size) {  
  19.         this.element = new Object[Math.abs(size)];//存储栈数据元素的数组  
  20.         this.top = -1;//栈顶元素下标  
  21.     }  
  22.       
  23.     /** 
  24.      * 构造默认容量的空栈 
  25.      */  
  26.     public SeqStack() {  
  27.         this(64);  
  28.     }  
  29.       
  30.     /** 
  31.      * 判断栈是否为空,若为空返回true 
  32.      */  
  33.     public boolean isEmpty() {  
  34.         return this.top == -1;  
  35.     }  
  36.       
  37.     /** 
  38.      * 取出栈顶元素,未出栈,若栈为空返回null 
  39.      */  
  40.     public T peek() {  
  41.         return this.top == -1 ? null : (T)this.element[this.top];  
  42.     }  
  43.       
  44.     /** 
  45.      * 出栈,返回栈顶元素,若栈为空返回null 
  46.      */  
  47.     public T pop() {  
  48.         return this.top == -1 ? null :(T)this.element[this.top--];  
  49.     }  
  50.       
  51.     /** 
  52.      * 元素x入栈,空对象不能入栈 
  53.      */  
  54.     public void push(T x) {  
  55.         if (x == null//空对象不能入栈  
  56.             return;  
  57.         if (this.top == element.length-1) { //若栈满,则扩充栈容量  
  58.             Object [] temp = this.element;  
  59.             this.element = new Object[temp.length*2];//重新申请一个容量更大的数组  
  60.             for (int i=0; i<temp.length; i++) {  
  61.                 this.element[i] = temp[i]; //复制数组元素,时间复杂度O(n);  
  62.             }  
  63.         }  
  64.         this.top++;  
  65.         this.element[this.top] = x;  
  66.     }  
  67.   
  68. }  

[java]  view plain copy print ?
  1. package com.lzw.demo.StackTest;  
  2.   
  3. public class lzwCode {  
  4.   
  5.     public static void main(String[] args) {  
  6.         SeqStack<String> stack = new SeqStack<String>();  
  7.         stack.push("Java");  
  8.         stack.push("Php");  
  9.         stack.push("C++");  
  10.         stack.push("C#");  
  11.         System.out.println(stack.pop());  
  12.         System.out.println(stack.peek());  
  13.         System.out.println(stack.isEmpty());  
  14.     }  
  15. }  

控制台信息:



链式栈

采用链式存储结构的栈

push()、pop()、peek()方法的时间复杂度为O(1)

[java]  view plain copy print ?
  1. package com.lzw.demo.StackTest;  
  2. /** 
  3.  * title : 单链表类 
  4.  * @author Lv 
  5.  * 
  6.  * @param <T> 
  7.  */  
  8. public class Node<T> {  
  9.       
  10.     public T data;         //数据域  保存数据元素  
  11.     public Node<T> next;   //地址域  引用后继结点  
  12.       
  13.     /** 
  14.      * 构造结点, data指定数据元素, next指定后继结点 
  15.      * @param data 
  16.      * @param next 
  17.      */  
  18.     public Node(T data, Node<T> next) {  
  19.         this.data = data;  
  20.         this.next = next;  
  21.     }  
  22.       
  23.     public Node() {  
  24.         this(nullnull);  
  25.     }  
  26. }  

[java]  view plain copy print ?
  1. package com.lzw.demo.StackTest;  
  2. /**  
  3.  * Title: 链式栈 
  4.  * Description: 链式栈类,实现栈接口 
  5.  * @author Mr Lv  
  6.  * @date 2011-11-26  
  7.  */  
  8. public class LinkedStack<T> implements SStack<T>{  
  9.       
  10.     private Node<T> top; //栈顶结点,单链表结点类  
  11.       
  12.     /** 
  13.      * 构造空栈 
  14.      */  
  15.     public LinkedStack() {  
  16.         this.top = null;  
  17.     }  
  18.       
  19.     /** 
  20.      * 判断栈是否为空,若为空返回true 
  21.      */  
  22.     public boolean isEmpty() {  
  23.         return this.top == null;  
  24.     }  
  25.       
  26.     /** 
  27.      * 取出栈顶元素,未出栈,若栈为空返回null 
  28.      */  
  29.     public T peek() {  
  30.         return this.top == null ? null : this.top.data;  
  31.     }  
  32.       
  33.     /** 
  34.      * 出栈,返回栈顶元素,若栈为空返回null 
  35.      */  
  36.     public T pop() {  
  37.         if (this.top == null)  
  38.             return null;  
  39.         T temp = this.top.data;   //取栈顶结点元素  
  40.         this.top = this.top.next; //删除栈顶结点  
  41.         return temp;  
  42.     }  
  43.       
  44.     /** 
  45.      * 元素x入栈,空对象不能入栈 
  46.      */  
  47.     public void push(T x) {  
  48.         if (x != null)   
  49.             this.top = new Node(x, this.top);//头插入, x结点作为新的栈顶结点  
  50.     }  
  51.   
  52. }  

[java]  view plain copy print ?
  1. package com.lzw.demo.StackTest;  
  2.   
  3. public class lzwCode {  
  4.   
  5.     public static void main(String[] args) {  
  6.         LinkedStack<String> stack = new LinkedStack<String>();  
  7.         stack.push("Java");  
  8.         stack.push("Php");  
  9.         stack.push("C++");  
  10.         stack.push("C#");  
  11.         System.out.println(stack.pop());  
  12.         System.out.println(stack.peek());  
  13.         System.out.println(stack.isEmpty());  
  14.     }  
  15. }  

控制台信息:


原文出自: http://blog.csdn.net/lzwjavaphp/article/details/7015333
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值