数据结构——双循环链表

#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
    int data;
    struct Node* pre;
    struct Node* next;
}Node;
Node* initList(){
    Node* List=(Node*)malloc(sizeof(Node));
    List->data=0;
    List->pre=List;
    List->next=List;
    return List;
}
void headInsert(Node* List,int data){
    Node* node=(Node*)malloc(sizeof(Node));
    node->data=data;
    if(List->data==0){//链表为空
        //这里node和List赋值的先后顺序可以随意
        List->next=node;
        List->pre=node;
        node->pre=List;
        node->next=List;
    }else{//链表不为空
        //注意node和List赋值的先后顺序,顺序不同全错,为什么???
        node->next=List->next;
        node->pre=List;

        List->next->pre=node;
        List->next=node;

    }
     List->data++;
}
void tailInsert(Node* List,int data){
    Node* node=(Node*)malloc(sizeof(Node));
    Node* head=List;
    head->data++;
    node->data=data;
    List=List->next;
    while(List->next!=head){
        List=List->next;
    }
    List->next=node;
    head->pre=node;
    node->next=head;
    node->pre=List;
}
void delete(Node* List,int data){
    List->data--;
    Node* node=List->next;
    while(node->data!=data){
        node=node->next;
    }
    node->pre->next=node->next;
    node->next->pre=node->pre;
    free(node);

}
void printList(Node* List){
    Node* node=List->next;
    while(node!=List){
        printf("%d->",node->data);
        node=node->next;
    }
    printf("over!\n");
}
int main() {
    Node* list=initList();
    headInsert(list,1);
    headInsert(list,2);
    headInsert(list,3);
    headInsert(list,4);


    tailInsert(list,0);
    tailInsert(list,5);
    tailInsert(list,6);
    tailInsert(list,7);
    tailInsert(list,8);
    delete(list,0);
    delete(list,1);
    delete(list,4);
    delete(list,8);
    printList(list);
    printf("链表共有%d个节点",list->data);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

karwen2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值