循环链表
关键点:向链表中插入数据时需要移动尾指针,函数内部改变的地址无法传到main函数,所以需要二级指针改变指针的地址。
typedef int ElemType;
struct Node
{
ElemType data;
struct Node * pNext;
};
//设立尾指针的单循环链表基本操作(12)个
//1构造一个空链表
struct Node * init_list()
{
struct Node * pTail;
pTail = (struct Node *)malloc(sizeof(struct Node));
if (!pTail)
exit(-1);
pTail->pNext = pTail;
return pTail;
}
//2将链表置为空
void clear_list(struct Node * pTail)
{
struct Node * p, * q;
pTail = pTail->pNext; //指向了头结点
p = pTail->pNext; //指向了头结点的下一个结点,即第一个结点
while (p != pTail)
{
q = p->pNext;
free(p);
p = q;
}
pTail->pNext = pTail;
}
//3销毁循环链表
void destroy_list(struct Node * pTail)
{
clear_list(pTail);
free(pTail);
pTail = NULL;
}
//4判断是否为空
bool is_empty(struct Node * pTail)
{
if (pTail->pNext == pTail)
return true;
else
return false;
}
//5返回链表中元素个数
int length_list(struct Node * pTail)
{
int cnt = 0;
struct Node * p;
p = pTail->pNext;
while (pTail != p)
{
cnt++;
p = p->pNext;
}
return cnt;
}
//6取得第pos个位置上元素的值
bool get_list(struct Node * pTail, int pos, ElemType * value)
{
int i = 1;
struct Node * p = pTail->pNext->pNext;
if (pos < 1 && pos > length_list(pTail))
return false;
while (i < pos)
{
i++;
p = p->pNext;
}
*value = p->data;
return true;
}
//7给定一个数,得到线性表中与此数相等的最小的位序
bool compare(ElemType e1, ElemType e2)
{
if (e1 == e2)
return true;
else
return false;
}
int locate_list(struct Node * pTail, ElemType val)
{
int i = 0;
struct Node * p = pTail->pNext->pNext;
while (p != pTail->pNext)
{
i++;
if(compare(p->data, val))
return i;
p = p->pNext;
}
return 0;
}
//8给定一个数,判断是否在链表里,如果在,从第二个元素开始得到此数的前一个数
bool prior_list(struct Node * pTail, ElemType cur_e, ElemType * pre_e)
{
struct Node * q;
struct Node * p = pTail->pNext->pNext;
q = p->pNext;
while (q != pTail->pNext)
{
if (q->data == cur_e)
{
*pre_e = p->data;
return true;
}
p = q;
q = q->pNext;
}
return false;
}
//9返回cur的后一个
bool next_list(struct Node * pTail, ElemType cur_e, ElemType * next_e)
{
struct Node * p = pTail->pNext->pNext;
while (p != pTail)
{
if (p->data == cur_e)
{
*next_e = p->pNext->data;
return true;
}
p = p->pNext;
}
return false;
}
//10插入数据
bool insert_list(struct Node ** pTail, int pos, int value)
{
int i = 0;
struct Node * p = (*pTail)->pNext; //p存放头结点
if (pos <= 0 && pos > length_list(*pTail) + 1)
return false;
struct Node * pNew = (struct Node *) malloc(sizeof (struct Node));
while (i < pos - 1)
{
p = p->pNext;
i++;
}
struct Node * q = p->pNext;
pNew->data = value;
p->pNext = pNew;
pNew->pNext = q;
if (p == *pTail)
*pTail = pNew;
return true;
}
bool delete_list(struct Node * pTail, int pos, int * val)
{
int i = 0;
struct Node * p = pTail->pNext;
if (pos <= 0 && pos > length_list(pTail) + 1)
return false;
while (i < pos - 1)
{
p = p->pNext;
i++;
}
struct Node * q = p->pNext;
*val = q->data;
p->pNext = q->pNext;
if (pTail == q)
pTail->pNext = p;
free(q);
q =NULL;
return true;
}
//12遍历链表
void traverse_list(struct Node * pTail)
{
struct Node * p;
p = pTail->pNext->pNext;
while (p != pTail->pNext)
{
printf("%d ", p->data);
p = p->pNext;
}
printf("\n");
}
测试
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
struct Node * pTail;
pTail = init_list();
for (int i = 0; i < 6; i++)
{
insert_list(&pTail, i+1, 2 * i);
}
insert_list(&pTail, 7, 13);
insert_list(&pTail, 8, 16);
traverse_list(pTail);
int len = length_list(pTail);
printf("len = %d\n", len);
ElemType val;
get_list(pTail, 4, &val);
printf("val = %d\n", val);
ElemType delete_val;
delete_list(pTail, 5, &delete_val);
printf("delete_val = %d\n", delete_val);
traverse_list(pTail);
int cnt = locate_list(pTail, 4);
printf("cnt = %d\n", cnt);
ElemType pre_e;
ElemType next_e;
prior_list(pTail, 13, &pre_e);
next_list(pTail, 13, &next_e);
printf("pre_e = %d\n", pre_e);
printf("next_e = %d\n", next_e);
traverse_list(pTail);
return 0;
}