Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Stack chain Storage

1. chained representation of stack
The stack's chain storage structure is called the chain stack, which is a single-chain table with limited operations. The insert and delete operations can only be performed at the header position. Therefore, the chain stack does not need to append header nodes as a single-chain table. The top pointer of the stack is the head pointer of the chain table. Figure 3-4 shows the stack chain storage representation.

The node types of the chain Stack are described as follows: typedef struct Snode {ElemType data; struct Snode * next;} SNode, * Link_Stack;
Implementation of basic chain stack operations
2 Implementation of basic stack operations (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) stack pressure (element stack) Status push (SNode * top, ElemType e) {SNode * p; p = (SNode *) malloc (sizeof (SNode )); if (! P) return ERROR;/* failed to apply for a new node, ERROR flag */p-> data = e; p-> next = top-> next; top-> next = p; /* hook link */return OK;} Note: 1. Create a linked list similar to the header insertion method. 2. Do not store data in the data field of the top node. You can also set it to store data, how to change the program?
(3) ElemType pop (SNode * top)/* exit the top element of the stack */{SNode * p; ElemType e; if (top-> next = NULL) return ERROR;/* the stack is empty, and the ERROR mark */p = top-> next; e = p-> data; /* get the top element of the stack */top-> next = p-> next;/* modify the top pointer of the stack */free (p); return OK ;}
Elements in the output Stack
Void print (SqStack * S) {int c; cout <"elements in the output stack" <endl; for (c = S. top --; c> = 0; c --) {cout <S. bottom [c] <endl ;}}
Stack and recursive call implementation
Another important application of stack is implementing recursive calls in programming languages. Recursive call: A function (or process) calls itself directly or indirectly, or recursion for short ). Recursion is a powerful tool in programming. Because recursive functions have a clear structure, the program is easy to read, and the correctness is easily proved. To prevent recursive calls from being terminated, a valid recursive call function (or process) consists of two parts: recursive rules (methods) and termination conditions.

To implement recursion, a system stack (recursive working stack) is required for processing function calls when a program is running.
The system stack is a special storage area. When a function is called, The system creates a work record, called a stack frame, and places it on the top of the stack.
Initially, only the returned address and pointer pointing to the previous Stack are included.
When the function calls another function, the local variables and parameters of the function are added to its stack arguments.
When a function stops running, it will delete its stack iterator from the stack, and the program control will return to the original called function for further execution.

General steps for returning the called function from the called Function
If the stack is empty, a normal result is returned.
Otherwise, a work record pops up from the top of the stack and assigns the parameter value and local variable value in the "work record" to the corresponding variable ;.
Read the return address and assign the function value to the corresponding variable.
Transfer to return address.

Advantages and disadvantages of Recursive Algorithms

Advantages
The program is simple, clear, easy to analyze, and readable.
Disadvantages
Free Space: A system stack is required to implement recursion for processing function calls during the running time.
Time-consuming: it takes a lot of time to import local variables, formal parameters, and return addresses to the stack, output stacks, and parameter transfer, and repeated computing in recursion is also a waste of time.

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.