ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

单循环链表(带头节点实现)

2021-10-03 21:59:17  阅读:184  来源: 互联网

标签:val int next 链表 PNode 单循环 plist NULL 节点


单循环链表

单循环链表:最后一个有效节点的next域指向头节点,当链表内有效节点数为0时,头节点的next域指向头节点本身。
结构体设计:

typedef int ELEMTYPE;
typedef struct CNode
{
	ELEMTYPE data;
	struct CNode* next;//保存下一个结点的地址
}CNode,*PNode;

单循环
1、当链表内有效节点数量为0时
在这里插入图片描述
2、当链表有多个有效节点
在这里插入图片描述
3、头文件

typedef int ELEMTYPE;
typedef struct CNode
{
	ELEMTYPE data;
	struct CNode* next;//保存下一个结点的地址
}CNode,*PNode;
//初始化
void Init_clist(PNode plist);
//头插
bool Insert_head(PNode plist,int val);
//尾插
bool Insert_tail(PNode plist,int val);
//按位置插
bool Insert_pos(PNode plist, int pos,int val);
//头删
bool Del_head(PNode plist);
//未删
bool Del_tail(PNode plist);
//按位置删
bool Del_pos(PNode plist,int pos);
//按值删
bool Del_(PNode plist,int val);
//查找
PNode Search(PNode plist,int val);
//找值为val值的前驱
PNode Get_pre(PNode plist,int val);
//找值为val的后继
PNode Get_next(PNode plist,int val);
//读取有效节点个数
int Get_length(PNode plist);
//判空
bool IsEmpty(PNode plist);
//清空
void Clear(PNode plist);
//销毁
void Destory(PNode plist);
//打印
void Print(PNode plist);

4、函数实现

#include"LinkList.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//带有头节点的链表,第一个节点不用带数据
//初始化
void Init_list(PNode plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return;
	}
	plist->next = NULL;
	return;
}
//头插
bool Insert_head(PNode plist, int val)
{
	assert(plist != NULL);
	//先购买一个节点
	PNode s = (PNode)malloc(sizeof(Node) * 1);
	s->data = val;
	s->next = plist->next;
	plist->next = s;
	return true;
}
//尾插
bool Insert_back(PNode plist, int val)
{
	assert(plist != NULL);
	PNode s = (PNode)malloc(sizeof(Node));
	assert(s != NULL);
	PNode p = plist;
	while (p->next != NULL)
	{
		p = p->next;
	}
	s->next = p->next;
	p->next = s;
	s->data = val;
	return true;
}
//按位置插
bool Insert_pos(PNode plist, int pos, int val)
{
	assert(plist != NULL);
	if (pos<0 || pos>Get_length(plist))
	{
		return false;
	}
	PNode s = (PNode)malloc(sizeof(Node) * 1);
	assert(s != NULL);
		PNode p = plist;
	int i = 0;
	while (i < pos)
	{
		p = p->next;
		i++;
	}
	s->next = p->next;
	p->next = s;
	s->data = val;
	return true;
}
//头删
bool Del_head(PNode plist)
{
	assert(plist != NULL);
	if (IsEmpty(plist))
	{
		return false;
	}
	PNode p = plist->next;
	plist->next = p->next;
	free(p);
	p = NULL;
	return true;
}
//尾删
bool Del_back(PNode plist)
{
	assert(plist != NULL);
	if (IsEmpty(plist))
	{
		return false;
	}
	PNode p = plist;
	while (nullptr != p->next)
	{
		if (NULL == p->next->next)
		{
			break;
		}
		p = p->next;
	}
	free(p->next);
	p->next = NULL;
	return true;
}
//按位置删
bool Del_pos(PNode plist, int pos)
{
	assert(plist != NULL);
	if (IsEmpty(plist))
	{
		return false;
	}
	if (pos<0 || pos>Get_length(plist) - 1)
	{
		return false;
	}
	PNode p = plist;
	int i = 0;
	while (i < pos)
	{
		p = p->next;
		i++;
	}
	PNode q = p->next;
	p->next = p->next->next;
	free(q);
	q = NULL;
	return true;
}
//按值删
bool Del_val(PNode plist, int val)
{
	assert(plist != NULL);
	if (NULL==Search(plist, val))
	{
		return false;
	}
	PNode p = plist;
	while (p->next != NULL)
	{
		if (p->next->data == val)
		{
			PNode q = p->next;
			p->next = q->next;
			free(q);
			return true;
		}
		p = p->next;
	}
}
//判空
bool IsEmpty(PNode plist)
{
	assert(plist != NULL);
	if (plist->next == NULL)
	{
		return true;
	}
	return false;
}
//搜索
PNode Search(PNode plist, int val)
{
	assert(plist != NULL);
	if (IsEmpty(plist))
	{
		return NULL;
	}
	PNode p = plist->next;
	while (NULL != p)
	{
		if (p->data == val)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}
//返回节点前趋
PNode Search_pre(PNode plist, int val)
{
	assert(plist != NULL);
	if (NULL==Search(plist, val))
	{
		return NULL;
	}
	PNode p = plist;
	while (p->next != NULL)
	{
		if (p->next->data == val)
		{
			return p;
		}
		p = p->next;
	}
}
//返回节点后继
PNode Search_back(PNode plist, int val)
{
	assert(plist != NULL);
	PNode p = Search(plist, val);
	return p->next;
}
//返回节点个数(除去头节点)
int Get_length(PNode plist)
{
	assert(plist != NULL);
	PNode p = plist->next;
	int count = 0;
	while (p != NULL)
	{
		count++;
		p = p->next;
	}
	return count;
}
//清空链表
void Clear(PNode plist)
{
	assert(plist != NULL);
	while (!IsEmpty(plist))
	{
		Del_head(plist);
	}
}
//摧毁链表
void Destory(PNode plist)
{
	assert(plist != NULL);
	Clear(plist);
	free(plist);
	plist = NULL;
}
//打印链表数据
void Print(PNode plist)
{
	assert(plist != NULL);
	PNode p = plist->next;
	while (p!= NULL)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}
int main()
{
	Node p = {};
	Init_list(&p);
	for (int i = 0; i < 3; i++)
	{
		Insert_back(&p, i + 1);
		Print(&p);
	}
	for (int i = 2; i>=0; i--)
	{
		Del_pos(&p,i);
		Print(&p);
	}
}

标签:val,int,next,链表,PNode,单循环,plist,NULL,节点
来源: https://blog.csdn.net/m0_57519884/article/details/120540378

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有