单向循环链表定义:
typedef struct node {
int data, sum;
struct node *next;
}LNode;
创建:
//创建链表
LNode* Create( int a[], int len)
{
LNode* L;
//创建头结点
L = (LNode*)malloc(sizeof(LNode));
L->data = a[0];
L->sum = 0;
L->next = L;
LNode *prior, *pcur;
prior = L;
for (int i =1; i < len; i++)
{
pcur = (LNode*)malloc(sizeof(LNode));
pcur->data = a[i];
pcur->sum = 0;
pcur->next = prior->next;
prior->next = pcur;
prior = pcur;
}
return L;
}
打印:
//打印链表
void Print(LNode* L)
{
LNode* p;
p = L->next;
while (p != L)
{
printf("data:%d,sum:%d\n", p->data, p->sum);
p = p->next;
}
//退出循环时,p==L
printf("data:%d,sum:%d\n", p->data, p->sum);
}
求结点sum域值: