链表的相关操作

定义

    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>

    typedef int DataType;

定义结构体

    typedef struct Node
    {
        DataType data;
        struct Node *next;
    }Node;

初始化

    void SListInit(Node **ppfirst)
    {
        assert(ppfirst);
        *ppfirst = NULL;
    }

销毁

    void SListDestory(Node **ppfirst)
    {
        assert(ppfirst);
        Node *cur, *next;
        for (cur = *ppfirst; cur != NULL; cur = next)
        {
            next = cur->next;
            free(cur);
        }
    }

注:销毁链表时一定要把每一个结点都释放

插入

头插
    static Node *CreateNewNode(DataType data)
    {
        Node *node = (Node *)malloc(sizeof(Node));
        node->data = data;
        node->next = NULL;
        return node;
    }

    void SListPushFront(Node **ppfirst, DataType data)
    {
        assert(ppfirst);
        Node *newNode = CreateNewNode(data);
        newNode->next = *ppfirst;
        *ppfirst = newNode;
    }
尾插
    void SListPushBack(Node **ppfirst, DataType data)
    {
        assert(ppfirst);
        Node *newNode = CreateNewNode(data);
        if (*ppfirst == NULL)
        {
            *ppfirst = newNode;
            return;
        }
        Node *cur = *ppfirst;
        while (cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next = newNode;
    }
随机插入(在结点pos前面插入)
    void SListInsert(Node **ppfirst, Node *pos, DataType data)
    {
        assert(ppfirst);
        Node *newNode = CreateNewNode(data);
        if (pos == *ppfirst)
        {
            SListPushFront(ppfirst, data);
            return;
        }
        Node *cur = *ppfirst;
        while (cur->next != pos)
        {
            cur = cur->next;
        }
        newNode->next = pos;
        cur->next = newNode;
    }

删除

头删
    void SListPopFront(Node **ppfirst)
    {
        assert(ppfirst); //断言不是空指针
        assert(*ppfirst); //断言不是空链表
        Node *first = *ppfirst;
        *ppfirst = (*ppfirst)->next;
        free(first);
    }
尾删
    void SListPopBack(Node **ppfirst)
    {
        assert(ppfirst); 
        assert(*ppfirst); 
        if ((*ppfirst)->next == NULL)
        {
            free(*ppfirst);
            *ppfirst = NULL;
            return;
        }
        Node *cur = *ppfirst;
        while (cur->next->next != NULL)
        {
            cur = cur->next;
        }
        free(cur->next);
        cur->next = NULL;
    }
随机删除(删除pos结点)
    void SListRemove(Node **ppfirst, Node *pos)
    {
        assert(ppfirst);
        assert(*ppfirst);
        if (*ppfirst == pos)
        {
            SListPopFront(ppfirst);
            return;
        }
        Node *cur = *ppfirst;
        while (cur->next != pos)
        {
            cur = cur->next;
        }
        cur->next = pos->next;
        free(pos);
    }

查找

    Node *SListFind(Node **ppfirst, DataType data)
    {
        assert(ppfirst);
        Node *cur = *ppfirst;
        while (cur->data != data || cur->next != NULL)
        {
            cur = cur->next;
        }
        if (cur->data == data)
        {
            return cur;
        }
        return NULL; //找不到返回NULL
    }

删除(第一个数值为data的结点)

    void SListRemoveFirst(Node **ppfirst, DataType data)
    {
        assert(ppfirst);
        assert(*ppfirst);
        Node *cur = *ppfirst;
        Node *node = *ppfirst; //记录cur的前一个
        while (cur->data != data && cur->next !=  NULL)
        {
            cur = cur->next;
        }
        if (cur->next == NULL)
        {
            printf("不存在\n");
            return;
        }
        if (cur == *ppfirst)
        {
            SListPopFront(ppfirst);
            return;
        }
        while (node->next != cur)
        {
            node = node->next;
        }
        node->next = cur->next;
        free(cur);
    }

删除(所有值为data的结点)

    void SListRemoveAll(Node **ppfirst, DataType data)
    {
        assert(ppfirst);
        assert(*ppfirst);
        Node *cur = *ppfirst;
        Node *node = *ppfirst;
        while (cur->next != NULL)
        {
            node = *ppfirst;
            if (cur->data == data)
            {
                if (cur == *ppfirst)
                {
                    cur = cur->next;
                    SListPopFront(ppfirst);
                    continue;
                }
                while (node->next != cur)
                {
                    node = node->next;
                }
                node->next = cur->next;
                free(cur);
                cur = node->next;
                continue;
            }
            cur = cur->next;
        }
        if (cur == *ppfirst &&  cur->data == data)
        {
            SListPopFront(ppfirst);
        }
    }

注:在链表中,插入元素一定要先申请空间,删除元素一个要记得释放

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值