C-language bidirectional circular link list

Source: Internet
Author: User

Two-way loop linked list, first of all, two-way linked list, two-way linked list is also called doubly linked list, is a list of links, each of its data nodes have two pointers, respectively, pointing to direct successor and direct precursor. Therefore, starting from any node in a doubly linked list, it is easy to access its precursor nodes and subsequent nodes. and the circular link list before also said, the single linked list is to let the last node pointer to the first node can constitute a circular chain list, here is actually the same, but one more step, It's OK to point the precursor of the first node to the last node, (here is the two-way loop linked list that leads to the node, so use the first node and the head node to differentiate between the two). Let's look directly at how to create a two-way loop linked list of leading nodes.

1#include <stdio.h>2#include <stdlib.h>3 structData4 {5     intnum;6     structData *front;//Point to the node before it7     structData *next;//the node that points after it8 };9 intMain ()Ten { One     intn=1; A     structData *p,*p1,*head; -P=head= (structData *) malloc (sizeof(structdata));//create a two-way loop linked list of lead nodes -p->next=null;//Set the pointer to the front and back to null at the beginning theP->front=NULL; -P1= (structData *) malloc (sizeof(structdata));//Create a new node to add data to the linked list -p1->num=1;  -      while(n<=Ten) +     { -n++; +p->next=p1;//let the previous node P's tail pointer point to this node P1 Ap1->front=p;//The head pointer of this node points to the previous node . atP=P1;//p1 change to P -P1= (structData *) malloc (sizeof(structdata));//Create a new node P1, assign it and repeat the loop above -p1->num=N; -     } -p->next=head->next;//Finally, because it is a double-loop linked list with the lead node, the last node's tail pointer points to the node after the head. -head->next->front=p;//Point the head of the node after head to the tail node . in Free (p1); -     return 0; to  +}

The two-way loop list can access the previous node of any node at any time, so it is convenient to find it. Its deletion and addition are similar to that of a single-linked list, except that there are two pointer fields to which they should be re-directed to the new node. There's one more thing to note, That is, if the node you want to delete is the first node, you need to re-point the head node. Just a little bit of code.

1  while(1)//Loop to find the nodes that need to be deleted2     {3         if(p->num==5)//look for the conditions4         {5             if(head->next==p)//to determine if the deleted node is the first node .6             {7head->next=p->next;//if it is the first node, let the head node point to the node after it8             }9p->front->next=p->next;//the previous node of the current node is P->front, which points to the next node of the current node P->nextTenp->next->front=p->front;//The P->next of the next node of the current node P points to its previous node p->front; so it deletes the node. One              Break; A         } -P=p->Next; -          the}

OK, good refueling!!! ^_^

C-language bidirectional circular link list

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.