双向循环链表(带头结点)

//定义结构体

typedef struct data {

      int  data;

      struct data *pro;   //前驱

      struct data *next; //后继

}DATA;


//创建双向循环链表

DATA *creat () {


    DATA  *head, *node1, *node2;
    char choice;


//先创建一个空的双向循环链表

    head = node1 = malloc(N);

    head->next = head;
    head->pro = head;

    printf ("\n PLease input the data:   \n");
    do {
    
        node2 = malloc(N);

        scanf ("%d“,node -> data);

  

        node2->next = head;
        head->pro = node2;
        node2->pro = node1;
        node1->next = node2;
        node1 = node2;
        
        printf ("\nDo you want to continue?(y/n) \n");
        getchar();
        scanf ("%c", &choice);
    
    }while (choice == 'y' || choice == 'Y');

    return head;

}


//插入
int insert (DATA *head) {

    char choice;
    DATA *node, *node_insert;

    node = head->next;

    printf ("\n insert: \n");
    do {
            node_insert = malloc(N);

            printf ("\nPlease input the data you want to insert:\n");
            scanf ("%d", &node_insert -> data);
            printf ("%d", node_insert -> data);
    
            while (node != head  && node_insert -> data  < node -> data) {
        
                node = node->next;
        
            }
    
            node_insert->pro = node->pro;
            node->pro->next = node_insert;
            node_insert->next = node;
            node->pro = node_insert;

            printf ("\nDo you  want to continue?(y/n)");
            getchar();
            scanf ("%c", &choice);
    
        }while (choice == 'y' || choice == 'Y');


    return 0;
}

//删除  类比插入自行推理~


小编提示:不要忘记在创建在开始创建一个空的双向循环链表,小编之前就忘记了,结果出现大大的bug,后来,请教了小编的老师,才知道的。之前在网上搜索的感觉好像看不懂,而且还有bug,给老师看得时候,老师说“你连单向的都不清楚,先用单向吧”,所以,网上的程序,你们懂得,小编就不多说了,但是,小编的一定时=是正确的哦。还有,就是在交换的时候,注意指针的指向,最好画画图,搞清楚,不要绕晕啦,之前小编就晕呢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值