双向链表和循环双向链表的一些简单操作集

双向链表

  • int inseser_debut_liste(Liste *list, int valeur) //插头
  • int inseser_fin_liste(Liste *list, int valeur) //插尾
  • int supprimer(Liste*list,int position) //删除
typedef struct Element
{
    int valeur;
    struct Element *suivant;
    struct Element *precedent;
}Element;

typedef struct Liste
{
    Element *tete;   //头
    Element *queue;  //尾
    int taille;     //元素个数
}Liste;

int inseser_debut_liste(Liste *list, int valeur) //插头
{
    Element *p=malloc(sizeof(Element));
    if(p==NULL) return -1;
    p->valeur=valeur;
    if(list->tete)
        list->tete->precedent=p;
    else
        list->queue=p;
    p->suivant=list->tete;
    list->tete=p;
    p->precedent=NULL;
    list->taille++;
    return 0;
}

int inseser_fin_liste(Liste *list, int valeur) //插尾
{
    Element *p=malloc(sizeof(Element));
    if(p==NULL) return -1;
    p->valeur=valeur;
    if(list->queue)
        list->queue->suivant=p;
    else
        list->tete=p;
    p->precedent=list->queue;
    list->queue=p;
    p->suivant=NULL;
    list->taille++;
    return 0;
}

int supprimer(Liste*list,int position)
{
    int k=1;
    if(list->nb==0||position<=0||position>list->taille)
        return -1;
    Element *p=list->tete;
    while(k<position)
    {
        p=p->suivant;
        k++;
    }
    if(p->precedent)
        p->precedent->suivant=p->suivant;
    else
        list->tete=p->suivant;
    if(p->suivant)
        p->suivant->precedent=p->precedent;
    else
        list->queue=p->precedent;
    list->taille--;
    free(p);
}
循环双向链表
建立哨兵sentinelle的双向列表
  • T_list * Creer_list()  //创建list
  • void Inserer_Tete(T_list *l, int x) //插头
  • void Inserer_Queue(T_list *l, int x)  //插尾
  • void Print_list(T_list *l)
  • void Supprimer_Cellule(T_cell *c)  //删除单元
  • T_cell *Chercher_Val(T_list *l, int val)  //查找
  • void Supprimer_Val(T_list *l, int val) 
  • int Liste_Vide(T_list *l) //判断是否为空
Figure1:插头 Figure 2:插


typedef struct cellule
{
    int clé;
    struct cellule *succ;
    struct cellule *pred;
}T_cell;
typedef struct liste
{
    T_cell *sentinelle;
}T_list;
T_list * Creer_list()
{
    T_list *l;
    T_cell *c;
    l=(T_list *)malloc(sizeof(T_list));
    c=(T_cell *)malloc(sizeof(T_cell));
    l->sentinelle=c;
    c->pred=c;
    c->succ=c;
    return(l);
}
void Inserer_Tete(T_list *l, int x)
{
    T_cell *c, *sentinelle;
    c=(T_cell *)malloc(sizeof(T_cell));
    c->clé=x;
    sentinelle=l->sentinelle;
    c->succ=sentinelle->succ;
    c->pred=sentinelle;
    sentinelle->succ->pred=c;
    sentinelle->succ=c;
}

void Inserer_Queue(T_list *l, int x)
{
    T_cell *c, *sentinelle;
    c=(T_cell *)malloc(sizeof(T_cell));
    c->clé=x;
    sentinelle=l->sentinelle;
    c->succ=sentinelle;
    c->pred=sentinelle->pred;
    sentinelle->pred->succ=c;
    sentinelle->pred=c;
}

void Print_list(T_list *l)
{
    T_cell *c;
    c=l->sentinelle->succ;
    while(c!=l->sentinelle)
    {
        printf("%d ",c->clé);
        c=c->succ;
    }
}


void Supprimer_Cellule(T_cell *c)
{
    c->pred->succ=c->succ;
    c->succ->pred=c->pred;
}

T_cell *Chercher_Val(T_list *l, int val)
{
    T_cell *c, *sentinelle;
    sentinelle=l->sentinelle;
    c=sentinelle->succ;
    while((c!=sentinelle)&&(c->clé!=val))
        c=c->succ;
    if(c==sentinelle)
        return NULL;
    else
        return c;
}

void Supprimer_Val(T_list *l, int val)
{
    T_cell *c;
    c=Chercher_Val(l,val);
    if(c!=NULL)
    {
        Supprimer_Cellule(c);
        free(c);
    }
}
int Liste_Vide(T_list *l)
{
    return(l->sentinelle->pred==l->sentinelle->succ);
}





   

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值