(无头)单链表的插,删,查,改及相关测试用例

SList.h

typedef int DataType;

typedef struct SListNode
{
    struct SListNode* _next;
    DataType _data;
}SListNode;

SListNode* BuySListNode(DataType x);
void SListPrint(SListNode* pHead);
void SListPushBack(SListNode** ppHead, DataType x);
void SListPopBack(SListNode** ppHead);
void SListPushFront(SListNode** ppHead, DataType x);
void SListPopFront(SListNode** ppHead);
SListNode* SListFind(SListNode* pHead, DataType x);
void SListInsert(SListNode** ppHead, SListNode* pos, DataType x);
void SListErase(SListNode** pHead,SListNode* pos);

//创建结点
SListNode* BuySListNode(DataType x)
{
    SListNode* node = (SListNode*)malloc(sizeof(SListNode));

    node->_data = x;
    node->_next = NULL;

    return node;
}

//打印链表
void SListPrint(SListNode* pHead)
{

    SListNode* cur = pHead;
    while (cur)
    {
        printf("%d ", cur->_data);
        cur = cur->_next;
    }
    printf("\n");
}

//尾插
void SListPushBack(SListNode** ppHead, DataType x)
{
    assert(ppHead);
    if (*ppHead == NULL)
    {
        *ppHead = BuySListNode(x);
    }
    else
    {
        SListNode* tail = *ppHead;
        while (tail->_next)
        {
            tail = tail->_next;

        }
        tail->_next = BuySListNode(x);
    }
}
//尾删
void SListPopBack(SListNode** ppHead)
{
    assert(ppHead);
    //空
    if (*ppHead == NULL)
    {
        return;
    }
    else if ((*ppHead)->_next == NULL)// 一个节点,直接删除,如果略过此种情况,程序崩溃
    {
        free(*ppHead);
        *ppHead = NULL;
    }
    else
    {
        //一个及以上节点
        SListNode* prev = NULL;
        SListNode* tail = *ppHead;
        while (tail->_next)
        {
            prev = tail;
            tail = tail->_next;
        }
        prev->_next = NULL;
        free(tail);
    }
}

void Test1()
{
    SListNode* list = NULL;

    SListPushBack(&list, 1);
    SListPushBack(&list, 2);
    SListPushBack(&list, 3);
    SListPushBack(&list, 4);

    SListPrint(list);

    SListPopBack(&list);
    SListPopBack(&list);
    SListPopBack(&list);
    SListPopBack(&list);

    SListPrint(list);

}

这里写图片描述

//头插
void SListPushFront(SListNode** ppHead, DataType x)
{
    //空
    if (*ppHead == NULL)
    {
        *ppHead = BuySListNode(x);
    }
    else//非空
    {
        SListNode* newNode = BuySListNode(x);
        newNode->_next = *ppHead;
        *ppHead = newNode;
    }
}

//头删
void SListPopFront(SListNode** ppHead)
{
    //空
    if (*ppHead == NULL)
    {
        return;
    }
    //一个
    else if ((*ppHead)->_next == NULL)
    {
        free(*ppHead);
        *ppHead = NULL;
    }
    //一个及以上
    else
    {
        SListNode* next = (*ppHead)->_next;
        free(*ppHead);
        *ppHead = next;
    }
}

这里写图片描述

//查找
SListNode* SListFind(SListNode* pHead, DataType x)
{
    SListNode* cur = pHead;
    while (cur)
    {
        if (cur->_data == x)
        {
            return cur;
        }
        cur = cur->_next;
    }
    return NULL;
}

void Test2()
{
    SListNode* list = NULL;

    SListPushFront(&list, 4);
    SListPushFront(&list, 3);
    SListPushFront(&list, 2);
    SListPushFront(&list, 1);

    SListPrint(list);

    //SListPopFront(&list);
    //SListPopFront(&list);
    //SListPopFront(&list);
    //SListPopFront(&list);

    //SListPrint(list);

    SListNode* node = SListFind(list, 4);
    printf("%d\n", node->_data);
}
//任意位置插入,在pos的前面插入
void SListInsert(SListNode** ppHead, SListNode* pos, DataType x)
{
    assert(*ppHead&&pos);
    //一个结点
    if (pos == *ppHead)
    {
        SListPushFront(ppHead, x);
    }
    else
    {
        //任意位置
        SListNode* newnode = BuySListNode(x);
        SListNode* prev = *ppHead;
        while (prev->_next != pos)
        {
            prev = prev->_next;
        }
        prev->_next = newnode;
        newnode->_next = pos;
    }
}

这里写图片描述


//任意位置删除
void SListErase(SListNode** ppHead, SListNode* pos)
{
    assert(ppHead && pos);
    if (*ppHead == pos)
    {
        SListPopFront(ppHead);
    }
    else
    {
        SListNode* prev = *ppHead;
        while (prev->_next != pos)
        {
            prev = prev->_next;
        }
        prev->_next = pos->_next;
        free(pos);//如果free(pos),会导致找不到pos的下一节点
    }
}

这里写图片描述

void Test3()
{
    SListNode* list = NULL;
    SListNode* pos = NULL;

    SListPushFront(&list, 4);
    SListPushFront(&list, 3);
    SListPushFront(&list, 2);
    SListPushFront(&list, 1);

    SListPrint(list);

    pos = SListFind(list, 4);
    SListInsert(&list, pos, 6);
    SListPrint(list);

    pos = SListFind(list, 4);
    SListErase(&list, pos);

    SListPrint(list);

}

Test.c

#define _CRT_SECURE_NO_WARNINGS 1

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

#include "SList.h"

int main()
{
    //Test1(); //尾插/删
    //Test2();//头插/删,查找
    //Test3();//任意位置插入,删除
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值