当前位置: 首页 > news >正文

单链表专题

test.c

#define _CRT_SECURE_NO_WARNINGS
//#include "SList.h"
//void SListTest01()
//{
//    //创建几个节点
//    SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
//    node1->data = 1;
//
//    SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
//    node2->data = 2;
//
//    SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
//    node3->data = 3;
//
//    SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));
//    node4->data = 4;
//    //将四个节点连接起来
//    node1->next = node2;
//    node2->next = node3;
//    node3->next = node4;
//    node4->next = NULL;
//    //调用链表的打印
//    SLTNode* plist = node1;
//    
//}
//
//void SListTest02()
//{
//    SLTNode* plist = NULL;
//    SLTPushBack(&plist, 1);
//    SLTPushBack(&plist, 2);
//    SLTPushBack(&plist, 3);
//    SLTPushBack(&plist, 4);
//    SLTPrint(plist);
//
//    /*SLTPushFront(&plist, 5);
//    SLTPrint(plist);
//
//    SLTPushFront(&plist, 6);
//    SLTPrint(plist);
//
//    SLTPushFront(&plist, 7);
//    SLTPrint(plist);
//    
//    SLTPopBack(&plist);
//    SLTPrint(plist);
//
//    SLTPopFront(&plist);
//    SLTPrint(plist);*/
//
//
//    SLTNode* p = SLTFind(plist, 1);
//    /*SLTInsert(&plist, p, 11);
//    SLTInsertAfter(&plist, p, 15);*/
//    //SLTErase(&plist,p);
//    SLTEraseAfter(p);
//    SListDestory(&plist);
//    SLTPrint(plist);
//
//    /*if (p != NULL)
//    {
//        printf("找到了");
//    }
//    else
//    {
//        printf("未找到");
//    }*/
//
//
//}
//
//
//int main()
//{
//    //SListTest01();
//    SListTest02();
//    return 0;
//}

SList.c

//typedef int Int;
//void Delete(int* arr, Int i, int sz)
//{
//    for (int m = i; m < sz - 1; m++)
//    {
//        arr[i] = arr[i + 1];
//    }
//}
//int remove(Int* arr, int sz, Int a
//{
//    int newsize = sz;
//    for (int i = 0; i < sz; i++)
//    {
//        if (arr[i] == a)
//        {
//            Delete(arr, i, sz);
//            newsize--;
//        }
//    }
//    return newsize;
//}
//int main()
//{
//    Int arr[] = { 1,23,4,5,3,2,1,4,2 };
//    Int a = 23;
//    int sz = sizeof(arr) / sizeof(arr[0]);
//    int newsize = remove(&arr, sz, a)
//    printf("%d", newsize);
//    return 0;
//}

//采用双指针法
//int newsize(int* arr, int sz, int a)
//{
//    assert(arr);
//    int src, dst;
//    src = dst = 0;
//    while (src < sz)
//    {
//        if (arr[src] == a)
//        {
//            src++;
//        }
//        else
//        {
//            arr[dst] = arr[src];
//            src++;
//            dst++;
//        }
//    }
//    return dst;
//}
// 1 4 5
// 1 3 4
// 1 1
//int main()
//{
//    int arr[] = { 1,2,34,2,4,6,3,6,7 };
//    int sz = sizeof(arr) / sizeof(arr[0]);
//    int a = 2;
//    int newsize = remove(arr,sz,a);
//    printf("%d", newsize);
//    return 0;
//}


//void move(int* nums1, int* nums2, int m, int n)
//{
//    int l1 = m - 1;
//    int l2 = n - 1;
//    int l3 = m + n - 1;
//    while (l1 >= 0 && l2 >= 0)
//    {
//        if (nums1[l1] < nums2[l2])
//        {
//            nums1[l3--] = nums2[l2--];
//        }
//        else
//        {
//            nums1[l3--] = nums2[l1--];
//
//        }
//    }
//    while (l2 >= 0)
//    {
//        nums1[l3--] = nums2[l2--];
//    }
//}
//int main()
//{
//    int nums1[] = {1,2,3};
//    int nums2[] = { 2,5,6 };
//    int m = sizeof(nums1) / sizeof(nums1[0]);
//    int n = sizeof(nums2) / sizeof(nums2[0]);
//
//    move(nums1,nums2,m,n);
//    for (int i = 0;i < m + n; i++)
//    {
//        printf("%d", nums1[i]);
//    }
//    return 0;
//}


//针对顺序表:中间/头部插入效率低下,增容降低运行效率、增容造成空间浪费

//链表
//链表也是线性表的一种,物理结构上非连续、非顺序,数据元素的逻辑顺序是通过链表中的指针链接次序实现的
// 链表是由一个一个的节点组成(结点)
// 链表有两部分组成——数据域和指针域
// 
#include "SList.h"
void SLTPrint(SLTNode* phead)
{
    SLTNode* pcur = phead;
    while (pcur)
    {
        printf("%d->", pcur->data);
        pcur = pcur->next;
    }
    printf("NULL\n");
}

SLTNode* SLTBuyNode(SLTDatatype x)
{
    SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
    if (newnode == NULL)
    {
        perror("SLTBuyNode()::malloc");
        exit(1);
    }
    newnode->data = x;
    newnode->next = NULL;
    return newnode;
}

