C语言双向循环链表

// Created on iPad.

#include <iostream>
using namespace std;

typedef struct DulNode{
    int data;
    struct DulNode *prior;
    struct DulNode *next;
}DulNode;
//初始化链表
void InitList(DulNode *head,int size){
    DulNode *rear=head;
    rear->prior=NULL;
    for(int i=2;i<=size;i++){
        DulNode *newnode=(DulNode*)malloc(sizeof(DulNode));
        newnode->next=NULL;
        newnode->data=i;
        rear->next=newnode;
        newnode->prior=rear;
        rear=newnode;
    }
    rear->next=head;
    head->prior=rear;
}
//插入元素,头插法
void Listinsert_dul(DulNode *head,int i,int e){
    int j=0;
    DulNode *p=head;
    DulNode *newnode=(DulNode*)malloc(sizeof(DulNode));
    while(p!=NULL&&j<i-1){
        p=p->next;
        j++;
    }
    newnode->data=e;
    newnode->prior=p;
    newnode->next=p->next;
    p->next->prior=newnode;
    p->next=newnode;
}
//遍历链表
void travelLink(DulNode *head){
    DulNode *p=head->next;
    while(p!=head){
        printf("%d\t",p->data);
        p=p->next;
    }
    cout<<endl;
}
//查询链表
DulNode query(DulNode *head,int i){
    DulNode *p=head;
    while(p&&p->data!=i)
        p=p->next;
    return p;
}
//删除元素
void delete_dul(DulNode *head,int i){
    DulNode *p=head->next;
    while(p&&p->data!=i)
        p=p->next;
    p->next->prior=p->prior;
    p->prior->next=p->next;
    delete p;
}
int main() {
    DulNode *head=(DulNode*)malloc(sizeof(DulNode));
    head->prior=NULL;
    head->next=NULL;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值