单循环链表(带头节点实现)
作者:互联网
单循环链表
单循环链表:最后一个有效节点的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