(堆)栈
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
(堆)棧概述
棧是一種特殊的線性表,是操作受限的線性表
棧的定義和特點(diǎn) ?定義:限定僅在表尾進(jìn)行插入或刪除操作的線性表,表尾—棧頂,表頭—棧底,不含元素的空表稱空棧 ?特點(diǎn):先進(jìn)后出(FILO)或后進(jìn)先出(LIFO)棧的結(jié)構(gòu)
如下圖所示:線性表的操作主要包括:
(1)清空(堆)棧
(2)判斷是否為空
(3)元素的個(gè)數(shù)
(4)入棧
(5)出棧
(6)取棧頂元素
接口
由此,對(duì)隊(duì)列的抽象數(shù)據(jù)類型定義Queue接口如下:package stack; /*** (堆)棧* @author Administrator**/ public interface Stack {/*** 清空堆棧*/public void clear();/*** 入棧* @param obj 入棧的元素*/public void push(Object obj);/*** 出棧* @return 出棧的結(jié)果*/public Object pop();/*** 判斷是否為空* @return*/public boolean isEmpty();/*** 求元素的個(gè)數(shù)* @return 元素的個(gè)數(shù)*/public int size();/*** 取棧頂元素* @return 棧頂元素*/public Object peek();}
順序(堆)棧
結(jié)構(gòu)模型
棧頂指針top,指向?qū)嶋H棧頂后的空位置,初值為0。
棧的初始空間大小為M
top=0,棧空,此時(shí)出棧,則下溢(underflow);
top=M,棧滿,此時(shí)入棧,則上溢(overflow);
源代碼
package stack; /*** 順序(堆)棧* @author Administrator**/ public class ArrayStack implements Stack{private static int DEFAULT_SIZE = 100; private int Top;Object array[];public ArrayStack() {Top = 0;array = new Object[DEFAULT_SIZE];}public boolean isEmpty() {return 0 == Top ;}public void expand() {Object[] newArray = new Object[2 * array.length];for(int i=0; i<array.length; i++) {newArray[i] = array[i];}array = newArray;}/*public void expand() {try {Object[] newArray = new Object[2*DEFAULT_SIZE];for(int i=0; i<array.length; i++) {newArray[i] = array[i];}array = newArray;}catch(OutOfMemoryError e) {System.out.println("error in expand of Stack class!");//e.printStackTrace();}DEFAULT_SIZE = 2*DEFAULT_SIZE;}*/public void push(Object obj) {if(Top == array.length) {expand();} array[Top] =obj;Top ++;}public Object pop() {if(0 == Top) throw new IllegalStateException();Object val = array[-- Top];array[Top] = null;return val;}public void clear() {for(int i=0; i<array.length; i++) {array[i] = null;Top = 0;}}public Object peek() {if(0 == Top) throw new IllegalStateException();return array[Top - 1];}public int size() {return Top;}public String toString() {String s = "[";for(int i=Top-1; i>=0 ; i--) {s = s + array[i];s = s + ", ";}s = s + "]";return s;}}鏈?zhǔn)?堆)棧
結(jié)構(gòu)模型
源代碼
package stack;/*** 鏈?zhǔn)?堆)棧的結(jié)點(diǎn)* @author luoweifu**/ class Node{Object data; //數(shù)據(jù)元素Node next; //后驅(qū)結(jié)點(diǎn)public Node() {this(null);}public Node(Object data) {this.data = data;this.next = null;} }/*** 鏈?zhǔn)?堆)棧, 無(wú)頭結(jié)點(diǎn)* @author Administrator**/ public class LinkStack implements Stack {private Node top; //棧頂指針private int size; //棧的大小public LinkStack() {top = null;size = 0;}@Overridepublic void clear() {top = null;size = 0;}@Overridepublic void push(Object obj) {Node p = new Node(obj);if(top == null) {top = p;} else {p.next = top;top = p;}size ++;}@Overridepublic Object pop() {Node p = top;top = top.next;size --;return p.data;}@Overridepublic boolean isEmpty() {if(size == 0)return true;elsereturn false;}@Overridepublic int size() {return size;}@Overridepublic Object peek() {return top.data;}public String toString() {StringBuilder sb = new StringBuilder("[");Node p = top;if(p == null) {sb.append("");} else {do{sb.append(p.data + ", ");}while((p = p.next) != null);}sb.append("]");return sb.toString();} }
測(cè)試(堆)棧
package stack;public class Test {/*** 測(cè)試堆棧* @param args*/public static void main(String[] args) {//Stack stack = new ArrayStack();Stack stack = new LinkStack();for(int i=0; i<10; i++) {stack.push(i);}System.out.println(stack.toString());Object a = stack.pop();System.out.println(a + stack.toString());stack.push(20);Object b = stack.peek();System.out.println( b + stack.toString());stack.clear();System.out.println( "數(shù)據(jù)數(shù)量:" + stack.size()+ " isEmpty? " + stack.isEmpty() + " 數(shù)據(jù)為:" + stack.toString());}}結(jié)果
[9, ?8, ?7, ?6, ?5, ?4, ?3, ?2, ?1, ?0, ?] 9[8, ?7, ?6, ?5, ?4, ?3, ?2, ?1, ?0, ?]20[20, ?8, ?7, ?6, ?5, ?4, ?3, ?2, ?1, ?0, ?]
數(shù)據(jù)數(shù)量:0 ?isEmpty? true ?數(shù)據(jù)為:[]
轉(zhuǎn)載于:https://my.oschina.net/verynix/blog/365814
總結(jié)
- 上一篇: opencv第一课 打开一个图片
- 下一篇: 如何实现CSS居中?–CSS居中常用方法