Data structure-chained storage of stacks

Source: Internet
Author: User

Chained storage of stacks

1-Stack chain representation
The chain storage structure of a stack is called a chain stack, which is a single linked list with limited operation. Its insert and delete operations can only be performed at the table header position. Therefore, there is no need for the chain stack to attach the head node as a single linked list, and the top pointer top is the head pointer of the linked list. Figure 3-4 is a chain-stored representation of the stack.

链栈的结点类型说明如下:typedef  struct  Snode{  ElemType   data ;struct Snode  *next ;} SNode, *Link_Stack ;
Implementation of basic operation of chain stack
2  the implementation of the basic operation of the chain stack (1 ) Stack initialization SNode *init_link_stack (void ) {SNode *top; top= ( SNode *) malloc (sizeof  (SNode)); if    (Top==null ) return  ERROR; else       {Top->next=null ; return  (top);}}  
 (2 ) press stack (element into stack) Status push (SNode *  top, elemtype e) {SNode *  p;p =  (SNode * ) malloc (sizeof (SNode)); if  ( p) return  ERROR; /* request new node failed, return error flag */ p->  data  =  e; P->  next=  top->  Next; Top->  next=  p; /* hook chain */ return  OK;} Note: 1 , with the head insertion method to establish a linked list like 2 , where the top node of the data field does not put data, can also be set to store data, How does the program change? 
 (3 ) stack (element out stack) elemtype pop (SNode *  top) /* stack top elements out of stack */ {SNode *  p; Elemtype e; if  (Top->  next==   NULL ) return  ERROR; /* stack empty, return error flag */ p=  top-  Next; E=  p->  data  ; /* take the top element of the stack */ top->  Next =  p->  next; /* modify stack top pointer */ free (p); return  OK;} 
Elements in the output stack
void print(SqStack *S){       int c;    cout<<"输出栈中元素"<<endl;    for( c=S.top--;c>=0;c--)    {        cout<<S.bottom[c]<<endl;    }}
Implementation of stack and recursive invocation
栈的另一个重要应用是在程序设计语言中实现递归调用。递归调用:一个函数(或过程)直接或间接地调用自己本身,简称递归(Recursive)。递归是程序设计中的一个强有力的工具。因为递归函数结构清晰,程序易读,正确性很容易得到证明。为了使递归调用不至于无终止地进行下去,实际上有效的递归调用函数(或过程)应包括两部分:递推规则(方法),终止条件。

Recursive system implementation requires a system stack (recursive work stack) to handle function calls while the program is running.
The system stack is a special storage area. When a function is called, the system creates a work record called the stack frame and puts it on top of the stack.
Initially, only the return address and pointers to the previous stack are included.
When the function calls another function, the function's local variables and parameters are added to its stack frame.
After a function is run, the stack frame is removed from the stack, and the program control returns to the original calling function to continue execution.

General steps to return the calling function from the function being tuned
If the stack is empty, the normal return is performed.
Otherwise, a work record pops up from the top of the stack, assigning the parameter value and local variable value in the "working record" to the corresponding variable.
Reads the return address and assigns the value of the function to the corresponding variable.
Transfer to the return address.

Advantages and disadvantages of recursive algorithm

Advantages
The program is very concise and clear, and easy to analyze, readability is strong.
Disadvantages
Fee Space: System implementation recursion requires a system stack that is used to process function calls during program run time.
Time-consuming: the stack, stack, and parameter transfer of local variables, formal parameters, and return addresses can take a while, and repetitive computations in recursion are also a waste.

Data structure-chained storage of stacks

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.