基于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("%dt",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.仿真

仿真结果

 二、单链表的简单应用---学生管理系统

1.代码

代码如下:

#include <stdio.h>
#include <stdlib.h>
struct student
{
	char name[20];//姓名
	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("nametnumtmathn");
    struct Node *Pmove=headNode->next;
    while(Pmove)
    {
        printf("%st%dt%dn",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.仿真

仿真结果

 参考链接:

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


总结

        以上就是今天要讲的内容,本文介绍了单链表的使用方法,希望对大家有所帮助。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>