数据结构单向链表的实现

目录

前言

实现过程

SList.h:

SList.c:

test.c:


前言

链表是一种物理存储结构上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针连接次序实现的。单向链表的特点:结构简单,一般不会单独用来存储数据,实际上更多的是作为其他数据结构的子节构。

实现过程

单向链表的实现需要通过接口来实现,需要创建三个源文件,分别是

SList.h  :用于存储头文件,函数的声明,结构体。

SList.c  :用于各个功能函数的实现。

test.c  :主函数,用来调用函数对数据进行测试计算,里面有几组测试用例对函数的功能进行测试。

SList.h:

#pragma once

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

typedef int SLTDataType;

typedef struct SListNode
{
    int data;
    struct SListNode* next;
}SLTNode;

void SListPrint(SLTNode* phead); //打印

void SListPushBack(SLTNode** pphead, SLTDataType x);//尾插

void SListPushFront(SLTNode** pphead, SLTDataType x);//头插

void SListPopFront(SLTNode** pphead);//头删

void SListPopBack(SLTNode** pphead);//尾删

SLTNode* SListFind(SLTNode* phead, SLTDataType x);//查找

void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);//插入

void SListErase(SLTNode** pphead, SLTNode* pos);//删除

void SListInsertAfter(SLTNode* pos,SLTDataType x);//pos之后插入值

void SListInsertAfter(SLTNode* pos, SLTDataType x);//pos之后插入值

void SListEarseAfter(SLTNode* pos);//删除pos之后的值

SList.c:

#define _CRT_SECURE_NO_WARNINGS
#include"SList.h"


void SListPrint(SLTNode* phead)//打印
{
    SLTNode* cur = phead;
    while (cur != NULL)
    {            
        printf("%d->", cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}

SLTNode* BuySListNode(SLTDataType x)
{
    SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
    assert(newnode);
    newnode->data = x;
    newnode->next = NULL;
    return newnode;
}


void SListPushBack(SLTNode** pphead, SLTDataType x)//尾插
{
    assert(pphead);
    SLTNode* newnode = BuySListNode(x);
    if (*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {
        SLTNode* tail = *pphead;
        while (tail->next != NULL)
        {
            tail = tail->next;
        }
        tail->next = newnode;
    }
}
void SListPushFront(SLTNode** pphead, SLTDataType x)//头插
{
    assert(pphead);
    SLTNode* newnode = BuySListNode(x);
    newnode->next = *pphead;
    *pphead = newnode;

}

void SListPopFront(SLTNode** pphead)//头删
{
    assert(pphead);
    assert(*pphead);
    SLTNode* next = (*pphead)->next;
    free(*pphead);
    *pphead = next;
}

void SListPopBack(SLTNode** pphead)//尾删
{
    assert(*pphead);
    assert(pphead);

    if ((*pphead)->next == NULL    )
    {
        free(*pphead);
        *pphead = NULL;
    }
    else
    {
        /*SLTNode* tailPrev = NULL;
        SLTNode* tail = *pphead;
        while (tail->next != NULL)
        {
            tailPrev = tail;
            tail = tail->next;
        }
        free(tail);
        tailPrev->next = NULL;*/
        SLTNode* tail = *pphead;
        while (tail->next->next != NULL)
        {
            tail = tail->next;
        }
        free(tail->next);
        tail->next=NULL;
    }
    
    
}

SLTNode* SListFind(SLTNode* phead, SLTDataType x)//查找
{
    SLTNode* cur = phead;
    while (cur)
    {
        if (cur->data == x)
        {
            return cur;
        }
        else
        {
            cur = cur->next;
        }
    }
    return NULL;
}
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)//插入
{
    assert(pos);
    assert(pphead);
    if (pos == *pphead)
    {
        SListPushFront(pphead, x);
    }
    else
    {
        SLTNode* prev = *pphead;
        while (prev->next != pos)
        {
            prev = prev->next;
        }
        SLTNode* newnode = BuySListNode(x);
        prev->next = newnode;
        newnode->next = pos;

    }
}


void SListErase(SLTNode** pphead, SLTNode* pos)//删除
{
    assert(pphead);
    assert(pos);

    if (*pphead == pos)
    {
        SListPopFront(pphead);
    }
    else
    {
        SLTNode* prev = *pphead;
        while (prev->next != pos)
        {
            prev = prev->next;
        }
        prev->next = pos->next;
        free(pos);
    }
}

void SListInsertAfter(SLTNode* pos, SLTDataType x)//pos之后插入值
{
    assert(pos);
    SLTNode* newnode = BuySListNode(x);
    newnode->next = pos->next;
    pos->next = newnode;

}

void SListEarseAfter(SLTNode* pos)//删除pos之后的值
{
    assert(pos);
    if (pos->next == NULL)
    {
        return;
    }
    SLTNode* del = pos->next;
    pos->next = del->next;
    free(del);
}

test.c:

#define _CRT_SECURE_NO_WARNINGS

#include"SList.h"
void TestSList1()
{
    SLTNode* n1 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n1);
    SLTNode* n2 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n2);
    SLTNode* n3 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n3);
    SLTNode* n4 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n4);

    n1->data = 1;
    n2->data = 2;
    n3->data = 3;
    n4->data = 4;

    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = NULL;
    SListPushBack(&n1, 5);
    SListPrint(n1);
}

void TestSList2()
{
    SLTNode* plist = NULL;
    SListPushBack(&plist, 1);
    SListPushBack(&plist, 2);
    SListPushBack(&plist, 3);
    SListPushBack(&plist, 4);
    SListPushBack(&plist, 5);
    SListPushBack(&plist, 5);
    SListPushFront(&plist, 0);
    SListPrint(plist);
    SListPopFront(&plist);
    SListPopFront(&plist);
    SListPopBack(&plist);
    SListPrint(plist);

}

void TestSList3()
{
    SLTNode* plist = NULL;
    SListPushBack(&plist, 5);
    SListPushBack(&plist, 4);
    SListPushBack(&plist, 3);
    SListPushBack(&plist, 2);
    SListPushBack(&plist, 1);
    SListPrint(plist);

    SLTNode* pos = SListFind(plist, 3);
    if (pos)
    {
        printf("找到啦!\n");
        pos->data = 50;
    }
    else
    {
        printf("没找到。。。\n");
    }
    pos = SListFind(plist, 4);
    if(pos)
    {
        SListInsert(&plist, pos, 20);//插入
    }

    SListPrint(plist);
    pos = SListFind(plist, 50);
    if (pos)
    {
        SListErase(&plist,pos);//删除
    }
    SListPrint(plist);

    pos = SListFind(plist, 20);
    if (pos)
    {
        SListInsertAfter(pos, 165);//后面插入
    }
    SListPrint(plist);

    pos = SListFind(plist, 165);
    if (pos)
    {
        SListEarseAfter(pos);//后面删除
    }
    SListPrint(plist);

}
int main()
{
    TestSList3();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值