The implementation of the chain-stack storage structure of Java stack

Source: Internet
Author: User

First, the chain stack

Using a single-linked list to hold all the elements in the stack, this chain-structured stack is called a chain stack.

Second, the stack of the chain storage structure implementation
1  PackageCom.ietree.basic.datastructure.stack;2 3 /**4 * Chain Stack5  *6 * Created by Ietree7 * 2017/4/298  */9  Public classLinkstack<t> {Ten  One     //defines an internal class Node,node instance that represents the node of the chain stack A     Private classNode { -  -         //saving data for a node the         PrivateT data; -         //reference to the next node -         PrivateNode Next; -         //no parameter constructor +          PublicNode () { -         } +         //constructors that initialize all properties A          Publicnode (T data, node next) { at  -              This. data =data; -              This. Next =Next; -  -         } -  in     } -     //Save the stack top element of the chain to     PrivateNode top; +     //Save the number of nodes that are already contained in the link stack -     Private intsize; the     //Create an empty chain stack *      PublicLinkstack () { $         //empty chain stack, top value is nullPanax Notoginsengtop =NULL; -  the     } +  A     //creates a chain stack with the specified data element, with only one element of the chain the      PublicLinkstack (T element) { +  -top =NewNode (element,NULL); $size++; $  -     } -  the     //returns the length of the link stack -      Public intLength () {Wuyi  the         returnsize; -  Wu     } -  About     //into the stack $      Public voidpush (T element) { -  -         //Point top to the newly created element, and the next reference of the new element points to the original top element of the stack -top =NewNode (element, top); Asize++; +  the     } -  $     //out of the stack the      PublicT Pop () { the  theNode OldTop =top; the         //let the top reference point to the next element of the top element of the original stack -top =Top.next; in         //releases the next reference to the top element of the original stack theOldtop.next =NULL; thesize--; About         returnOldtop.data; the  the     } the  +     //accesses the top element of the stack without deleting the top element of the stack -      PublicT Peek () { the Bayi         returnTop.data; the  the     } -  -     //determine if the link stack is an empty stack the      Public Booleanempty () { the  the         returnSize = = 0; the  -     } the  the     //Please empty chain stack the      Public voidClear () {94  thetop =NULL; theSize = 0; the 98     } About  -      PublicString toString () {101 102         //when the link stack is an empty stack103         if(Empty ()) {104  the             return"[]";106 107}Else {108 109StringBuilder SB =NewStringBuilder ("["); the              for(Node current = top; Current! =NULL; Current =current.next) {111  theSb.append (current.data.toString () + ",");113  the             } the  the             intLen =sb.length ();117             returnSb.delete (Len-2, Len). Append ("]"). toString ();118         }119  -     }121 122}

Test class:

1  PackageCom.ietree.basic.datastructure.stack;2 3 /**4 * Created by Ietree5 * 2017/4/296  */7  Public classLinkstacktest {8 9      Public Static voidMain (string[] args) {Ten  Onelinkstack<string> stack =NewLinkstack<string>(); A  -Stack.push ("AAAA"); -Stack.push ("BBBB"); theStack.push ("CCCC"); -Stack.push ("dddd"); - System.out.println (stack); -  +SYSTEM.OUT.PRINTLN ("Access stack top element:" +Stack.peek ()); -  +System.out.println ("First pop-up stack top element:" +Stack.pop ()); A  atSystem.out.println ("Second pop-up stack top element:" +Stack.pop ()); -  -System.out.println ("Stack after two pops:" +stack); -  -     } -  in}

Program output:

[dddd, CCCC, BBBB, AAAA] access the top element of the stack: dddd first pop-up top element: dddd The second pop-up of the top element: CCCC two times after pop stack: [bbbb, AAAA]

The implementation of the chain-stack storage structure of Java stack

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.