Java sequential stacks and chained stacks

Source: Internet
Author: User

Tag: source Specifies next rule Java section Note data structure OID

Definition of stacks

A stack is a linear table that restricts the operations of inserting and deleting a section of a table, and is usually able to insert and delete one end to the top of the stack, the exception is called the bottom of the stack, and is called an empty stack when there are no elements in the table.

Usually delete (also known as "fallback") is called pop-up pop operation, each delete is the top of the stack of elements, each insert (also known as "stack") is called a press push -in operation.

When the stack is full, the push operation, will, when the stack when the stack 上溢 operation is called 下溢 .

Overflow is a case of error, and underflow may be a normal condition to handle.

The operation of the stack is based on the principle of LIFO, short LIFO .

The basic operation definition of the stack:

    • Initstack: Constructs an empty stack;
    • Stackempty: Judging whether it is empty;
    • Stackfull: Judge whether the stack is full;
    • Push: Into the stack, the element is pressed into the top of the stack;
    • Pop: Back stack, pop up the top of the stack element, pay attention to non-null judgment;
    • Stacktop: Removes the top element of the stack without changing the pointer.

To make a simple stack interface:

Package com.wuwii.utils;/*** Stack * @authorZhang Kai * @version1.0* @since <PRE>2017/12/14 22:51</pre> */ Public Interfacestack<e> {/*** in-Stack     *     * @param elementelements in the stack     */    void Push(E Element);/*** Pop up the top element of the stack and change the pointer     *     * @returnStack Top element     */EPop();/*** Returns the top element of the stack without changing the pointer     *     * @returnStack Top element     */Etopelement();/*** Determine if the stack is empty     *     * @returntrue is empty stack     */BooleanIsEmpty();/*** Empty Stack     */    void Clear();}
Sequential stacks

is a sequential linear table that conforms to the rules of the LIFO operation.

Package com.wuwii.utils;/*** Sequential Stacks * @authorZhang Kai * @version1.0* @since <PRE>2017/12/14 23:05</pre> */ Public classArraystack<e>Implementsstack<e> {/*** Default size of the initialization stack     */    Private Final intDefaultSize =Ten;/*** Stack's collection size     */    Private intSize/*** location of the top of the stack     */    Private intTop/*** elements stored in an array     */    PrivateObject[] elements;/*** Initialize a stack with a default size of 10     */     Public Arraystack() {Initstack(defaultsize); }/*** Initialize the stack of the specified size     * @param givensizeSpecify stack size     */     Public Arraystack(Integer givensize) {Initstack(givensize); }/*** Initialize Stack     * @param givensizethe given stack size     */    Private void Initstack(Integer givensize)        {size = Givensize; top =0; elements =NewObject[size]; }/*** Empty Stack     */    @Override     Public void Clear() {top =0; }/*** in-Stack     * @param elementelements in the stack     */    @Override     Public void Push(E Element) {Sizecheckforpush();    elements[top++] = element; }/*** Pop up the top element of the stack and change the pointer     * @returnStack Top element     */    @Override     PublicEPop() {Sizecheckforpop();return(E) elements[--top]; }/*** Returns the top element of the stack without changing the pointer     * @returnStack Top element     */    @Override     PublicEtopelement() {Sizecheckforpush();return(E) Elements[top-1]; }/*** Determine if the stack is empty     * @returntrue is empty stack     */    @Override     PublicBooleanIsEmpty() {returnSize = =0; }/*** Check when entering the stack     */    Private void Sizecheckforpush() {if(Top >= size) {Throw NewRuntimeException ("Stack Overflow"); }    }/*** Check back stack     */    Private void Sizecheckforpop() {if(IsEmpty()) {Throw NewRuntimeException ("Stack is empty"); }    }}
Chained stacks

A chain-linear table that conforms to the LIFO rule.

Package com.wuwii.utils;/** * @authorZhang Kai * @version1.0* @since <PRE>2017/12/15 12:58</pre> */ Public classLinkstack<e>Implementsstack<e> {/*** Chain Unit     */    PrivateNode<e> top;/*** Initialize chained stacks     */     Public Linkstack() {Initstack(); }/*** Initialize     */    Private void Initstack() {top =NULL; }/*** Storage Unit     */    Private Static classnode<e> {E element;        Node<e> Next; Node (E element, node<e> next) { This.element= element; This.Next= Next; }    }/*** in-Stack     *     * @param elementelements in the stack     */    @Override     Public void Push(E Element) {top =NewNode<e> (element, top); }/*** Pop up the top element of the stack and change the pointer     *     * @returnStack Top element     */    @Override     PublicEPop() {Checkempty(); E element = top.element; top = top.Next;returnElement }/*** Returns the top element of the stack without changing the pointer     *     * @returnStack Top element     */    @Override     PublicEtopelement() {Checkempty();returnTop.element; }/*** Determine if the stack is empty     *     * @returntrue is empty stack     */    @Override     PublicBooleanIsEmpty() {returntop = =NULL; }/*** Empty Stack     */    @Override     Public void Clear() {if(IsEmpty()) {return; } for(node<e> x = top; X! =NULL; ) {node<e> next = x.Next; X.element=NULL; X.Next=NULL;        x = Next; } size =0; }/*** Check if the chain stack is empty and throws an exception for null     */    Private void Checkempty() {if(IsEmpty()) {Throw NewRuntimeException ("Linkstack is empty"); }    }}

First, push modifies the next field of the newly generated list node and points to the top of the stack, then sets top to point to the new linked list node, and the pop is the opposite.

Comparison of sequential stacks and chain stacks

The operation of the chain stack and the sequential stack requires constant time, and the time Complexity is O (1), which is mainly considered from the space and time complexity.

The sequence stack initialization must be given a specified size, when the stack is not satisfied, it will cause a part of the space wasted, the chain stack is longer, relatively space-saving, but increased the pointer field, additional data structure overhead.

When multiple stack shares are required, sequential storage can take full advantage of the one-way extension of the sequential stack, and an array can exist in two stacks, each of which extends from the top of its stack, thus reducing wasted space. But only two of the space on the stack has the opposite demand when it can be used. is the best one stack can only increase, one can only be reduced. If, two increases together, it can cause a stack overflow.

If in multiple sequential stack spaces, one stack is full and the other may not be full, you need to use the LIFO algorithm of the stack to pan the full stack element left or right, which can cause a large number of data elements to move, which increases the overhead of the time.

In contrast, using two stacks to share a space is a more appropriate way to store it, but it also increases the risk of stack overflow.

Because of the discontinuity of the chain storage structure, when need, when to go to storage, there is no overflow problem, but increase the overhead of the structure, the overall waste of space, but do not need stack sharing,

Java sequential stacks and chained stacks

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.