顺序栈
采用顺序存储结构的栈
push()、pop()、peek()方法的时间复杂度为O(1),当需要扩充栈容量时push()方法的时间复杂度为O(n)
package com.lzw.demo.StackTest;
public interface SStack<T> { //栈接口,栈抽象数据类型
boolean isEmpty(); //判断是否是空战
void push(T x); //元素 x 入栈
T pop(); //出栈,返回当前栈顶元素
T peek(); //取出栈顶元素,未出栈
}
package com.lzw.demo.StackTest;
/**
* Title: 顺序栈
* Description: 顺序栈类,实现栈接口
* @author Mr Lv
* @date 2011-11-26
*/
public class SeqStack<T> implements SStack<T> {
private Object [] element; //存储栈数据元素的数组
private int top; //栈顶元素的下标
/**
* 构造容量为size的空栈
* @param size
*/
public SeqStack(int size) {
this.element = new Object[Math.abs(size)];//存储栈数据元素的数组
this.top = -1;//栈顶元素下标
}
/**
* 构造默认容量的空栈
*/
public SeqStack() {
this(64);
}
/**
* 判断栈是否为空,若为空返回true
*/
public boolean isEmpty() {
return this.top == -1;
}
/**
* 取出栈顶元素,未出栈,若栈为空返回null
*/
public T peek() {
return this.top == -1 ? null : (T)this.element[this.top];
}
/**
* 出栈,返回栈顶元素,若栈为空返回null
*/
public T pop() {
return this.top == -1 ? null :(T)this.element[this.top--];
}
/**
* 元素x入栈,空对象不能入栈
*/
public void push(T x) {
if (x == null) //空对象不能入栈
return;
if (this.top == element.length-1) { //若栈满,则扩充栈容量
Object [] temp = this.element;
this.element = new Object[temp.length*2];//重新申请一个容量更大的数组
for (int i=0; i<temp.length; i++) {
this.element[i] = temp[i]; //复制数组元素,时间复杂度O(n);
}
}
this.top++;
this.element[this.top] = x;
}
}
package com.lzw.demo.StackTest;
public class lzwCode {
public static void main(String[] args) {
SeqStack<String> stack = new SeqStack<String>();
stack.push("Java");
stack.push("Php");
stack.push("C++");
stack.push("C#");
System.out.println(stack.pop());
System.out.println(stack.peek());
System.out.println(stack.isEmpty());
}
}
控制台信息:
链式栈
采用链式存储结构的栈
push()、pop()、peek()方法的时间复杂度为O(1)package com.lzw.demo.StackTest;
/**
* title : 单链表类
* @author Lv
*
* @param <T>
*/
public class Node<T> {
public T data; //数据域 保存数据元素
public Node<T> next; //地址域 引用后继结点
/**
* 构造结点, data指定数据元素, next指定后继结点
* @param data
* @param next
*/
public Node(T data, Node<T> next) {
this.data = data;
this.next = next;
}
public Node() {
this(null, null);
}
}
package com.lzw.demo.StackTest;
/**
* Title: 链式栈
* Description: 链式栈类,实现栈接口
* @author Mr Lv
* @date 2011-11-26
*/
public class LinkedStack<T> implements SStack<T>{
private Node<T> top; //栈顶结点,单链表结点类
/**
* 构造空栈
*/
public LinkedStack() {
this.top = null;
}
/**
* 判断栈是否为空,若为空返回true
*/
public boolean isEmpty() {
return this.top == null;
}
/**
* 取出栈顶元素,未出栈,若栈为空返回null
*/
public T peek() {
return this.top == null ? null : this.top.data;
}
/**
* 出栈,返回栈顶元素,若栈为空返回null
*/
public T pop() {
if (this.top == null)
return null;
T temp = this.top.data; //取栈顶结点元素
this.top = this.top.next; //删除栈顶结点
return temp;
}
/**
* 元素x入栈,空对象不能入栈
*/
public void push(T x) {
if (x != null)
this.top = new Node(x, this.top);//头插入, x结点作为新的栈顶结点
}
}
package com.lzw.demo.StackTest;
public class lzwCode {
public static void main(String[] args) {
LinkedStack<String> stack = new LinkedStack<String>();
stack.push("Java");
stack.push("Php");
stack.push("C++");
stack.push("C#");
System.out.println(stack.pop());
System.out.println(stack.peek());
System.out.println(stack.isEmpty());
}
}
控制台信息: