04.17

本文详细介绍了使用C语言实现的双向链表操作,包括头结点创建、节点创建、判断空列表、头尾插入和删除、以及按位置插入和删除功能,展示了顺序表和单向链表的特点和区别。
摘要由CSDN通过智能技术生成

#include "double_list.h"
//创建双向链表头结点
node_p create_double()
{
    node_p H=(node_p)malloc(sizeof(node));
    if(H==NULL)
    {
        printf("入参空\n");
        return NULL;
    }
    H->next=NULL;
    H->pri=NULL;
    return H;
}
//创建节点
node_p create_node(int data)
{
    node_p new=(node_p)malloc(sizeof(node));
    if(new==NULL)
    {
        printf("创建失败\n");
        return NULL;
    }
    new->data=data;
        return new;
}
//判空
int empty_double_list(node_p H)
{   
    if(H==NULL||H->next==NULL)
    {
        printf("入参为空\n");
        return -1;
    }
    return 0;
}
//头插
void insert_head(node_p H,int data)
{
    if(H==NULL)
    {
        printf("头结点为空\n");
        return;
    }
    node_p new=create_node(data);
    if(new==NULL)
    {
        printf("节点创建失败\n");
    }
    new->next=H->next;
    if(H->next!=NULL)
    {
        H->next->pri=new;
    }
    new->pri=H;
    H->next=new;
    H->data++;
}
//输出
void show(node_p H)
{
    if(H==NULL)
    {
        printf("入参为空\n");
        return;
    }
    node_p p=H->next;
    for(;p!=NULL;p=p->next)
    {
        printf("%d->",p->data);
    }
    printf("NULL\n");
}
//头删
void dele_head(node_p H)
{
    if(H==NULL)
    {
        printf("入参为空\n");
        return;
    }
    node_p p=H->next;
    H->next->next->pri=H->next->pri;
    H->next=H->next->next;
    free(p);
}
//尾插
void insert_tail(node_p H,int data)
{
    if(H==NULL)
    {
        printf("入参为空\n");
        return;
    }
    node_p new=create_node(data);
    node_p p=H->next;
    for(;p->next!=NULL;p=p->next);
    new->next=p->next;
    new->pri=p;
    p->next=new;
    H->data++;

}
//尾删
void dele_tail(node_p H)
{
    if(H==NULL)
    {
        printf("入参为空\n");
        return;
    }
    if(empty_double_list(H))
    {
        printf("表空\n");
        return;
    }
    node_p p=H;
    for(;p->next!=NULL;p=p->next);
    p->pri->next=NULL;
    free(p);
}
//按位置插入
void insert_pos(node_p H,int data,int pos)
{
    if(H==NULL)
    {
        printf("入参为空\n");
        return;
    }
    if(pos<1||pos>H->data+1)
    {
        printf("位置不合理\n");
        return;
    }
    node_p p=H;
    for(int i=0;i<pos-1;i++,p=p->next);
    node_p new=create_node(data);
    new->next=p->next;
    new->pri=p;
    if(p->next!=NULL)
    {
    p->next->pri=new;
    }
    p->next=new;
}
//按位置删除
 void dele_pos(node_p H,int pos)
{
    if(H==NULL)
    {
        printf("入参为空\n");
        return;
    }
    if(pos<1||pos>H->data)
    {
        printf("位置不合理\n");
        return;
    }
    node_p p=H;
    for(int i=0;i<pos-1;i++,p=p->next);
    node_p del=p->next;
    del->pri=p;
    p->next=del->next;
    free(del);
    H->datad
}

1.顺序表:数据之间逻辑连续物理地址也连续,插入删除需要遍历,支持随机访问。
2.单向链表:链式存储,在逻辑上连续,物理地址不连续,单个结点创建和删除,不会造成内存空间浪费。

  • 19
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值