This is the 4th chapter of Java data Structure and algorithm, from the beginning of this chapter we will understand the design and implementation of the stack, the following is the relevant knowledge points:
- Abstract data types for stacks
- Design and implementation of sequential stacks
- Design and implementation of chain-stack
- Application of Stacks
abstract data types for stacks
The stack is a simple data structure for storing, a bit like a linked list or sequential table (collectively, the linear table), the largest difference between the stack and the linear table is the operation of data access, we can think of the stack (stack) is a special linear table, its insert and delete operations are only allowed at one end of the linear table, generally speaking, The one end of the allowed operation is called the top of the stack (top), the non-operational end is called the bottom of the stack (Bottom), and the operation of the inserted element is called the stack (push), and the operation to delete the element is called the Out stack (POP). If there is no element in the stack, it is called an empty stack, and the structure of the stack is as follows:
From the graph we can view the stack only from the top of the stack to access elements, while the first entry is the back of the element, and the top of the stack will always point to the topmost element in the stack. This gives a formal definition of the stack: stack is an ordered special linear table that can only be inserted and deleted at one end of the table (called the top of the stack, top, always pointing to the top of the stack), and the last inserted element will be first deleted, so the stack is also called a LIFO LIFO) or a linear table of advanced back-out (first-in-last-out FILO). Stack of basic operations to create a stack, empty, into the stack, out of the stack, get stack top elements, etc., note that the stack does not support the deletion of the specified location, insert, its interface stack declaration as follows:
Packagecom.spring.test;/*** Stack Interface abstract data type*/ Public InterfaceStack<t> { /*** Whether the stack is empty *@return */ BooleanIsEmpty (); /*** Data element into stack *@paramData*/ voidpush (T data); /*** Returns the top element of the stack without a stack *@return */T Peek (); /*** Stack, return the top element of the stack, and remove the element from the stack *@return */T pop ();}
Packagecom.spring.test;Importjava.io.Serializable;Importjava.util.EmptyStackException;/*** Created by Administrator on 2018/3/9.*/ Public classSeqstack<t>ImplementsStack<t>, Serializable {Private Static Final LongSerialversionuid = -5413303117698554397l; /*** Stack top pointer,-1 for empty stack*/ Private intTop=-1; /*** Capacity size defaults to ten*/ Private intcapacity=10; /*** An array of elements to store*/ Privatet[] Array; Private intsize; PublicSeqstack (intcapacity) {Array= (t[])NewObject[capacity]; } PublicSeqstack () {array= (t[])Newobject[ This. capacity]; } Public intsize () {returnsize; } @Override Public BooleanIsEmpty () {return This. top==-1; } /*** add element, insert from top of stack (end of array) *@paramData*/@Override Public voidpush (T data) {//determine if the capacity is adequate if(array.length==size) ensurecapacity (size*2+1);//Expansion//adding elements from the top of the stackarray[++top]=data; Size++; } /*** Gets the value of the top element of the stack without deleting *@return */@Override PublicT Peek () {if(IsEmpty ())Newemptystackexception (); returnArray[top]; } /*** removed from top of stack (sequential table trailer) *@return */@Override PublicT Pop () {if(IsEmpty ()) {Newemptystackexception (); } size--; returnarray[top--]; } /*** Method of expansion *@paramcapacity*/ Public voidEnsurecapacity (intcapacity) { //If you need to expand the capacity than the current array of capacity is smaller, there is no need for expansion if(capacity<size) { return; } t[] old=Array; Array= (t[])NewObject[capacity]; //copying elements for(inti=0; I<size; i++) {Array[i]=Old[i]; } } }
Packagecom.spring.test;/** * */ Public classEqualstohashcodetest { Public Static voidMain (string[] args) {Seqstack<String> s =NewSeqstack<string>(); S.push (A); S.push (B); S.push (C); intL =s.size (); System.out.println ("Size-->" +s.size ());//size is decreasing, you must first record for(inti=0;i<l;i++) {System.out.println ("S.pop" +S.pop ()); } System.out.println ("S.peek-->" +S.peek ()); }}
design and implementation of chain-stack
Understanding the sequence stack, we then look at the chain stack, the so-called chain stack (Linked stack), is the use of chain storage structure of the stack, because we operate on the top end of the stack, so here the single-linked list (not the lead node) as the basis, directly implement the stack of the addition, access, delete and other major operations. The operation process is as follows:
As can be seen from the graph, whether it is inserting or deleting the direct operation is the list head is the top element of the stack, so we just need to use a single linked list without the lead node. The code is implemented as follows, which is relatively simple, but more analysis:
Stack design and implementation of Java data structure and algorithm