Doubly linked list and circular link list

Source: Internet
Author: User

Double-linked list definition


A doubly linked list adds a pointer field to a single-linked table node that points to the precursor of the current node. In this way, it is convenient for the successor to find its precursor, and the data sequence of the output terminal node to the beginning node can be realized.

Similarly, a doubly linked list is divided into a double-linked list of lead nodes and a doubly linked list without a lead node, similar to a single linked list. The linked list is empty when the doubly linked list of the lead node head->next is null. The linked list is empty when the double-linked list head is null without the lead node.

1. Using the tail interpolation method to establish double-linked list
void CreateDlistR (DLNode *&L, int a[], int n){ DLNode*s,*r; inti; L = (DLNode*)malloc(sizeof(DLNode)); L->next = NULL; //和单链表一样r始终指向终端结点,开始头结点也是尾结点 r = L; for(i = 1; i< = n; i++){ //创建新结点s->data = a[i]; s = (DLNode*)malloc(sizeof(DLNode)); /*下边3句将s插入在L的尾部并且r指向s,s->prior = r;这一句是和建立单链表不同的地方。 */ r->next = s; s->prior = r; r = s; } r->next = NULL;}
2. Algorithm for finding nodes

Finds the node with the first node value x in a doubly linked list. Starting with the first node, the Edge scan edge is compared, and if such a node is found, the node pointer is returned, otherwise null is returned. The algorithm code is as follows:

DLNode* Finfnode(DLNode *C, int x){     DLNode *p = C->next;    while(p != NULL){         if(p->data == x) {            break;        }        p = p->next;    }     return p; //如果找到则p中内容是结点地址(循环因break结束), //没找到 p中内容是NULL(循环因p等于NULL而结束) //因此这一句可以将题干中要求的两种返回值的种情况统一。 }
3. Algorithm for inserting nodes

Suppose that a node s is inserted after the node referred to by P in a doubly linked list, and its action statement is described as:

s->next = p->next;s->prior = p;p->next = s;s->next->prior = s;

Pointer change process:

If inserted in the order above, it can be seen as a universal insertion method. The nodes that are to be inserted are linked on both sides to ensure that no nodes can be found after the link has been broken.

4. Algorithm for deleting nodes

To delete the successor node of the P-node in the doubly-linked list, the following statements are:

q= p->next;p->next= q->next;q->next->prior= p;free(q);

Pointer change process:

Circular single-linked list

Simply point the last pointer field (null pointer) of the single-linked list to the first node in the list (the reason why the first node is not the head node is because if the loop single-linked list is the lead node, the pointer field of the last node points to the head node; if the circular single-linked list does not lead the node, The last pointer field to point to the start node).

The loop list of the leading node is empty when head equals Head->next;
Loop single linked list without the lead node when head equals NULL, the list is empty.

Circular doubly linked list


The construction of the cyclic doubly linked list originates from the doubly linked list, which points to the first node in the linked list, and the prior pointer of the first node in the list points to the terminal node, which is the NNext pointer to the terminal node.

The loop doubly linked list of the leading nodes when the Head->next and heaad->prior two pointers are equal to the head, the linked list is empty.
The loop double-linked list without the lead node is empty when the head equals null.

Algorithm operation of circular list

Circular single-linked lists and circular double-linked lists are transformed by the corresponding single-linked lists and double-linked lists, simply by establishing a connection between the terminal node and the head node.

The next node pointer of the terminal node of the circular single-link list points to the table header node; the next pointer to the terminal node of the cyclic doubly linked list points to the table header node, and the prior pointer to the head node points to the footer node.

If the P-pointer walks along a circular-linked list, the condition that p goes to the end-of-table junction is p->next == head . The various operations of the circular list are similar to the non-circular linked list.

Doubly linked list and circular link list

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.