Regression data structure (4): Two-way circular linked list

Source: Internet
Author: User

Insert the bidirectional loop linked list, delete the operation as shown in figure:

#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct lnode{int data;
    struct Lnode *next;
struct Lnode *prior;

}node,*linklist;
    int createlinklist (linklist &l,int length) {linklist head,temp,hmove;
    L = (linklist) malloc (sizeof (Node)); if (!
        L) {printf ("malloc action error");
    return 0;
    } l->next = L;
    L->prior = L;
    Hmove = head = L;

    int j=0;
        for (; j<length;j++) {temp = (linklist) malloc (sizeof (Node));
           if (!temp) {printf ("malloc action error");
        return 0;
        } scanf ("%d", &temp->data);
        Temp->next = head;
        Head->prior = temp;
        Temp->prior = Hmove;
        Hmove->next = temp;
    Hmove = temp;
} return 1;
    } int printlinklist (linklist L) {linklist head;
    head = L;
    Head = head->next;
        while (head!=l) {printf ("%d", head->data);
Head = head->next;    } return 1;
    } int insertlinklist (linklist l,int i,int Element) {linklist head,temp;
    head = L;
    Head = head->next;
    int j = 0;
        while (j<i-1) {++j;
        Head = head->next;
        if (head = = L) {break;
        }} if (j<i-1) {printf ("Insert ERROR");
    return 0;
        temp = (linklist) malloc (sizeof (Node));
           if (!temp) {printf ("malloc action error");
        return 0;
    } Temp->data = element;
    Temp->next = head->next;
    Temp->prior =head;
    Head->next = temp;
    Temp->next->prior = temp;
return 1;
    } int deletelinklist (linklist l,int i) {linklist head;
    int j = 0;
    head = L;
    Head = head->next;
        while (j<i-1) {++j;
        Head = head->next;
        if (head==l) {break;
        }} if (j<i-1) {printf ("Insert ERROR");
    return 0;
    } printf ("%d\n", head->data); Head->prior->next = head->next;
    Head->next->prior = head->prior;
    Free (head);
return 1;
    } int main () {linklist L;
    int length;
    printf ("Create linklist\n");
    scanf ("%d", &length);
    Createlinklist (l,length);

    Printlinklist (L);
    printf ("\ninsert linklist\n");
    int index,element;
    scanf ("%d%d", &index,&element);
    Insertlinklist (l,index,element);

    Printlinklist (L);
    printf ("\ndelete linklist\n");
    scanf ("%d", &index);
    Deletelinklist (L,index);


Printlinklist (L);
 }

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.