Stack 01 I understand

Source: Internet
Author: User


A stack is a linear table that only supports insertion and deletion at one position. It is usually located at the end of the table and is called the top table) ---- In fact, I like to use the head of the linked list as the top of the stack. The top of the stack does not store any value, but it is just a sign. The so-called "Top element of the stack" is actually

Is the direct successor value of the head node of the linked list. The insert and delete operations are convenient. Stack can be implemented using linked lists (including dynamic and static linked lists) or arrays. Let's take a look at how to use a linked list (take a dynamic linked list as an example, you can promote it to a static linked list ).

// ----------- Use a dynamic single-chain table to implement the stack storage structure -------------

Typedef struct Node {

ElemType data;

Struct Node * next;

} * LNode, * Stack;

// ------------------- Use a dynamic single-chain table to implement the basic stack operations ------------------------

Stack InitStack (void); // construct an empty Stack

Void DestroyStack (Stack * S); // initial condition: Stack S already exists. Operation Result: Destroy stack S.

Stack MakeEmpty (Stack S); // initial condition: Stack S is not empty. Operation Result: reset stack S to empty stack.

Int IsEmpty (Stack S); // initial condition: Stack S already exists. Operation Result: judge whether the stack is empty.

Int StackLength (Stack S); // initial condition: Stack S already exists. Operation Result: return the number of stack S nodes.

ElemType GetTop (Stack S); // initial condition: Stack S is not empty. Operation Result: return the value of the top element of the stack.

LNode NewLNode (ElemType X); // construct a new node with the data field X

// Initial condition: Stack S already exists. Operation Result: element X is inserted as the new top element of the stack.

Void Push (Stack S, ElemType X );

ElemType Pop (Stack S); // initial condition: the Stack S is not empty. Operation Result: the stack top element of S is deleted and its value is returned.

//---------------------------------------------------------------

In fact, we can easily find that six of the nine basic operations above are the same as those of the Dynamic Linked List, so we will not repeat them here, we will list the remaining three algorithm implementations.

Note: Here I will directly call the basic operations of the Dynamic Linked List.

// ---------- Algorithm Implementation of basic stack operations using a dynamic single-chain table -----

ElemType GetTop (Stack S); // initial condition: Stack S is not empty. Operation Result: return the value of the top element of the stack.

{

Return S-> next-> data;

}

// Initial condition: Stack S already exists. Operation Result: element X is inserted as the new top element of the stack.

Void Push (Stack S, ElemType X)

{

LNode P;

P = NewLNode (X );

ListInsert (S, P );

}

ElemType Pop (Stack S); // initial condition: the Stack S is not empty. Operation Result: the stack top element of S is deleted and its value is returned.

{

ElemType X = GetTop (S );

ListDelete (S );

Return X;

}

//---------------------------------------------------------------------------------

Stack Array Implementation. an array stack is very simple. each stack has a TopOfStack (Top for short). For an empty stack, it is-1 --- this is the initialization of the empty stack (some may ask why the value is not 0, don't you like a theme "Stack top", just like the head pointer in the linked list?

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.