目录
一、概念
循环链表是一个首尾相连的链表,就是最后一个结点指向第一个结点(形成了一个环)。将单链表的最后一个结点的指针域由NULL改为指向表头结点。
带头结点的循环链表:尾结点的后继为头结点(不是NULL)
结点定义代码如下:
typedef struct CNode//循环链表节点
{
int data;//数据
struct CNode* next;//后继指针
}CNode, * CList;
二、循环链表基本操作
①初始化
void InitList(CList plist)
{
assert(plist != NULL);
if (plist == NULL)
return;
//数据域不使用
plist->next = plist;//形成环
}
②头插(插在最前面)
- 动态创建一个新结点
- 把数据存放到新结点
- 新结点插入到头结点后面
和单链表一样,并未涉及尾结点的操作,所以头插插法一样。
//往plist中头部插入数字val
bool Insert_head(CList plist, ElemType val)
{
//1.动态申请一个新的节点
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
if (p == NULL)
return false;
//2.把数据存放在新节点中
p->data = val;
//3.把新节点插入在头结点后面
p->next = plist->next;
plist->next = p;
return true;
}
③尾插
- 动态创建新结点
- 把值存放到新结点
- 找到链表尾巴 //尾巴的next==NULL
- 把新结点插在尾结点的后面
//往plist中的尾部插入数字val
bool Insert_tail(CList plist, ElemType val)
{
//1.动态创建新结点
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
if (p == NULL)
return false;
//2.把数据存放到新结点中
p->data = val;
//3.找尾巴
CNode* q;
//思考第一个点的前驱:plist,因此q的初始化为q=plist
for (q = plist; q->next != plist; q = q->next)
;
//4.把新结点插入到当前尾巴处
p->next = q->next;
q->next = p;
return true;
}
④查找数据
遍历链表,如若data==val,即为找到
//在pCList查找第一个key值,找到返回结点地址,没有找到返回NULL;
CNode* Search(CList plist, int key)
{
assert(plist != NULL);
if (plist == NULL)
{
return NULL;
}
for (CNode* p = plist->next; p != plist; p = p->next)
{
if (p->data == key)
{
return p;
}
}
return NULL;
}
⑤获取val的前驱
//返回key的前驱地址,如果不存在返回NULL;
CNode* GetPrio(CList plist, int key)
{
assert(plist != NULL);
if (plist == NULL)
{
return NULL;
}
CNode* p;
for (p = plist; p->next != plist; p = p->next)
{
if (p->next->data == key)
{
return p;
}
}
return NULL;
}
⑥获得后继
//返回key的后继地址,如果不存在返回NULL;
CNode* GetNext(CList plist, int key)
{
CNode* p = Search(plist, key);
if (p == NULL)
{
return NULL;
}
return p->next;
}
⑦删除数据
删除val数据也就是删除val数据所在的结点。
通过前驱结点进行查找val所在的前驱结点。删除方法为该节点的前驱节点直接指向该节点的后继节点
步骤:
- 找到val的前驱 //调用⑤GetPrio函数
- 删除
//删除第一个val的值(考试重点)
bool DelVal(CList plist, int val)
{
CNode* p = GetPrio(plist, val);
if (p == NULL)
{
return false;
}
CNode* q = p->next;
p->next = q->next;
free(q);
return true;
}
⑧删除某位置的值
//删除pos位置的值
bool DelPos(CList plist, int pos)
{
//对位置合法性进行判断
if (pos < 0 || pos >= GetLength(plist))
{
return false;
}
CNode* p;
int i;
for (p = plist, i = 0; i < pos; i++, p = p->next)
{
;
}
CNode* q = p->next;//q是要删除的点
p->next = q->next;
free(q);
return true;
}
⑨输出数据
遍历链表,输出data值即可
//输出plist的所有数据
void Show(CList plist)
{
//从头到尾遍历数据节点
//plist->next是存放数据的第一个结点
for (CNode* p = plist->next; p != plist; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
⑨判断plist是否为空链表(没有数据节点)
循环链表判空:p->next==plist
bool IsEmpty(CList plist)
{
assert(plist!=NULL);
if (plist == NULL)
{
return NULL;
}
return plist->next == plist;
}
⑩获取plist长度,数据节点的个数
int GetLength(CList plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return -1;
}
int count = 0;
for (CNode* p = plist->next; p != plist; p = p->next)
{
count++;
}
return count;
}
⑩①插入数据
在pCList链表的pos位置插入val
//插入数据,在pCList链表的pos位置插入val;
bool Insert(CList plist, int pos, int val)
{
if (pos<0 || pos>GetLength(plist))
{
return false;
}
//申请结点
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
p->data = val;
//找位置
CNode* q;
int i = 0;
for (q = plist; i < pos; i++, q = q->next)
{
;
}
//插入
p->next = q->next;
q->next = p;
return true;
}
⑩②获取plist链表的pos(下标)位置的值
利用循环i++查找下标所在位置
//获取plist链表的pos(下标)位置的值
//关于函数的设计:
/*int GetElem(List plist, int pos)
* pos如果<0或者>链表长度就无法返回值
* 用断言assert(pos >= 0 && pos <= GetLength(plist))限定不好,因为断言在release模式下无效
* bool GetElem(List plist, int pos)
* 只能返回true或者false无法返回data值并且1、0有歧义
*/
bool GetElem(CList plist, int pos, int* rtval)//rtval:输出参数
{
if (pos < 0 || pos >= GetLength(plist))
return false;
CNode* p = plist->next;
for (int i = 0; i < pos; i++, p = p->next)
;
*rtval = p->data;
return true;
}
⑩③清空\销毁
总是删除第一个数据结点(首元结点)
p指向当前结点,剔除掉 //plist->next=p->next,free(p)
void Destory(List plist)
{
while (plist->next != NULL)
{
p = plist->next;
plist->next = p->next;
free(p);
}
void Destroy(CList plist)
{
if (plist == NULL || plist->next == NULL)
return;
CNode* p = plist->next;
while (plist->next!= plist)// (p!=plist)
{
p = plist->next;
plist->next = p->next;
free(p);
}
}
三、完整代码
#include "clist.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
//初始化
void InitCList(CList plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return;
}
plist->next = plist;
}
//头插(考试重点)
bool Insert_head(CList plist, int val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
//申请结点
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
//放入数据
p->data = val;
//头插
p->next = plist->next;
plist->next = p;
return true;
}
//尾插(考试重点)
bool Insert_tail(CList plist, int val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
//创建新结点
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
p->data = val;
//找尾巴
CNode* q;
for (q = plist; q->next != plist; q = q->next)
{
;
}
//尾插
p->next = q->next;//p->next=plist;//Ok
q->next = p;
return true;
}
//插入数据,在pCList链表的pos位置插入val;
bool Insert(CList plist, int pos, int val)
{
if (pos<0 || pos>GetLength(plist))
{
return false;
}
//申请结点
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
p->data = val;
//找位置
CNode* q;
int i = 0;
for (q = plist; i < pos; i++, q = q->next)
{
;
}
//插入
p->next = q->next;
q->next = p;
return true;
}
//判空
bool IsEmpty(CList plist)
{
assert(plist!=NULL);
if (plist == NULL)
{
return NULL;
}
return plist->next == plist;
}
//获取数据结点的个数
int GetLength(CList plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return -1;
}
int count = 0;
for (CNode* p = plist->next; p != plist; p = p->next)
{
count++;
}
return count;
}
//在pCList查找第一个key值,找到返回结点地址,没有找到返回NULL;
CNode* Search(CList plist, int key)
{
assert(plist != NULL);
if (plist == NULL)
{
return NULL;
}
for (CNode* p = plist->next; p != plist; p = p->next)
{
if (p->data == key)
{
return p;
}
}
return NULL;
}
//删除pos位置的值
bool DelPos(CList plist, int pos)
{
if (pos < 0 || pos >= GetLength(plist))
{
return false;
}
CNode* p;
int i;
for (p = plist, i = 0; i < pos; i++, p = p->next)
{
;
}
CNode* q = p->next;//q是要删除的点
p->next = q->next;
free(q);
return true;
}
//删除第一个val的值(考试重点)
bool DelVal(CList plist, int val)
{
CNode* p = GetPrio(plist, val);
if (p == NULL)
{
return false;
}
CNode* q = p->next;
p->next = q->next;
free(q);
return true;
}
//返回key的前驱地址,如果不存在返回NULL;
CNode* GetPrio(CList plist, int key)
{
assert(plist != NULL);
if (plist == NULL)
{
return NULL;
}
CNode* p;
for (p = plist; p->next != plist; p = p->next)
{
if (p->next->data == key)
{
return p;
}
}
return NULL;
}
//返回key的后继地址,如果不存在返回NULL;
CNode* GetNext(CList plist, int key)
{
CNode* p = Search(plist, key);
if (p == NULL)
{
return NULL;
}
return p->next;
}
//输出
void Show(CList plist)
{
for (CNode* p = plist->next; p != plist; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
//清空数据
void Clear(CList plist)
{
Destroy(plist);
}
//销毁整个内存
void Destroy(CList plist)
{
//总是删除第一个数据结点
CNode* p;
while (plist->next != plist)
{
p = plist->next;
plist->next = p->next;
free(p);
}
}