[Challenges from sister paper]-expand/restore multi-layer linked list and multi-layer

Source: Internet
Author: User

[Challenges from sister paper]-expand/restore multi-layer linked list and multi-layer

Preface:

When my sister-in-law was reviewing, and by the way I was studying, I asked this question [How did she think of this ].

There is a multi-layer linked list. Well, it's two-way. Then each node has a pointer pointing to its sub-linked list, which is separate. If so, the sub-linked list at the same layer, well, it's also a two-way link. How can we quickly turn them into a primary linked list? The relationship remains unchanged. By the way, if you can expand it, restore it back...

In order not to be in front of the sister paper, I decided to pick up the paper money and draw the picture on the left and right. After 20 minutes, I moved left and right, and added the necessary code to explain it to the sister paper. I almost got down.

 

Process:

First of all, there is a two-way, so it is much easier to do. Many of the problems encountered previously were one-way. First, draw a multi-layer linked list. Ask me if this is the case, she said...

  

Then let's take a look at the sequence of storage, which can be sustained by a parent, or a row, I feel that the order of changes at one layer is a little less, so that's it.

It must be traversed from the beginning, so the time complexity is at least O (n); if there is a sublinked list, it will be appended to the existing tail pointer; so where should the new tail pointer be? Which child node is newly added? No, it's not that, because we need to put the whole layer together. In that case, we need to use the last node of the sub-linked list as the new tail pointer, so that the whole block will be appended.

Next, continue the traversal. In the same way, a sub-linked list is appended. When the first layer is complete, it continues from the appended node, and then the layers are connected.

  

Expanded. It's time to think about how to change back ?!

Reverse thinking is to traverse back from the tail. It is a two-way approach. But from the tail, how can we determine whether this node is a subnode? If you want to confirm it, we can only traverse the child nodes that match the source from the beginning, so it is necessary to traverse the child nodes multiple times, which is not scientific;

You can also use things to store all the sub-nodes, so that you do not need to traverse them every time from the end, but you need to use things in the East and West, there may be a better way;

Or start from scratch!

For a traversal chain table, if a sub-linked list exists, it is cut off from the primary linked list, but it cannot be returned in this way. Otherwise, there will be no subsequent nodes at the first layer, then we can only separate the sublinked list at the first layer. By the way, we will recursively cut it down from the sublinked list, if any, or sublinked list.

So how can the tail pointer be properly placed? Think about it because it will keep separating from the back, without affecting the front, then we will directly use the last traversal as the tail pointer, and then it will be at the end of the original first layer.

Now that you have thought about it, try it.

 

Code:

  

/********************** Author: annsShadow Time = **************************/# include <stdio. h> # include <stdlib. h> // define the typedef struct chainNode {struct chainNode * next; struct chainNode * previous; struct chainNode * child; int value;} chainNode; /** why should I use a pointer to the pointer for tail, because I need to modify it! Otherwise, the changes in the function won't affect the original ones! ** // Append the link void appendChain (chainNode * child, chainNode ** tail) to the end of the linked list; // expand the multi-layer linked list to make it a layer of void expandChain (chainNode * head, chainNode ** tail); // separates the sublinked list from a chain list by void seperateChild (chainNode * childHead); // restores a chain table to a multi-linked list by void unexpandChain (chainNode * head, chainNode ** tail); int main () {chainNode * head, * tail; // construct the corresponding test multilayer linked list/** Some codes; **/expandChain (head, & tail); unexpandChain (head, & tail); return 0;} void appendChain (chainNode * child, chainNode ** tail) {// assign the current pointer to the input sub-linked list chainNode * currentNode = child; // assign the next value to the input sub-linked list (* tail) of the original tail pointer) -> next = child; // The previous value of the sub-linked list is child-> previous = * tail; // traverses the current sub-linked list until the last (; currentNode-> child; currentNode = currentNode-> next); // assign a value to generate a new tail pointer * tail = currentNode;} void expandChain (chainNode * head, chainNode ** tail) {// point the current pointer to the head pointer chainNode * currentNode = head; // The current pointer is not NULL while (currentNode) {// if the current pointer has a sublinked list if (currentNode-> child) {// append it to the appendChain (currentNode-> child, tail) at the end of the linked list );} // point to the next pointer currentNode = currentNode-> next;} void seperateChild (chainNode * childHead) {// assign the current pointer to the chainNode * currentNode = childHead; // The current pointer is not empty while (currentNode) {// if the current pointer has a sublinked list if (currentNode-> child) {// cancel the value appended to the 'Next' of the pointer // that is, cut off the linked list currentNode-> child-> previous-> next = NULL; // The sublinked list is separated from the primary linked list currentNode-> child-> previous = NULL; // if the current pointer has a sublinked list, the seperateChild (currentNode-> child) is further separated );} // point to the next pointer currentNode = currentNode-> next;} void unexpandChain (chainNode * head, chainNode ** tail) {// assign the current pointer to the head pointer chainNode * currentNode = head of the primary linked list; // start to separate seperateChild (head); // traverse the primary linked list to the end of the (; currentNode-> next; currentNode = currentNode-> next); // re-assign the tail pointer * tail = currentNode ;}

 

Postscript:

Sister paper thought the answer was satisfactory. Then she asked, what if it was one-way?

I will answer the question, so remember where to traverse each time;

What if the multi-layer structure is like this?

  

I want to answer another question: in this way, we can append a sub-linked list, and append an entire layer at a time. However, remember the position of the sub-linked list, which seems to have overhead, you can remember the first one. The first layer is connected.

Sister paper smiled.

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.