双向链表的有关内容

#include<stdio.h>
#include<stdlib.h>
//节点结构体
typedef int datatype_t 
typedef struct node_t
{
    datatype_t data;
    struct node_t *prior;
   
    struct node_t *next;
}link_t;
//保存头尾节点地址及长度的结构体
typedef struct 
{
   link_t *head;
   link_t *tail;
   int len;
}double_t;

//创建一个空的结构体
double_t *createDoubleLink(void)
{
 //1开辟保存头尾节点地址及长度的结构体空间
 double_t *p=(double_t *)malloc(sizeof(double_t));
 if (NULL==p)
 {
    printf("malloc double err\n");
    return NULL;
 }
 //初始化-创建一个无效头节点
 p->head=p->tail=(link_t *)malloc(sizeof(link_t));
 if (NULL==p->head)
 {
     printf("malloc node err\n");
     free(p);
     return NULL;
 }
 //初始化节点 空链表
 p->head->prior=NULL;
 p->head->next=NULL;
 p->len=0;
 return p;
}

int insertPost(double_t *p,int post)
{
  //容错处理
    link_t *pt=NULL;//暂时保存头或尾节点的地址,进行遍历
    if (post<0 || post>p->len)
    {
       printf("post err\n");
       return -1;
    }
    //创建一个新节点保存插入的数据,并初始化
    link_t *pnew=(link_ *)malloc(size(link_t));
    if (NULL==pnew)
    {
       printf("malloc node err\n");
       return -1;
    }
    pnew->prior=pnew->next=NULL;
    pnew->data=data;
   //3。判断是尾插,还是中间插入
   if (post==p->len)
   {
       p->tail->next=pnew;
       pnew->prior=p->tail;
       p->tail=pnew;
   }
   else
   {
       //4。判断是从头遍历到指定位置前一个位置插入,还是从尾遍历到指定位置前一个位置
       //插入方式:1》》移动到指定位置往前插(先连前边在连后边)
       //2。》》移动到指定位置前一个位置往后插(先连后边在连前边)
       if (post<p->len/2)
       {  //头开始
         pt=p->head;
         for (int i=0;i<post;i++)
         {
             pt=pt->next;
         }
       }
         else
         {
             pt=p->tail;
             for(int i=0;i<p->len-post;i++)
             {
                 pt=pt->prior;
             }
         }
    //5.插入,先连后边在连前边
        pnew->prior=pt->next->prior;
        pt->next->prior=pnew;
        pnew->next=pt->next;
        pt->next=pnew;
   }
     p->len++;
    return 0;
}
//3.遍历双向链表
void showDouble(double_t *p)
{
    //正向遍历
    link_t *pt = NULL;
    pt = p->head;
    while (pt->next != NULL)
    {
        pt = pt->next;
        printf("%d ", pt->data);
    }
    putchar(10);
    //反向遍历
    pt = p->tail;
    while (pt->prior != NULL)
    {
        printf("%d ", pt->data);
        pt = pt->prior;
    }
    putchar(10);
}
//4.指定位置删除 -双向链表
int delectPost(double_t *p, int post)
{
    link_t *pdel = NULL;
    if (post < 0 || post >= p->len)
    {
        printf("delect post err.\n");
        return -1;
    }
    //判断尾删,中间删除
    if (post == p->len - 1)
    {
        pdel = p->tail;
        p->tail = p->tail->prior;
        p->tail->next = NULL;
    }
    else
    {
        //将pdel移动删除节点位置
        //从头
        if (post < p->len / 2)
        {
            pdel = p->head;
            for (int i = 0; i <= post; i++)
            {
                pdel = pdel->next;
            }
        }
        else
        {
            pdel = p->tail;
            for (int i = 0; i < p->len - post - 1; i++)
            {
                pdel = pdel->prior;
            }
        }
        //跨过这个节点
        pdel->prior->next = pdel->next;
        pdel->next->prior = pdel->prior;
    }
    //释放节点空间
    free(pdel);
    pdel = NULL;

    p->len--;
    return 0;
}
// 5.判断双向链表是否为空
int isEmptyDouble(double_t *p)
{
    return p->head == p->tail;
}
// 	6.求双向链表的长度
int lenDouble(double_t *p)
{
    return p->len;
}

// 	7.查找指定数据出现的位置
int searchData(double_t *p, datatype_t data)
{
    if (isEmptyDouble(p))
    {
        printf("is empty.\n");
        return -1;
    }
    int post = 0;
    link_t *pt = p->head;
    while (pt->next != NULL)
    {
        pt = pt->next;
        if (pt->data == data)
        {
            return post;
        }
        post++;
    }
    return -1;
}
// 	8.修改指定位置的数据
int changePost(double_t *p, int post, datatype_t data)
{
    if (post < 0 || post >= p->len)
    {
        printf("post err.\n");
        return -1;
    }
    link_t *pdel=NULL;
    if (post < p->len / 2)
    {
        pdel = p->head;
        for (int i = 0; i <= post; i++)
        {
            pdel = pdel->next;
        }
    }
    else
    {
        pdel = p->tail;
        for (int i = 0; i < p->len - post - 1; i++)
        {
            pdel = pdel->prior;
        }
    }
    pdel->data=data;
    return 0;
}
// 	9.删除双向链表中的指定数据              *******
int delectData(double_t *p,datatype_t data)
{
    if(isEmptyDouble(p))
    {
        printf("is empty.\n");
        return -1;
    }
    int post=0;
    link_t *pt=p->head;
    while(pt->next != NULL)
    {
        pt=pt->next;
        if(pt->data == data)
        {
            pt=pt->prior;
            delectPost(p,post);
        }else 
         post++;
    }
}
// 	10.转置双向链表
void resortDouble(double_t *p)
{
    link_t *pt=NULL;//暂时保存砍下的头
    link_t *ph=NULL;//保存断开之后的无头双向链表
    if(isEmptyDouble(p))
    {
        printf("is empty.\n");
        return ;
    }
    //1.断开
    ph=p->head->next;
    p->tail=p->head;
    p->head->next=NULL;
    p->len=0;

    //2.循环无头单向链表砍头插入到有头双向链表的头节点之后
    while(ph !=NULL)
    {
        pt=ph;
        ph=ph->next;
        pt->next=pt->prior=NULL;
        if(p->len == 0)
        {
            p->tail->next=pt;
            pt->prior=p->tail;
            p->tail=pt;
        }else
        {
            pt->prior=p->head->next->prior;
            p->head->next->prior=pt;
            pt->next=p->head->next;
            p->head->next=pt;
        }
        p->len++;
    }  
}


int main(int argc, char const *argv[])
{
    double_t *p = createDoubleLink();
    insertPost(p, 0, 10);
    insertPost(p, 1, 20);
    insertPost(p, 2, 30);
    insertPost(p, 1, 100);

    showDouble(p);
    //delectPost(p, 1);
    resortDouble(p);
    showDouble(p);
    return 0;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值