Jumpnode recursive and non-recursive access

Source: Internet
Author: User

The JUMPNODE definition structure is as follows:
structJumpNode {    data; //存储数据    int order; // 记录访问次序,初始化均为0    JumpNode *jump, *next; // next为线性下一节点,jump为跳跃到下一节点    JumpNodedata(d), order(-1), jump(NULL), next(NULL) {}};

It is now necessary to access a jumpnode *list in a recursive and non-recursive manner. The Order property of each node is logged on the access sequence at the time of the visit.

    • Recursive access
    voidRecursive_traversal (Jumpnode*node, int&Order) {if(node== NULL ||Node -Order != -1)return; Node -Order = Order++; Recursive_traversal (node -JumpOrder); Recursive_traversal (node -NextOrder); }
    • Non-recursive access
    voidint &order) {        JumpNode *curr = node;        stack<JumpNode *> s;        while (!s.empty() || (curr && curr->order==-1)) {            if (curr && curr->order == -1) {                curr->order = order++;                s.push(curr);                curr = curr->jump;            else {                 curr = s.top();                s.pop();                curr = curr->next;            }        }    }
    • Test section
      The linked list is printed using the following function when testing:
    print*node) {        whileprintf("(%d,%d) ", node->data, node->order), node = node->next;        printf("\n");    }

The linked list structure is:

Jumpnode*N1= NewJumpnode (1); Jumpnode*N2= NewJumpnode (2); Jumpnode*N3= NewJumpnode (3); Jumpnode*N4= NewJumpnode (4); N1 -Jump=N3; N2 -Jump=N4; N3 -Jump=N2; N4 -Jump=N4; N1 -Next=N2; N2 -Next=N3; N3 -Next=N4;

The final print results are:

[root@localhost cpp]# ./jumpnode (1,1) (2,3) (3,2) (4,4
About non-recursive access

It can be seen that the Recursive_traversal internal conditions (node! = NULL && Node->order = =-1) are set to Iterative_traversal for recursive access modification Conditions for access to the left branch (if branch).
and the specific statements of the pre-order traversal can be performed within the left branch (if branches);
For the middle order access to a particular statement is placed in the non-recursive version of the Else branch.

Jumpnode recursive and non-recursive access

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.