Let's take a look at how the single-linked list structure can be used to implement stacks and queues. Since the operation of the stack is limited to the top element of the stack, and only the first element of the single-linked list can be inserted and deleted in O (1) time, the first node of the single-linked list is used as the top of the stack, and the remaining elements are arranged sequentially. In addition, to ensure that the GetSize () method can be completed in O (1) time, an instance variable is required to dynamically record the number of elements in the stack. The specific implementation is shown in code two. 12.
Node Java code See (Java implementation linked list)
Stacklink class:
PackageCom.list.stack;ImportJava.util.Arrays;ImportCom.stack.ExceptionStackEmpty;ImportCom.list.stack.Node; Public class stacklist { //declared size of stack list Private Static intSize//declared Element Private StaticNode top; Public stacklist() { This. Size =0; This. top =NULL; }//Get the size of the stack Public int GetSize() {if(IsEmpty ())return 0;Else returnSize }//Get whether stack is empty Public Boolean IsEmpty() {returnSize = =0; }//Get the top element of the stack PublicObjectTop()throwsExceptionstackempty {if(IsEmpty ())Throw NewExceptionstackempty ("Stack is empty!");returnTop.getelement (); }//Push element to stack Public void Push(Object Element) {Node NewNode =NewNode (Element,top); top = NewNode; Size + +; }//Pop element from stack PublicObjectPop()throwsExceptionstackempty {if(IsEmpty ())Throw NewExceptionstackempty ("Stack is empty!"); Object container = Top.getelement (); top = Top.getnext (); Size--;returnContainer }//Get The all elements of stack Public void getallelements()throwsexceptionstackempty {Node traveltop; Traveltop = top; object[] Array =NewObject[getsize ()]; for(inti =0; i < Array.length;i + +) {Array[i] = traveltop.getelement (); Traveltop = Traveltop.getnext (); } System.out.println ("Get all elemnt:"+ arrays.tostring (array)); }}
Stacklinktest class:
Packagecom. List. Stack;Importcom. Stack. Exceptionstackempty;public class Stacklisttest {public static void main (string[] args) throws Exceptionstackempty {//test class St Acklist stacklist sl = new Stacklist ();System. out. println("Size of Stack list:"+ SL. GetSize());System. out. println("is emplty?:"+ SL. IsEmpty());Sl. Push( A);Sl. Push( -);Sl. Push( the);Sl. Push( -);Sl. Push(2);Sl. Push(6);Sl. Getallelements();System. out. println("Size of Stack list:"+ SL. GetSize());System. out. println("is emplty?:"+ SL. IsEmpty());Sl. Getallelements();System. out. println(SL. Pop());System. out. println(SL. Pop());System. out. println(SL. Pop());System. out. println(SL. Pop());System. out. println(SL. Pop());System. out. println(SL. Pop());System. out. println("Size of Stack list:"+ SL. GetSize());System. out. println("is emplty?:"+ SL. IsEmpty());}}
Test results:
Size ofStack list:0 isEmplty? :trueGetAll elemnt: [6,2, -, the, -, A]size ofStack list:6 isEmplty? :false62 - the - ASize ofStack list:0 isEmplty? :true
Reprint please indicate the source, thank you!
http://blog.csdn.net/github_27609763/article/details/46476917
Implementing a Java custom stack queue based on a linked list