1、循环链表示意图
2、相关操作
考察重点:头插、尾插、在指定位置插入数据
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include <string.h>
#include<ctype.h>
#include<assert.h>
#include<stdlib.h>
typedef struct CNode
{
int data;
struct CNode* next;
}CNode, * CList;
//CList=CNode*
//初始化
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(plist != 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;先连接后面
q->next = p;
return true;
}
//获取数据节点的个数
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;
}
//插入数据,在plist链表的pos位置插入val;
bool Insert(CList plist, int pos, int val)
{
assert(plist != NULL);
if (plist == NULL)
return false;
if (pos<0 || pos>Getlength(plist))
return false;
//申请节点
CNode* p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
//放入数据
p->data = val;
//找位置
int i = 0;
CNode* q;
for (q = plist; i < pos; i++, q = q->next)
{
;
}
//插入
p->next = q->next;//先连接后面
q->next = p;
}
//判空
bool IsEmpyt(CList plist)
{
return plist->next == plist;
}
//在plist中查找第一个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)
{
assert(plist != NULL);
if (plist == NULL)
return false;
CNode* p;
int i;
for (p = plist, i = 0; i < pos; i++, p = p->next)
{
;
}
CNode* q = p->next;//先保存
p->next = p->next->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;
}
//删除第一个val的值
bool DelVal(CList plist, int vaSSl)
{
CNode* p = GetPrio(plist, vaSSl);
if (p == NULL)
return false;
CNode* q = p->next;
p->next = q->next;
free(q);
return true;
}
//返回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)
{
assert(plist != NULL);
if (plist == NULL)
return;
for (CNode* p = plist->next; p != plist; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
//销毁整个内存
void Destroy(CList plist)//总删除第一个结点
{
CNode* p;
while (plist->next != plist)
{
p = plist->next;
plist->next = plist->next->next;//p->next
free(p);
}
}
//清空数据
void Clear(CList plist)
{
Destroy(plist);
}
int main()
{
CNode head;
InitCList(&head);
for (int i = 0; i < 10; i++)
{
//Insert_head(&head, i);
Insert_tail(&head, i);
}
Show(&head);
/*Insert(&head, 1, 100);
Show(&head);
CNode*p=Search(&head, 2);
if (p != NULL)
printf("%d\n", p->data);
else
printf("没有找到!\n");
DelVal(&head, 8);
Show(&head);*/
DelPos(&head, 0);
Show(&head);
return 0;
}