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.
voidRecursive_traversal (Jumpnode*node, int&Order) {if(node== NULL ||Node -Order != -1)return; Node -Order = Order++; Recursive_traversal (node -JumpOrder); Recursive_traversal (node -NextOrder); }
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