"Chaos" algorithm-two-way linked list

Source: Internet
Author: User

Disclaimer: copyright. You are welcome to reprint it. Contact mailbox: yiluohuanghun@gmail.com]

Two blog posts on "data structures and algorithms" have been posted before, and two bloggers have sent me private messages to discuss my classification, some Boyou said how can the data structure be integrated with algorithms? In fact, I feel that the relationship between data structures and algorithms is better than that between friends for a lifetime. They see the truth in their troubles, and they will never give up on life and death ...... In fact, data structures and algorithms are similar. Just talk about the data structure. We can introduce several important data structures in a short time. However, after hearing this, you may have no idea what the data structure is. However, if we combine the corresponding algorithms and give a demonstration, you will find that, even begin to feel, the original solution to computer problems is so wonderful!

This article mainly talks about two-way linked list.

A two-way linked list is also called a double-chain table, which is a type of linked list. Each of its data nodes has two pointers pointing to the direct successor and direct precursor respectively. Therefore, starting from any node in the two-way linked list, you can easily access its precursor node and successor node. A two-way linked list is a linear linked list that can travel (traverse) both in the front and back directions. Structure of each node in a two-way linked list:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16225340T-0.png "title =" double chain table 1.png "/>

A two-way linked list usually uses a circular linked list with table header nodes.

The general data structure is as follows:

typedef struct _DOUBLE_LINK_NODE{    int data;    struct _DOUBLE_LINK_NODE  *prev;    struct _DOUBLE_LINK_NODE  *next;};

Each node in the linked list has at least three fields: A two-way linked list has a header node, which is indicated by the first pointer of the table header of the linked list. Its data field or no data is stored, or store a data with special requirements. Its lLink points to the last node of the two-way linked list, and its lLink points to the first node at the frontend of the two-way linked list.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/162253G63-1.png "title =" double-oriented chain table 2.png "/>

Point to p = p → lLink → rLink = p → rLink → lLink

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1622532037-2.png "title =" double chain table 3.png "/>

There are many people wondering why there is a two-way linked list? Next we can look at another figure:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16225330M-3.png "title =" double-oriented chain table 4.png "/>

We call this train. I remember the first time when I was on a train, that is, the train was moving forward. After arriving at a station, I took another train, but what surprised me at the time was that the train was able to go back without turning around. Later I realized that the train was originally two heads that could connect to the locomotive, this may be the simplest two-way linked list instance.

Insert a two-way linked list

The insert operation is a little more complex than a single-chain table. The first step we need to do is to locate the position to insert and then point the back-drive of the inserted node to the back-drive of the current node, point the forward of the inserted node to the current node. Point the rear drive of the current node Back To The with-inserted node, and the front of the Back-drive node of the current node points to the node to be inserted. Does it sound a bit too winding? It doesn't matter! Use the diagram to illustrate the problem:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1622533F1-4.png "title =" double chain table 5.png "/>

The Code is as follows: // create a bidirectional linked list node _ DOUBLE_LINK_NODE * CreateDoubleLink (int data) {_ DOUBLE_LINK_NODE * head = (_ DOUBLE_LINK_NODE *) malloc (sizeof (_ DOUBLE_LINK_NODE )); ASSERT (head! = NULL); head-> data = data; head-> prev = NULL; head-> next = NULL; return head ;} // Insert the int partition (_ DOUBLE_LINK_NODE ** ppDLinkNode, int data) {_ DOUBLE_LINK_NODE * pNode; _ DOUBLE_LINK_NODE * pIndex; if (NULL = ppDLinkNode) return 0; if (NULL = * ppDLinkNode) {pNode = CreateDoubleLink (data); ASSERT (NULL! = PNode); * ppDLinkNode = pNode; (* ppDLinkNode)-> prev = (* ppDLinkNode)-> next = NULL; return 1;} if (NULL! = Find_data_in_double_link (* ppDLinkNode, data) return FALSE; pNode = CreateDoubleLink (data); ASSERT (NULL! = PNode); pIndex = * ppDLinkNode; while (NULL! = PIndex-> next) pIndex = pIndex-> next; pNode-> prev = pIndex; pNode-> next = pIndex-> next; pIndex-> next = pNode; return 1 ;}

The two-way linked list deletion operation is actually easier for other deletion and search operations after we have mastered the insert operation, so we can directly go to the Code:

// Delete the int vertex (_ DOUBLE_LINK_NODE ** ppDLinkNode, int data) {_ DOUBLE_LINK_NODE * pNode; if (NULL = ppDLinkNode | NULL = * ppDLinkNode) return 0; pNode = find_data_in_double_link (* ppDLinkNode, data); if (NULL = pNode) return 0; if (pNode = * ppDLinkNode) {if (NULL = (* ppDLinkNode)-> next) {* ppDLinkNode = NULL;} else {* ppDLinkNode = pNode-> next; (* ppDLinkNode) -> prev = NULL;} e Lse {if (pNode-> next) pNode-> next-> prev = pNode-> prev; pNode-> prev-> next = pNode-> next ;} free (pNode); return 1 ;}// search for data in a two-way linked list _ DOUBLE_LINK_NODE * find_data_in_double_link (const _ DOUBLE_LINK_NODE * pDLinkNode, int data) {_ DOUBLE_LINK_NODE * pNode =; if (NULL = pDLinkNode) return NULL; pNode = (_ DOUBLE_LINK_NODE *) pDLinkNode; while (NULL! = PNode) {if (data = pNode-> data) return pNode; pNode = pNode-> next;} return NULL ;} // count the number of data in the two-way linked list int round (const_DOUBLE_LINK_NODE * pDLinkNode) {int count = 0; _ DOUBLE_LINK_NODE * pNode = (_ DOUBLE_LINK_NODE *) pDLinkNode; while (NULL! = PNode) {count ++; pNode = pNode-> next;} return count ;}// print the data void print_double_link_node (const_DOUBLE_LINK_NODE * pDLinkNode) in the two-way linked list) {_ DOUBLE_LINK_NODE * pNode = (_ DOUBLE_LINK_NODE *) pDLinkNode; while (NULL! = PNode) {printf ("% d \ n", pNode-> data); pNode = pNode-> next ;}}

There are so many basic operations. Of course, we must remember to write test programs for ourselves. This is very important.

This article from the "Yi fall Dusk" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1262417

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.