栈的理解和代码实现(java)

从数据结构的角度来看,其实栈也是线性表。特殊性在于栈和队列的基本操作是线性表操作的子集,栈是操作受限制的线性表。

栈的定义

栈是限定仅在表尾进行插入或者删除操作的线性表。对于一个栈来说,表尾端有着特殊的含义,称为栈顶,表头端称为栈底,不含元素的空表称之为空栈,栈又称为后进先出的线性表,简称 LIFO(Last In First Out)结构。也就是说后存放的先取,先存放的后取,这就类似于我们要在取放在箱子底部的东西(放进去比较早的物体),我们首先要移开压在它上面的物体(放进去比较晚的物体)。

栈的理解和代码实现(java)

和线性表类似,栈已有两种存储表示方法,分别称之为顺序栈和链栈。

顺序栈的实现:

顺序栈是指利用顺序存储结构实现的栈,即利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设top指示栈顶元素在顺序栈中的位置。

实现栈功能的泛型类:

package 顺序栈; import java.lang.reflect.Array; import java.util.Arrays; public abstract class OrderStack<T> { private int maxSize; protected T arr[]; private int top; public OrderStack(Class<T> componentType) { this.maxSize = 1024; this.arr = (T[]) Array.newInstance(componentType, this.maxSize); this.top = 0; } public OrderStack(Class<T> componentType, int maxSize) { this.maxSize = maxSize; } public int getSize() { return top; } public int getMaxSize() { return maxSize; } public void expandMaxSize(int maxSize) { Arrays.copyOf(arr, this.maxSize + maxSize); this.maxSize += maxSize; } public void push(T data) { if(this.top > this.maxSize) { throw new IllegalArgumentException("new element will be out of limits."); } arr[this.top] = data; this.top++; } public T pop() { if(this.top == 0) { throw new IllegalArgumentException("this stack is already empty"); } this.top--; return arr[this.top]; } public T peek() { if(this.top == 0) { throw new IllegalArgumentException("this stack is already empty"); } return arr[this.top - 1]; } public boolean isEmpty() { return this.top == 0 ? true : false; } public boolean isFull() { return this.top == this.maxSize ? true : false; } }

Integer实现类:

package 顺序栈; public class IntegerStack extends OrderStack<Integer> { public IntegerStack() { super(Integer.class); // TODO Auto-generated constructor stub } public IntegerStack(int maxSize) { super(Integer.class, maxSize); } public void print() { System.out.println("top"); System.out.println("------"); for (int i = this.getSize() - 1; i >= 0; i--) { System.out.println(arr[i]); } System.out.println("------"); System.out.println("bottom"); } }

测试类:

package 顺序栈; public class IntegerStackDemo { public static void main(String[] args) { IntegerStack test = new IntegerStack(); System.out.println("size is: " + test.getSize()); System.out.println("is empty: " + test.isEmpty()); test.print(); System.out.println("==============="); test.push(2); test.push(5); test.push(7); test.push(8); System.out.println("size is: " + test.getSize()); System.out.println("is empty: " + test.isEmpty()); test.print(); System.out.println("================"); System.out.println("first element is: " + test.peek()); test.pop(); System.out.println("first element is: " + test.peek()); System.out.println("size is: " + test.getSize()); test.print(); System.out.println("================"); } }

链栈的实现

链栈是采用链式存储结构实现的栈,通常链栈采用单链表来实现,由于栈的主要操作是在栈顶进行插入和删除,所以以链表的头部作为栈顶最为方便,且没必要向单链表那样为了操作方便而加一个头结点

链栈类代码:

package 链栈; public class LinkStack { private Element top; private Element base; class Element { public Object data; public Element next; public Element() { this.data = null; this.next = null; } public Element(Object data, Element next) { this.data = data; this.next = next; } } public void initStack() { top = new Element(); base = new Element(); top.next = base; } public void push(Object e) { Element ele = new Element(e, null); ele.next = top.next; top.next = ele; } public void pop() { if(top.next == base) { System.out.println("栈中没有元素"); } else { Element e = top.next; System.out.println("出栈操作:" + e.data); top.next = top.next.next; e = null; } } public Object getTop() { if(top.next == base) System.out.println("栈中没有元素"); return top.next.data; } public boolean isEmpty() { return top.next == base ? true : false; } public void printStack() { System.out.println("打印栈"); Element ele = top; while(ele.next != base) { System.out.println(ele.next.data); ele = ele.next; } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwsfxf.html