双向循环链表的数据添加为一个环形,重点在于对链表节点的连接;
相信配合代码大家会更加清晰的理解;核心就是先连后断。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* pnext;
struct node* Ppore;
};
void endadd(struct node* blankhead, int* count, int adddata);
int main()
{
//创造空头
struct node blankhead;
blankhead.data = 0;
blankhead.pnext = &blankhead;
blankhead.Ppore = &blankhead;
int count = 0;
endadd(&blankhead, &count, 9);
endadd(&blankhead, &count, 8);
endadd(&blankhead, &count, 7);
return 0;
system("pause>0");
}
void endadd(struct node * blankhead, int* count, int adddata)
{
if (NULL == blankhead)
return;
struct node* ptemp = (struct node*)malloc(sizeof(struct node));
if (NULL == ptemp)
return;
//连接
ptemp->data = adddata;
ptemp->pnext = NULL;
ptemp->Ppore = NULL;
ptemp->Ppore = blankhead->Ppore;
ptemp->pnext = blankhead;
//后断(有顺序)
blankhead->Ppore->pnext = ptemp;
blankhead->Ppore = ptemp;
(*count)++;
}