数据结构(c语言实现)——单链表

  1. 单链表数据结构定义
    在这里插入图片描述
typedef struct LNode {
	People data; //结点的数据域
	struct LNode* next; //结点的指针域
} LNode, *LinkList; //LinkList为指向结构体LNode的指针类型,这里Linklist 与LNote*等价
  1. 单链表
    在这里插入图片描述
  2. 头插操作
    在这里插入图片描述
    这里修改指针的时候一定要先将新节点指针指向原链表第一个元素,然后修改头节点。
    如果是先将头节点指向新节点,原来链表中元素会丢失
    后面插入操作也是先将新节点指针指向插入点后元素,然后修改插入点前的指针

尾插操作

//头插法插入元素

void list_tailInset(LinkList& list, People people)
{
	
	LNode *peo = (LNode*)malloc(sizeof(LNode));//创建新节点;
	peo->data = people;//修改节点数据
	peo->next = NULL;
	LNode* s = getElem(listLength(list));//链表尾结点
	s->next = peo; 
}
  1. 插入操作
    在这里插入图片描述
  2. 删除操作
    在这里插入图片描述
    执行删除操作时一定要用一个一个节点存放要删除节点,善后用free()函数释放节点内存,因为这里的所有节点内从空间都是动态分配的,系统不会自动回收这些内存空间,如果不适用free()函数释放,这些节点的内存空降将无法使用
//有头结点单链表
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#define max 51

 struct People
{
	char name[max];
	char ID[max];
};
typedef struct LNode {
	People data; //结点的数据域
	struct LNode* next; //结点的指针域
} LNode, *LinkList; //LinkList为指向结构体LNode的指针类型,这里Linklist 与LNote*等价

int listLength(LinkList list);//链表的长度
bool JudgeEq(People peo1 ,People peo2);//判断两个people是否相等
void listInit(LinkList& list);//初始化链表
void list_headInset(LinkList &list , People people);//头插法向链表中插入元素
LNode* GetElem(LinkList& list, int i);//按位查找元素
LNode* LocalElem(LinkList& list, People peo);//按值查找元素
bool InsertLNde(LinkList& list, People peo , int i);//按位插入节点
bool peo_InsertLNde(LinkList& list, People peo1, People peo2);//按值后插 peo1为插入元素,peo2为目标元素

bool del(LinkList& list , int i);//删除节点

void main()
{
	LinkList list;
	listInit(list);

	People peo[4] = { {"李参政" , "0418"} , { "参政" ,"0418"} , {"政" ,"0418"} ,{"李" ,"0418"} };
	People peo1 = { "李参政"  , "0419"};

	for (int i = 3; i >= 0; i--)
		list_headInset(list, peo[i]);
	printf("%d\n" , listLength(list));
	printf("----------------------------\n");
	InsertLNde(list, peo1, 1);
	printf("%d\n", listLength(list));
	printf("----------------------------\n");
	del(list, 2);
	printf("%d\n", listLength(list));
	printf("----------------------------\n");
	int j = listLength(list);
	for(int i = 0 ; i < j ; i++)
		del(list, 1);
	printf("%d\n" ,listLength(list));
	free(list);




}

//初始化链表,创建头指针并且初始化
void listInit(LinkList& list)
{
	list = (LinkList)malloc(sizeof(LNode));//创建头节点
	list->next = NULL;			//初始化头节点
}

//头插法插入元素
void list_headInset(LinkList& list, People people)
{
	LNode *peo = (LNode*)malloc(sizeof(LNode));//创建新节点;
	peo->data = people;//修改节点数据
	peo->next = list->next;//将新节点指针指向原链表第一个元素节点
	list->next = peo;//将链表头节点指针指向新节点
}


//按位查找元素
LNode* GetElem(LinkList& list, int i)
{
	int j = 1;
	LNode* p = list->next;//头节点值赋给p
	if (i == 0) //i的值为 0,输出头节点
		return list;
	//从第一个元素结点,直至p空或者找到目标节点,结束循环
	while (p != NULL && j < i)
	{
		p->next = p;
		j++;
	}
	return p;
}

//按值查找
LNode* LocalElem(LinkList& list, People peo)
{
	LNode* p = list->next;
	while (p->next!=NULL  && !JudgeEq(p->data, peo))
		p = p->next;
	return p;
}


//按位插入
bool InsertLNde(LinkList& list, People peo, int i)
{
	int j = 0;
	LNode* p = list->next;
	if (i > listLength(list))//判断i的有效性
		return false;
	p = GetElem(list, i - 1);
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->next = p->next;
	p->next = s;


}
//按值后插入
bool peo_InsertLNde(LinkList& list, People peo1, People peo2)//按值后插 peo1为插入元素,peo2为目标元素
{
	int j = 0;
	LNode* p = list->next;
	while (p != NULL && !JudgeEq(p->data, peo2))
	{
		j++;
	}
	//表中没有目标元素
	if (j == listLength(list))
		return false;
	p = LocalElem(list, peo2);
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->next = p->next;
	p->next = s;
	
}


/*这里本来是这样的
 LNode* s = GetElem(list, i - 1);
 LNode* p = GetElem(list, i);
 s->next = p->next;
 free(p);
 但是数据运行的时候提示数据访问异常
*/
bool del(LinkList& list, int i)//删除节点
{
	if (i > listLength(list))
		return 0;
	
	LNode* s = GetElem(list, i - 1);
	LNode* p;
	p = s->next;
	s->next = p->next;
	free(p);
	return true;

}
//链表长度
int listLength(LinkList list)
{
	if (list->next == NULL)
		return 0;
	int i = 0;
	while (list->next != NULL) 
	{
		i++;
		list = list->next;
	}
	return i;
}

//判断两个people 是否相等
bool JudgeEq(People peo1, People peo2)
{
	if (strcmp(peo1.name, peo2.name) == 0 && strcmp(peo1.ID, peo2.ID) == 0)
		return true;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅隐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值