双链表实例(C语言)

双链表基本操作

编译环境:VS !!
对应操作已写注解!!

#include <stdio.h>
#include <stdlib.h>
typedef char datatype; //定义数据域类型结构
typedef struct node
{
    datatype data;
    struct node *prev;
    struct node *next;
} listnode;
//创建新结点
listnode *buylsitnode(datatype d)
{
    listnode *newnode = (listnode *)malloc(sizeof(listnode));
    if (newnode == NULL)
    {
        printf("内存空间分配失败");
        exit(1);
    }
    newnode->data = d;
    newnode->next = NULL;
    newnode->prev = NULL;
    return newnode;
}
//获取表长度
int lengthlist(listnode *head)
{
    listnode *cur = head;
    int j = 0;
    while (cur != NULL)
    {
        cur = cur->next;
        j++;
    }
    return j;
}
//表头插入
void insertelematstart(listnode **head, datatype d)
{
    listnode *newnode = buylsitnode(d);
    if (*head == NULL)
    {
        *head = newnode;
    }
    else
    {
        listnode *cur = *head;
        newnode->next = cur;
        cur->prev = newnode;
        cur = newnode;
        // printf("%c\n", cur->next->data);
    }
}
//表尾插入
void insertelematend(listnode **head, datatype d)
{
    listnode *newnode = buylsitnode(d);
    if (*head == NULL)
    {
        *head = newnode;
    }
    else
    {
        listnode *cur = *head;
        while (cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next = newnode;
        newnode->prev = cur;

    }
}
//指定位置插入
void insertelem(listnode **head, datatype d, int p)
{
    listnode *newnode = buylsitnode(d);
    int length = lengthlist(*head);
    if (*head == NULL)
    {
        *head = newnode;
    }
    if (p > length || p < 0)
    {
        printf("插入失败,超出索引范围\n");
        exit(1);
    }
    listnode *cur = *head;
    if (p == 0)
    {
        newnode->next = cur;
        cur->prev = newnode;
        cur = newnode;
        printf("插入成功\n");
    }
    else if (p == length)
    {
        while (cur->next != NULL)
        {

            cur = cur->next;
        }
        cur->next = newnode;
        newnode->prev = cur;
         printf("插入成功\n");
    }
    else
    {
        int j = 1;
        while (j != p)
        {
            cur = cur->next;
            j++;
        }
        cur->next->prev = newnode;
        newnode->next = cur->next;
        cur->next = newnode;
        newnode->prev = cur;
         printf("插入成功\n");
    }
}
//删除元素
void delectelem(listnode **head, int p)
{
    if (*head == NULL)
    {
        printf("不为空,删除失败");
        exit(1);
    }
    int length = lengthlist(*head);
    length--;
    if (p > length || p < 0)
    {
        printf("删除失败,超出索引范围\n");
        exit(1);
    }
    listnode *cur = *head;
    if (p == 0)
    {

        cur->next->prev = cur->prev;
        cur->prev->next = cur->next;
        free(cur);
        printf("删除成功\n");
    }
    else if (p == length)
    {
        while (cur->next != NULL)
        {

            cur = cur->next;
        }
        cur->prev->next = NULL;
        printf("删除成功\n");
    }
    else
    {
        int j = 0;
        while (j != p)
        {
            cur = cur->next;
            j++;
        }
        cur->next->prev = cur->prev;
        cur->prev->next = cur->next;
        printf("删除成功\n");
    }
}
//修改元素
void changeelem(listnode **head, int p, datatype d)
{
    if (*head == NULL)
    {
        printf("不为空,修改失败\n");
        exit(1);
    }
    int length = lengthlist(*head);
    length--;
    if (p > length || p < 0)
    {
        printf("修改失败,超出索引范围\n");
        exit(1);
    }
    listnode *cur = *head;
    int j = 0;
    while (j != p)
    {
        cur = cur->next;
        j++;
    }
    cur->data = d;
    printf("修改成功\n");
}
//遍历打印
void print(listnode *head)
{
    listnode *cur = head;
    while (cur != NULL)
    {

        printf("%0.4c", cur->data);
        cur = cur->next;
    }
    printf("\n");
}

//测试
int main(int argc, char const *argv[])
{
    listnode *head = NULL;
    //表头插入操作测试
    insertelematstart(&head, 'c');
    print(head);
    //表尾插入操作测试
    insertelematend(&head, 'd');
    insertelematend(&head, 'e');
    insertelematend(&head, 'f');
    insertelematend(&head, 'g');
    print(head);
    //指定位置插入操作测试
    insertelem(&head, 'p', 1);
    print(head);
    //删除操作测试
    delectelem(&head, 2);
    print(head);
    //修改操作测试
    changeelem(&head, 0, 'o');
    print(head);
    //长度打印
    printf("%d\n", lengthlist(head));
    return 0;
}

运行结果

在这里插入图片描述
该文章作为学习记录,水平有限。如有差错,请多多指点!!🐵

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值