void SLTPushBack(SLTNode** pphead, SLTDatatype x)
{
    assert(pphead);
    SLTNode* newnode = SLTBuyNode(x);
    if (*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {
        //尾插法:先找到尾结点,再将尾结点和新节点连接起来
        SLTNode* ptail = *pphead;
        while (ptail->next)
        {
            ptail = ptail->next;
        }
        ptail->next = newnode;
    }
    
}

void SLTPushFront(SLTNode** pphead, SLTDatatype x)
{
    assert(pphead);
    SLTNode* newnode = SLTBuyNode(x);
    if (*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {
        newnode->next = *pphead;
        *pphead = newnode;
    }
}

void SLTPopBack(SLTNode** pphead)
{
    assert(pphead);
    assert(*pphead);
    if ((*pphead)->next == NULL)
    {
        free(*pphead);
        *pphead = NULL;
    }
    else
    {
        SLTNode* prev = *pphead;
        SLTNode* ptail = *pphead;
        while (ptail->next)
        {
            prev = ptail;
            ptail = ptail->next;
        }
        free(ptail);
        ptail = NULL;
        prev->next = NULL;

    }
    
    
}

void SLTPopFront(SLTNode** pphead)
{
    assert(pphead);
    assert(*pphead);
    if ((*pphead)->next == NULL)
    {
        free(*pphead);
        *pphead = NULL;
    }
    else
    {
        SLTNode* pp = *pphead;
        *pphead = (*pphead)->next;
        free(pp);
        pp = NULL;
    }
}

SLTNode* SLTFind(SLTNode* phead, SLTDatatype x)
{
    assert(phead);
    SLTNode* pcur = phead;
    while (pcur )
    {
        if (pcur->data == x)
        {
            return pcur;
        }
        else
        {
            pcur = pcur->next;
        }
    }
    return NULL;
}

void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDatatype x)
{
    assert(phead);
    assert(pos);
    if (pos == *phead)
    {
        SLTPushFront(phead,x);
    }
    else
    {
        SLTNode* pcur = *phead;
        while (pcur->next != pos)
        {
            pcur = pcur->next;
        }
        SLTNode* newnode = SLTBuyNode(x);
        newnode->next = pos;
        pcur->next = newnode;
    }
    
}

void SLTInsertAfter(SLTNode** phead, SLTNode* pos, SLTDatatype x)
{
    assert(phead);
    assert(pos);
    SLTNode* newnode = SLTBuyNode(x);
    newnode->next = pos->next;
    pos->next = newnode;
}

void SLTErase(SLTNode** pphead, SLTNode* pos)
{
    assert(pphead);
    assert(pos);
    SLTNode* prev = *pphead;
    if (pos == *pphead)
    {
        SLTPopFront(pphead);
        
    }
    else
    {
        while (prev->next != pos)
        {
            prev = prev->next;
        }
        prev->next = pos->next;
        free(pos);
        pos = NULL;
    }
}

void SLTEraseAfter(SLTNode* pos)
{
    assert(pos);
    assert(pos->next);
    SLTNode* prev = pos->next ;
    pos->next = prev->next;
    free(prev);
    prev = NULL;
}

void SListDestory(SLTNode** pphead)
{
    assert(pphead && *pphead);
    SLTNode* pcur = *pphead;
    while (pcur)
    {
        SLTNode* next = pcur->next;
        free(pcur);
        pcur = next;
    }
    *pphead = NULL;
}

SList,h

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

typedef int SLTDatatype;
//定义节点的结构
typedef struct SListNode
{
    SLTDatatype data;//数据域
    struct SListNode* next;//指针域 
}SLTNode;

void SLTPrint(SLTNode* phead);

void SLTPushBack(SLTNode** pphead, SLTDatatype x);

void SLTPushFront(SLTNode** pphead, SLTDatatype x);

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

//头插
void SLTPopFront(SLTNode** pphead);

//查找
SLTNode* SLTFind(SLTNode* phead, SLTDatatype x);

//在指定位置之前插入数据
void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDatatype x);

//在指定位置之后插入数据
void SLTInsertAfter(SLTNode** phead, SLTNode* pos, SLTDatatype x);

//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos);
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos);

//销毁链表
void SListDestory(SLTNode** pphead);

相关文章:

  • 我的世界进阶模组开发教程——制作机械动力附属模组
  • 避坑:启动sdk-c demo master需要注意的事情
  • 技术栈CMake的介绍和使用
  • 如何设计三高架构
  • TiDB 上线步骤是怎么样?怎么做到数据不丢失?怎么保证可靠性?
  • 火山引擎发布豆包大模型 1.6 与视频生成模型 Seedance 1.0 pro
  • cmake 编译grpc
  • Decode Valid Comma Only说明
  • linux引导过程与服务控制
  • 电流传感器在汽车中的应用:从BMS电池管理到电机控制的工程解析
  • day031-Shell自动化编程-数组与案例
  • 使用VirtualBox安装ubuntu22.04虚拟机
  • PHP语法基础篇:变量与数据类型
  • Smartbi双产品线功能更新:主动分析更省心,数据治理更高效
  • seo优化新利器:AI如何让内容批量生成与排名提升双管齐下?
  • 边缘计算如何重塑能源管理?从技术原理到应用场景全解析
  • ldkGUI如何添加自定义的字库
  • Android12 开机后桌面加载框的适配
  • Yakit 热加载入门学习指南
  • 纯血Harmony NETX 5小游戏实践:趣味三消游戏(附源文件)
  • 可以做哪些网站有哪些内容/免费b2b信息发布网站
  • 涉密资质 网站建设/2023年6月份又封城了
  • wap建站程序哪个好/百度搜索排名推广
  • 西安网站排名哪家公司好/如何做网络营销?
  • 青岛网站设计哪家好/百度推广业务员电话
  • 线上推广工作是做什么的/seo站长工具平台