勿忘初心做自己 发表于 2024-8-25 05:03:26

基于C语言实现单链表(附有两份完整的详细代码+仿真效果)

用模块化的方式借助C语言实现数据结构中的单链表(附有两份完整的详细代码)
目次
前言
一、何为链表?
二、单链表
1.代码
2.仿真
 二、单链表的简朴应用---学生管理系统
1.代码
2.仿真
总结

前言

        链表是同数据范例的集合,不占用连续内存空间。适合分类存放空间上不连续但须要大量的连续存储空间(雷同档案馆),缺点就是检索速度慢且耗费的时间不固定。本文以单链表来阐明链表的利用,包罗创建链表表头、创建节点、指定位置删除节点、遍历打印节点、头插法插入节点、尾插法插入节点的利用。
提示:以下是本篇文章正文内容,写文章实属不易,盼望能资助到各位,转载请附上链接。
一、何为链表?

         链表说白了就是结构体的集合,是同数据范例的集合,不占用连续内存空间,通过动态内存申请将结构体指针变为结构体变量,用结构体变量去操纵链表。适合分类存放空间上不连续但须要大量的连续存储空间(雷同档案馆),缺点就是检索速度慢且耗费的时间不固定。
二、单链表

1.代码

代码如下:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;
    struct Node *next;
};
//创建链表表头
struct Node *creatlist()//结构指针经动态内存申请变成结构体变量
{
    struct Node *headNode=(struct Node*)malloc(sizeof(struct Node));
    headNode->next=NULL;
    return headNode;
};
//创建节点,要插入节点得先有节点
struct Node *creatNode(int data)
{
    struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=data;
    newNode->next=NULL;
    return newNode;
};
//打印,除去表头,从第二个节点开始遍历打印
void printList(struct Node *headNode)
{
    struct Node *Pmove=headNode->next;
    while(Pmove)
    {
      printf("%d\t",Pmove->data);
      Pmove=Pmove->next;
    }
    printf("\n");
}
//插入节点 头插法
void insert_Node_By_HeadNode(struct Node *headNode,int data)
{
    struct Node *newNode=creatNode(data);//首先创建要插入的节点
    newNode->next=headNode->next;//创建完后插入
    headNode->next=newNode;
}
//尾插法
void insertNodebyRear(struct Node *headNode, int data)
{
        struct Node *newNode=creatNode(data);
        while(headNode->next)
        {
                headNode = headNode->next;
        }
        headNode->next = newNode;
        newNode->next = NULL;
}
//指定位置删除
void deleteNoteByAppoin(struct Node *headNode,int posData)
{
        struct Node *posNode=headNode->next;
        struct Node *posNodeFront=headNode;
        if(posNode==NULL)
        {
                printf("无法删除链表为空\n");
        }
        else
        {
                while(posNode->data!=posData)
                {
                        posNodeFront=posNode;
                        posNode=posNodeFront->next;
                        if(posNode==NULL)
                        {
                                printf("没有找到相关信息无法删除\n");
                                return;
                        }
                }
                posNodeFront->next=posNode->next;
                free(posNode);
        }
}
int main()
{
    struct Node *list=creatlist();
    insert_Node_By_HeadNode(list,1);
    insert_Node_By_HeadNode(list,2);
    insert_Node_By_HeadNode(list,3);
    printList(list);
    deleteNoteByAppoin(list,1);
    printList(list);
    insertNodebyRear(list,4);
    insertNodebyRear(list,5);
    printList(list);
    system("pause");
    return 0;
} 2.仿真


      https://i-blog.csdnimg.cn/blog_migrate/f4ef5f91c23f9e85651ab33f1274dae9.png       仿真效果        二、单链表的简朴应用---学生管理系统

1.代码

代码如下:
#include <stdio.h>
#include <stdlib.h>
struct student
{
        char name;//姓名
        int num;//学号
        int math;//成绩
};
struct Node
{
    struct student date;
    struct Node *next;
};
//创建链表表头
struct Node *creatlist()//结构指针经动态内存申请变成结构体变量
{
    struct Node *headNode=(struct Node*)malloc(sizeof(struct Node));
    headNode->next=NULL;
    return headNode;
};
//创建节点,要插入节点得先有节点
struct Node *creatNode(struct student date)
{
    struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->date=date;
    newNode->next=NULL;
    return newNode;
};
//打印,除去表头,从第二个节点开始遍历打印
void printList(struct Node *headNode)
{
        printf("name\tnum\tmath\n");
    struct Node *Pmove=headNode->next;
    while(Pmove)
    {
      printf("%s\t%d\t%d\n",Pmove->date.name,Pmove->date.num,Pmove->date.math);
      Pmove=Pmove->next;
    }
    printf("\n");
}
//插入节点 头插法
void insert_Node_By_HeadNode(struct Node *headNode,struct student date)
{
    struct Node *newNode=creatNode(date);//首先创建要插入的节点
    newNode->next=headNode->next;//创建完后插入
    headNode->next=newNode;
}
//尾插法
void insertNodebyRear(struct Node *headNode, struct student data)
{
        struct Node *newNode=creatNode(data);
        while(headNode->next)
        {
                headNode = headNode->next;
        }
        headNode->next = newNode;
        newNode->next = NULL;
}
//指定位置删除,通过学号删除
void deleteNoteByAppoinNum(struct Node *headNode,int num)
{
        struct Node *posNode=headNode->next;
        struct Node *posNodeFront=headNode;
        if(posNode==NULL)
        {
                printf("无法删除链表为空\n");
        }
        else
        {
                while(posNode->date.num!=num)
                {
                        posNodeFront=posNode;
                        posNode=posNodeFront->next;
                        if(posNode==NULL)
                        {
                                printf("没有找到相关信息无法删除\n");
                                return;
                        }
                }
                posNodeFront->next=posNode->next;
                free(posNode);
        }
}
int main()
{
    struct Node *list=creatlist();
        struct student info;
        while(1)
        {
                printf("学生的姓名 学号 数学成绩:");
                setbuf(stdin,NULL);//清除缓存
                scanf("%s%d%d",info.name,&info.num,&info.math);
                insertNodebyRear(list,info);
                printf("continue(1/0)?\n");
                setbuf(stdin,NULL);
                int choice=0;
                scanf("%d",&choice);
                if(choice==0)
                {
                        break;
                }
        }
    printList(list);
    printf("要删除学生的学号:");
    int a;
    setbuf(stdin,NULL);
    scanf("%d",&a);
    deleteNoteByAppoinNum(list,a);
    printList(list);
    system("pause");
    return 0;
} 2.仿真

      https://i-blog.csdnimg.cn/blog_migrate/ec99418edcd66768b9734fa0624c74ff.png       仿真效果        参考链接:

1个小时学会单链表,C语言数据结构专题_哔哩哔哩_bilibili
总结

        以上就是本日要讲的内容,本文介绍了单链表的利用方法,盼望对大家有所资助。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 基于C语言实现单链表(附有两份完整的详细代码+仿真效果)