Single linked list + tail prime link

List initialization

Typedef struct Node{ElemType data; struct Node *next; }Node; typedef struct Node * LinkList;Copy the code

Circular linked lists create and insert new elements

/* Create a database for the first time. YES-> create a new node and make the next of the new node point to itself; (*L)->next = (*L); NO-> find the end of the list and place next = new. Next = (*L); */ int CreateList(LinkList *L){ int item; LinkList temp,target;printf("Enter the value of the node, enter 0 to end \n"); // LinkList r = NULL; While (1) {scanf("%d",&item); if(item==0) break; If (*L==NULL) {// If the input list is empty. Create a new Node, *L = (LinkList)malloc(sizeof(Node)); if(! L)exit(0); (*L)->data=item; (*L)->next=*L; (*head)->next=*head; //r = *L; } else {for (target = *L; target->next ! = *L; target = target->next); Temp =(LinkList)malloc(sizeof(Node)); if(! temp) return ERROR; temp->data=item; temp->next=*L; // Target ->next=temp; //r = temp; } } return 1; }Copy the code

Iterate over the circular linked list

// Iterate over a circular listdo whileVoid show(LinkList p) {// If the list is emptyif(p == NULL){
        printf("The printed list is empty! \n");
        return;
    }else{
        LinkList temp = p;
        do{
            printf("%5d",temp->data);
            temp = temp->next;
        }while(temp ! = p);printf("\n"); }}Copy the code

Circular linked list inserts data

Int ListInsert(LinkList *L, int place, int num){temp = (LinkList)malloc(sizeof(Node)); Create a new node temp and check whether it is created successfully. If it is created successfully, the value is assigned. Otherwise, ERROR is returned.if (temp == NULL) {
          return 0;
     }
    temp->data = num;
    LinkList target;
    if(place == 1) {// Place = 1for(target = *L; target->next ! = *L; target = target->next); ////2. Temp ->next = *L; Target ->next = temp; target->next = temp; //4. Next points to the new header; *L = temp; //5. Let the header pointer point to temp(temporary new node)}else {
       
        int i = 1;
        for(target = *L; target->next ! = *L && i < place - 1; target = target->next,i++); If the list length is longer, the queue will be automatically inserted into the end of the queue. temp->next = target->next; //3. Insert node's precursor to new node, new node's next to target's original next position; target->next = temp; // set target->next = temp; }return 1;
}
Copy the code

Loop over the linked list to delete elements

LinkListDelete1(LinkList *L,int place){LinkList temp = *L;if (temp == NULL) {
        return 0;
    }
    LinkList target;
    
    if (place == 1) {
        if(temp->next == *L) {temp->next == *L; *L = NULL;return 1;
        }
        
        for(target = *L; target->next ! = *L; target = target->next); *L = (*L)->next; Target ->next = *L; // end to end}else {
        int i = 1;
        for(target = *L; target->next ! = *L && i < place-1; target = target->next,i++); Temp = target->next; target->next = temp->next; // make target->next point to the next node} free(temp); / / releasereturn 1;
}
Copy the code
// loop the list to find the corresponding position based on the value. int findValue(LinkList L,int value){ int i = 1; LinkList p; p = L; // Find the node in the list data == valuewhile(p->data ! = value && p->next ! = L) { i++; p = p->next; } // If the end points to the head, it will jump out of the loop.if(p->next == L && p->data ! = value) {return- 1; }return i;
}
Copy the code