Paper discussion: Stack)

Source: Internet
Author: User
ArticleDirectory
    • Summary

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

Stack)It is a simple data structure, but it is widely used in computers. It is a set of ordered elements. The most notable feature of stack isLIFO(Last in, first out, first in, first out ). When we store a pile of books in the box, we store the books under the box first. We must extract the books we store later to see and take out the books we store earlier.

 

 

Each element in the stack is calledFrame. WhileTop ElementCalledTop Frame. Stack supports only three operations, Pop, top, and push.

Pop extracts the top-most elements in the stack (8 ).Top ElementTo the element (9) that entered earlier ).

Top view the top element of the stack (8 ).

Push puts a new element (5) at the top of the stack.

Stack does not support other operations. If you want to retrieve element 12, you must perform three pop operations.

Stack and pop, push, and top operations

 

 

 

Function call is the most typical computer application on the stack. Each process has a stack. Each frame records the parameters, automatic variables, and return addresses of the called functions. When this function calls a new function, a frame is pushed in the stack. When the function is returned after execution, the frame will pop to enter the original function that calls the function and continue execution. For details, see Linux from program to process

The actually used stack does not necessarily conform to the data structure stack. For example, some languages can be viewed by calling functions.Non-top Frame. This stack is more similar to the following classic game

 

 

Stack C implementation (based on tables)

The stack is an ordered set of elements for operations. Therefore, we can implement stacks Based on arrays or tables. If arrays are used to implement stacks, We need to reserve enough space for the stack to use, and a subscript is required to record the position of the top element.

Here we use a one-way linked list to implement the stack. We can use the operations defined in the Introduction table (list) article to implement three operations, but here the operations are overwritten relatively independently.Code.

 /*  By vamei  */ /*  Use Single-linked list to implement Stack  */  # Include <Stdio. h> # Include <Stdlib. h> Typedef  Struct Node * Position; typedef  Int  Elementtp;  //  Point to the head node of the List Typedef Struct Node *Stack;  Struct  Node {elementtp element; position next;}; stack init_stack (  Void  );  Void  Delete_stack (stack); elementtp top (stack );  Void  Push (stack, elementtp); elementtp POP (stack );  Int  Is_null (stack );  Void Main ( Void  ) {Elementtp; Int  I; stack SK; SK = Init_stack (); push (sk,  1  ); Push (sk,  2  ); Push (sk,  8  ); Printf (  "  Stack is null? % D \ n  "  , Is_null (SK ));  For (I = 0 ; I <3 ; I ++ ) { = Pop (SK); printf (  "  Pop: % d \ n  "  , A);} printf (  "  Stack is null? % D \ n  "  , Is_null (SK); delete_stack (SK );}  /*  * Initiate the stack * malloc the head node. * head node doesn't store valid data * head-> next is the top node  */ Stack init_stack (  Void  ) {Position NP; stack SK; NP = (Position) malloc ( Sizeof ( Struct  Node); NP -> Next = NULL; //  SK-> next is the top node SK = NP;  Return  SK ;}  /*  Pop out all elements * and then delete head node */  Void  Delete_stack (stack SK ){  While (! Is_null (SK) {Pop (SK);} Free (SK );}  /*  * View the top Frame  */  Elementtp top (stack SK ){  Return (SK-> next-> Element );}  /*  * Push a value into the stack  */ Void  Push (stack SK, elementtp value) {position NP, oldtop; oldtop = Sk-> Next; NP = (Position) malloc ( Sizeof ( Struct  Node); NP -> Element = Value; NP -> Next = Sk-> Next; SK -> Next = NP ;}  /*  * Pop out the top Value  */ Elementtp POP (stack SK) {elementtp element; position top, newtop;  If  (Is_null (SK) {printf (  "  Pop () on an empty Stack  "  ); Exit (  1  );}  Else  {Top = Sk-> Next; element = Top-> Element; newtop = Top->Next; SK -> Next = Newtop; free (top );  Return  Element ;}}  /*  Check whether a stack is empty  */  Int  Is_null (stack SK ){  Return (SK-> next = Null );} 

 

Output result:

Stack is null? 0
Pop: 8
Pop: 2
Pop: 1
Stack is null? 1

 

Summary

Stack, LIFO

Pop, push, top

 

Welcome to the "paper discussion: algorithms and data structures" series.

 

Update:

I previously implemented the stack using a two-way circular linked list, and later found that this is unnecessary. It does not bring additional benefits to the stack, but also increases the memory space required.

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.