C-language recursion, non-recursive implementation of flip chain list

Source: Internet
Author: User

Flip linked list As, the list of common operations, is also often encountered in the interview.

Analysis
Non-recursive analysis:

Non-recursive uses a lot of tricks, very prone to error.

Recursive analysis is relatively simple, in the code


Code:

#include <stdio.h>#include<stdlib.h>typedefintElemtype;typedefstructnode{elemtype element; structNode*next;//written in node* Next;node * Next;node *next, is also possible, in order to facilitate reading, later unified written elemtype* element. ' * ', '; ', ', ' ' = ', ' and ' and so on these special meanings of the key character syntax rules are similar, itself has the division meaning, both sides with the space and no space meaning is consistent, that is, the space for these special characters is not working, and the parsing compiler, The runner is not separated by a space; the glyphs are not used with these characters, for readability, not to look at a piece of excrement, it is best to add a space on both sides of these special characters. }node;//This type name can be the same as its struct name, not contradictory. /*Function: Prints all nodes of the linked list from the beginning of the node, traversing the linked list. Traverse_linkedlist * Traverse a kind of data organization, go inside to look, give a person to say, general traverse print inside of data, know is go in, can get inside of data. * This data structure has a lot of elements, but each traversal can only access one element, so to loop, and to access all nodes, there must be a mechanism to jump from one node to another node. * @paramb: node* Phead: The first address of the list, the address of the first node. */voidTraverse_linkedlist (node*phead) {Node*p=phead;//define a Jump control pointer    if(NULL = =p) {printf ("Pro, the list is empty \ n"); return ; }    Else{         while(NULL! =p) {printf ("%d\n", p-Element);//access to the structure of the data, can be used: its structure variables. member variables; You can also: pointer-to-member variables that refer to this structp = PNext; }        }}/*Function: Flip List (non-recursive) reverse_linkedlist *param:node** phead//Parameter meaning: Save the address of the linked list pointer variable *return:void * knowledge point: node** p; P represents the address of the linked list pointer variable. *p is the linked list. **p represents the node (can be considered "*" has the analytic function) * The overall idea: the relationship between the reorganization node, the original from left to right, turned from the right to the left; it points to the head node, and flips to the end node. * Time Complexity: O (N) * **/voidReverse_linkedlist (node** phead) {//The address of the pointer variable that is stored in the chain header is passed in to change the variable, and the pointer is essentially a variable that saves the address. Node *pleft,*pright,*pmiddle;//define three pointers: Left and right, Pright: move. pleft=pright=*Phead; //an empty list or only one node.    if(NULL = = Pleft | | NULL = = (pleftnext)) {            return ; }Else{        //processing of the node of the headPleft = *Phead; Pright= (*phead), next;//'--' priority highestPleft-Next =NULL; //Cyclic control conditionsNode *ctl=Pright;  while(NULL! =ctl) {Pmiddle= Pright;//before you move, savePright = Pright Next;//Pright move to the next node.CTL = Pmiddle Next;//before transforming the relationship, save the control information .Pmiddle next = Pleft;//Transformation RelationshipsPleft = Pmiddle;//P-Transform        }                *phead = Pleft;//point to the list header pointer variable, pointing to the new header.     }}/**function: Flip List (recursive) reverse_recursive_linkedlist *param:node* head *return:node* * Knowledge Point: * recursion: Call itself; export. * Thinking: * * If the list is empty linked list or only one node, you can directly return the table header, if it is two nodes above the linked list, call itself, get the sub-node of the table head, and then rebuild their relationship. * * */node* Reverse_recursive_linkedlist (Node *head) {    if(head = NULL | | head--NEXT = =NULL) {       returnHead; }    Else{node* Second = HeadNext; Node* New_head =Reverse_recursive_linkedlist (second); SecondNext =Head; HeadNext =NULL; returnNew_head; } }intMain () {node*p, *q, *head;//just a few pointer variables are defined, what does the system do? Space required to open these pointer variable types. If no initial value is normally assigned by the system randomly, there is no point to the true node, that is, the P-and member variable is meaningless. Will report "";    A node-type declaration that tells the system to do a corresponding syntax check, what to do with what the pointer is pointing to. //Insert Ten nodes//To Create a node, the node is essentially a block of data, which is space-consuming, with storage space, and a data carrier. So opening up space is the key to creating data, and then telling the system how you deal with this space, and finally the space opens up successfully. p = q = (node*)malloc(sizeof(node)) ;//Unlike the data type identifier, there is no identifier, you want to open the space manually, and extract the information. Identifier function: Parse + address. Head =p; P-element =0 ; intI=0;  for(i =0; I <9; i++) {Q= (node*)malloc(sizeof(node)); Q-element = i +1 ; PNext =Q; P=Q;    } traverse_linkedlist (head); Reverse_linkedlist (&head);//& can only be used for access, no parsing function. Traverse_linkedlist (head); Traverse_linkedlist (Reverse_recursive_linkedlist (Head));}

I regain C, a lot of things to see is familiar and unfamiliar, so the comments are a little more, only for reference, not directly ignored,

C-language recursion, non-recursive implementation of flip chain 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.