数据结构_单链表

单链表定义:

typedef int DataType;

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

对单链表“增删查改”的函数:

#ifndef __CODE_H__
#define __CODE_H__

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

typedef int DataType;

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

SListNode* BuySListNode(DataType x);  //创建一个结点
void SListPrint(SListNode* pHead);    //打印单链表
void SListDestory(SListNode** ppHead);    //删除单链表

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 SListInsest(SListNode** ppHead, SListNode* pos, DataType x);  //指定位置插入
void SListErase(SListNode** ppHead, SListNode* pos);     //指定位置删除
#endif  //__CODE_H__

函数的代码实现:

#include "code.h"

SListNode* BuySListNode(DataType x)
{
    SListNode* cur = (SListNode*)malloc(sizeof(SListNode));
    cur->_data = x;
    cur->_next = NULL;
    return cur;
}

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



void SListDestory(SListNode** ppHead)
{
    assert(ppHead);

    if (*ppHead == NULL)
    {
        return;
    }
    else
    {
        while (*ppHead)
        {
            SListPopFront(ppHead);
        }
    }
}

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

void SListPopBack(SListNode** ppHead)
{
    assert(ppHead);
    assert(*ppHead);

    SListNode* cur = *ppHead;
    SListNode* tail = NULL;
    if ((*ppHead)->_next == NULL)
    {
        SListPopFront(ppHead);
    }
    else
    {
        while (cur->_next)
        {
            tail = cur;
            cur = cur->_next;
        }
        free(cur);
        tail->_next = NULL;
    }
}

void SListPushFront(SListNode** ppHead, DataType x)
{
    assert(ppHead);

    if (*ppHead == NULL)
    {
        *ppHead = BuySListNode(x);
    }
    else
    {
        SListNode* cur = BuySListNode(x);
        cur->_next = *ppHead;
        *ppHead = cur;
    }
}

void SListPopFront(SListNode** ppHead)
{
    assert(ppHead);

    if (*ppHead != NULL)
    {
        SListNode* cur = *ppHead;
        *ppHead = (*ppHead)->_next;
        free(cur);
    }
}

SListNode* SListFind(SListNode* pHead, DataType x)
{
    assert(pHead);

    while (pHead)
    {
        if (pHead->_data == x)
        {
            return pHead;
        }
        else
        {
            pHead = pHead->_next;
        }
    }
    return NULL;
}

void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)
{
    assert(ppHead);
    assert(pos);

    if (*ppHead == pos)
    {
        SListPushFront(ppHead, x);
    }
    else
    {
        SListNode* cur = *ppHead;
        SListNode* tail = NULL;
        while (cur->_next != pos)
        {
            cur = cur->_next;
        }
        tail = BuySListNode(x);
        cur->_next = tail;
        tail->_next = pos;
    }
}

void SListErase(SListNode** ppHead, SListNode* pos)
{
    assert(ppHead);
    assert(pos);

    if (*ppHead == pos)
    {
        SListPopFront(ppHead);
    }
    else
    {
        SListNode* cur = *ppHead;
        while (cur->_next != pos)
        {
            cur = cur->_next;
        }
        SListPopFront(&pos);
        cur->_next = pos;
    }
}

测试用例:

#include "code.h"

SListNode* Node;


void test1()
{
    SListPrint(Node);

    SListPushFront(&Node, 3);
    SListPushFront(&Node, 2);
    SListPushFront(&Node, 1);
    SListPushFront(&Node, 0);
    SListPrint(Node);
}
void test2()
{
    SListPushBack(&Node, 4);
    SListPushBack(&Node, 5);
    SListPushBack(&Node, 6);
    SListPrint(Node);
}
void test3()
{
    SListPopFront(&Node);
    SListPopBack(&Node);
    SListPrint(Node);
}
void test4()
{
    SListInsest(&Node, Node->_next, 10);
    SListPrint(Node);
}
void test5()
{
    SListErase(&Node, Node->_next);
    SListPrint(Node);
}
void test6()
{
    SListErase(&Node, SListFind(Node, 2));
    SListPrint(Node);
}
void test7()
{
    SListDestory(&Node);
    SListPrint(Node);
}


int main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值