4, the implementation of the stack: sequential storage and chain storage

Source: Internet
Author: User

ADT for Stack:

1  Packageren.laughing.datastructure.base;2 3 Importren.laughing.datastructure.exception.StackEmptyException;4 5 /**6 * Stack stack: LIFO7 * can only be inserted (into the stack), delete (stack) operation on top of the stack8  * @authorLaughing_lz9 * @time April 6, 2016Ten  */ One  Public InterfaceStack { A     //returns the size of the stack -      Public intgetsize (); -     //determine if the stack is empty the      Public BooleanisEmpty (); -     //data element e into the stack -      Public voidpush (Object e); -     //stack top element out stack +      PublicObject pop ()throwsstackemptyexception; -     //take the top element of the stack +      PublicObject Peek ()throwsstackemptyexception; A}

Sequential storage of Stacks:

1  PackageRen.laughing.datastructure.baseImpl;2 3 ImportRen.laughing.datastructure.base.Stack;4 Importren.laughing.datastructure.exception.StackEmptyException;5 /**6 * Sequential storage structure of stacks7 * Generally, you should not set the stack's maximum capacity when constructing the stack. 8 * A reasonable practice is to first allocate a basic capacity for the stack, and then in the actual use of the process,9 * When there is not enough space on the stack to multiply the storage space, the time required for this process is halved to each numberTen * The element time is θ (1), which does not affect the time complexity of the operation implementation.  One  * @authorLaughing_lz A * @time April 6, 2016 -  */ -  Public classStackarrayImplementsStack { the     Private Final intLEN = 8;//storage size of default array -     PrivateObject[] elements;//array of data elements -     Private intTop//stack Top pointer -      +      PublicStackarray () { -          This. elements =NewObject[len]; +          This. top =-1;//when top is-1 indicates an empty stack A     } at  - @Override -      Public intGetSize () { -         returnTop+1; -     } -  in @Override -      Public BooleanIsEmpty () { to         if(Top < 0){ +             return true; -}Else{ the             return false; *         } $     }Panax Notoginseng  - @Override the      Public voidpush (Object e) { +         if(GetSize () >=elements.length) { A expandspace (); the         } +         //++top: Because the stack operation is equivalent to InsertAfter, -         //can only be inserted after the vertex, so first add 1 to the top and then the data element $Elements[++top] =e; $     } -  - @Override the      PublicObject pop ()throwsstackemptyexception { -         if(top<0){Wuyi             Throw NewStackemptyexception ("Error: Stack is empty, no stack operation"); the}Else{ -Object obj =Elements[top]; Wuelements[top--] =NULL;//first remove the original stack top data element, then empty, top minus one -             returnobj; About         } $     } -     //take the top element of the stack - @Override -      PublicObject Peek ()throwsstackemptyexception { A         if(GetSize () <=0){ +             Throw NewStackemptyexception ("Error: Empty stack top"); the}Else{ -             returnElements[top]; $         } the     } the     /** the * Extended Array length the      */ -     Private voidExpandspace () { inObject[] A =NewObject[elements.length * 2]; the          for(inti = 0; i < elements.length; i++) theA[i] =Elements[i]; Aboutelements =A; the     } the}

Chained storage for Stacks:

1  PackageRen.laughing.datastructure.baseImpl;2 3 ImportRen.laughing.datastructure.base.Stack;4 Importren.laughing.datastructure.exception.StackEmptyException;5 /**6 * Stack-chained storage7 *★ A single-linked list with no head nodes for this list8  * @authorLaughing_lz9 * @time April 6, 2016Ten  */ One  Public classStacklinkedImplementsstack{ A     PrivateSlnode top;//link List First node reference -     Private intSize//the size of the stack -      the      Publicstacklinked () { -          This. Size = 0; - //this.top = new Slnode (null, NULL); -top =NULL;//is this also instantiated? Is there any difference from the above?  +     } -  + @Override A      Public intGetSize () { at         returnsize; -     } -  - @Override -      Public BooleanIsEmpty () { -         returnSize==0; in     } -      to @Override +      Public voidpush (Object e) { -Slnode node =NewSlnode (E, top);//equivalent to insertbefore inserting a new data element before the top of the original stack thetop =node; *size++; $     }Panax Notoginseng  - @Override the      PublicObject pop ()throwsstackemptyexception { +         if(size<=0){ A             Throw NewStackemptyexception ("Error, stack is empty. "); the         } +Object obj =Top.getdata (); -top =Top.getnext (); $size--; $         returnobj; -     } -  the @Override -      PublicObject Peek ()throwsstackemptyexception {Wuyi         if(size<=0){ the             Throw NewStackemptyexception ("Error, stack is empty. "); -         } WuObject obj =Top.getdata (); -         returnobj; About     } $  -}

4, the implementation of the stack: sequential storage and chain storage

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.