No.1:栈的定义
No.2:栈的存储栈(stack)是一种只能在一端进行插入(push)或删除(pop)操作的线性表。其中允许进行插入或删除操作的一端称为栈顶。固定不变的一端是栈底。插入一般称为入栈,删除则称为出栈。栈也被称为先进后出(FILO)表。
No.3:栈的操作栈主要有两种存储结构,顺序栈和链式栈。
No.4:顺序实现栈的主要操作包括入栈、出栈、判断栈空栈满、获取栈顶元素、获取栈中元素数量等。
这里给出了一个Java实现的泛型顺序栈ArrayStack,不足之处,多谢批评指正。
public class ArrayStack<T> { /** * 默认构造函数 */ public ArrayStack() { top = -1; data = (T[]) new Object[size]; } /** * 构造函数 * @param size 初始化栈大小 */ public ArrayStack(int size) { top = -1; data = (T[]) new Object[size]; } /** * 入栈操作 * @param value 入栈数据 */ public void push(T value) { if(isFull()) { throw new ArrayIndexOutOfBoundsException(); } else { data[++top] = value; } } /** * 出栈操作 * @return 出栈数据 */ public T pop() { T temp = null;//这里估计存储基本数据类型的时候会出现问题的 if(isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } else { temp = data[top--]; } return temp; } /** * 获取栈顶数据 * @return 栈顶数据 */ public T getTop() { return data[top]; } /** * 获取栈中存储数据的长度 * @return 栈中存储数据的长度 */ public int size() { return top + 1; } /** * 判断栈空 * @return 若空返回true,否则返回false */ public boolean isEmpty() { return top == -1; } /** * 判断栈满 * @return 若满返回true,否则返回false */ public boolean isFull() { return top == data.length - 1; } /** * 打印栈中数据 * @throws IndexOutOfBoundsException 空栈打印异常 */ public String toString() { if(isEmpty()) { throw new IndexOutOfBoundsException(); } String temp = "["; for(int i = top; i >= 0; --i) { if(i == 0) { temp += data[i].toString() + "]"; } else { temp += data[i].toString() + ", "; } } return temp; } private int top;//栈顶指针 private int size = 100;//默认栈数组大小 private T[] data;//栈数组 /** * 测试主函数 */ public static void main(String[] args) { ArrayStack<Integer> stack = new ArrayStack<Integer>(); for(int i = 0; i < 50; ++i) { stack.push(i + 1); } for(int i = 0; i < 40; ++i) { stack.pop(); } System.out.println(stack.toString() + "--" + stack.size() + "--"); } }
No.5:链式实现
这里给出了一个Java实现的泛型顺序栈LinkedStack,不足之处,多谢批评指正。
public class LinkedStack<T> {//链栈 /** * 默认构造函数 */ public LinkedStack() { top = null; currentSize = 0; } /** * 构造函数 * @param size 栈初始化大小 */ public LinkedStack(int size) { top = null; currentSize = 0; this.size = size; } /** * 入栈 * @param value 指定数据 */ public void push(T value) { if(isFull()) { throw new ArrayIndexOutOfBoundsException(); } if(currentSize == 0) {//栈底结点 top = new Node<T>(value, null); } else { top = new Node<T>(value, top); } ++currentSize; } /** * 出栈 * @return 出栈的数据 */ public T pop() { if(isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } Node<T> temp = top; top = top.next; --currentSize; return temp.data; } /** * 获取栈顶数据 * @return 栈顶数据 */ public T getTop() { return top.data; } /** * 链栈存储数据长度 * @return 链栈存储数据长度 */ public int size() { return currentSize; } /** * 判断栈空 * @return 栈空则返回true,否则返回false */ public boolean isEmpty() { return currentSize == 0 && top == null; } /** * 判断栈满 * @return 栈满则返回true,否则返回false */ public boolean isFull() { return currentSize == size; } /** * 打印链栈中的数据 */ public String toString() { if(isEmpty()) { throw new IndexOutOfBoundsException(); } String temp = "["; Node<T> p = top; for(int i = 0; i < currentSize; ++i) { if(i == currentSize - 1) { temp += p.data.toString() + "]"; } else { temp += p.data.toString() + ", "; } p = p.next; } return temp; } private Node<T> top;//栈顶指针 private int currentSize;//栈中存储数据长度 private int size = 100;//栈的初始大小 private class Node<T> {//结点 public Node(T data, Node<T> next) { this.data = data; this.next = next; } private T data;//数据域 private Node<T> next;//指针域 } public static void main(String[] args) { LinkedStack<String> stack = new LinkedStack<String>(); System.out.println("栈空?[" + stack.isEmpty() + ", " + stack.size() + "]"); for(int i = 0; i < 100; ++i) { stack.push(i + 1 + ""); } System.out.println("栈满?[" + stack.isFull() + ", " + stack.size() + "]"); for(int i = 0; i < 90; ++i) { stack.pop(); } System.out.println("[" + stack.isEmpty() + ", " + stack.size() + "]"); System.out.println(stack.toString() + "," + stack.size()); } }