学习参考: 严蔚敏: 《数据结构-C语言版》
单向循环链表的基本操作
基本操作代码实现
单向循环链表的结点定义
typedef struct node
{
struct node* next; // 指向下一个结点的指针域
int data; // 该结点的数据
}*pNode, Node;
单向循环链表的定义
typedef struct
{
pNode node; // 指向头节点的指针域
int len; // 单向循环链表的长度
}*pCirList, CirList;
单向循环链表的建立
int initList(pCirList list)
{
if(!list)
return 0;
list->node = NULL;
list->len = 0;
return 1;
}
单向循环链表添加结点(尾插法)
int addDataHead(pCirList list, int data)
{
pNode p = NULL, q = NULL;
int count = 1;
if(!list)
return 0;
p = list->node;
while( countlen)
{
count++;
p=p->next;
}
q = (pNode)malloc(sizeof(Node));
q->data = data;
if( count > list->len && !p)
q->next = q;
else
{
q->next = list->node;
p->next = q;
}
list->node = q;
list->len++;
return 0;
}
单向循环链表添加结点(尾插法)
int addDataTial(pCirList list, int data)
{
pNode q = NULL, p = NULL;
int count = 1;
if(!list)
return 0;
p = list->node;
while( count++len)
p = p->next;
q = (pNode)malloc(sizeof(Node));
q->data = data;
if(!p)
{
list->node = q;
q->next = q;
}
else
{
q->next = p->next;
p->next = q;
}
list->len ++;
return 1;
}
单向循环链表的输出
int display(pCirList list)
{
pNode p = NULL;
int count = 0;
if(!list || !list->node)
return 0;
p = list->node;
while( countlen)
{
count++;
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 1;
}
单向循环链表的修改
int updata(pCirList list, int pos, int data)
{
pNode p = NULL;
int count = 1;
if(!list || !list->node || pos<1 || pos>list->len)
return 0;
p = list->node;
while( countnext;
}
p->data = data;
return 1;
}
单向循环链表的插入
int insertData(pCirList list, int pos, int data)
{
pNode pre = NULL, p = NULL;
int count = 1;
if(!list || pos<1 || pos>list->len+1)
return 0;
p = list->node;
while ( countnext;
++count;
}
if(count == 1 || pos == list->len)
addDataTial(list,data);
else
{
p = (pNode)malloc(sizeof(Node));
p->next = pre->next;
p->data = data;
pre->next = p;
list->len ++;
}
return 1;
}
单向循环链表的删除
int deleteData(pCirList list, int pos, int* e)
{
pNode pre = NULL, p = NULL;
int count = 1;
if(!list || !list->node || pos<1 || pos>list->len+1)
return 0;
p = list->node;
if(pos==1)
{
while (countlen)
{
count++;
p = p->next;
}
pre = p->next;
list->node = pre->next;
p->next = list->node;
*e = pre->data;
free(pre);
pre = NULL;
}
else
{
while ( countnext;
count++;
}
pre->next = p->next;
*e = p->data;
free(p);
p = NULL;
}
list->len --;
return 1;
}
单向循环链表按序号查找
int getData(pCirList list, int pos, int* data)
{
pNode p = NULL;
int count = 1;
if(!list || !list->node || pos<1 || pos>list->len)
return 0;
p = list->node;
while(countnext;
count++;
}
*data = p->data;
return 0;
}
单向循环链表按值查找
int getLocate(pCirList list, int data, int* index)
{
pNode p = NULL;
int count = 1;
if(!list || !list->node)
return 0;
p = list->node;
while( countlen)
{
if(p->data==data)
break;
p = p->next;
count++;
}
if(count == list->len)
*index = -1;
else
*index = count;
return 1;
}
单向循环链表销毁
int destroy(pCirList list)
{
pNode q = NULL, p = NULL;
int count = 0;
if(!list || !list->node)
return 0;
p = list->node;
while(countlen)
{
q = p;
p = p->next;
count++;
free(q);
q = NULL;
}
list->node = NULL;
list->len = 0;
return 0;
}
求单向循环链表的长
int getLength(pCirList list,int* len)
{
if(!list)
return 0;
*len = list->len;
return 1;
}
测试代码
#include
#include "CircularList.h"
int main()
{
int i=1;
int val = -1;
CirList list;
initList(&list);
/* addDataHead(&list, 1);
addDataHead(&list, 2);
addDataTial(&list, 3);
addDataTial(&list, 4);
display(&list);
updata(&list, 1, 1);
display(&list);
updata(&list, 2, 2);
display(&list);*/
for(i; i<11; ++i)
insertData(&list, i, i);
display(&list);
deleteData(&list, 5, &val);
printf("%d \n", val);
getData(&list, 8, &val);
getLocate(&list, 1, &val);
printf("%d \n", val);
destroy(&list);
display(&list);
return 0;
}