循环链表的初始化的思路
循环链表初始化的思路#include <stdlib.h>
#include <stdio.h>
typedef struct tagLinkList
{
int data;
tagLinkList * pNext;
}LL,*LPLL;
void AddNode(LPLL pHead,int data)
{
//将节点插入到头节点的后一个位置
LPLL pNode = (LPLL)malloc(sizeof(LL));
pNode->data = data;
pNode->pNext = pHead->pNext;
pHead->pNext = pNode;
}
void Display(LPLL pHead)
{
LPLL pNode = pHead->pNext;
while(pHead != pNode)
{
printf("%d,",pNode->data);
pNode = pNode->pNext;
}
}
void DestoryList(LPLL pHead)
{
LPLL pNode = pHead->pNext;
while(pHead != pNode)
{
LPLL pNext = pNode->pNext;
free(pNode);
pNode = pNext;
}
}
int main()
{
LL head;
head.data = 0;
//头节点的next节点 = 自己,就构筑了循环链表。
head.pNext = &head;
for(int i = 1 ; i <= 10 ; i ++)
{
AddNode(&head,i);
}
Display(&head);
DestoryList(&head);
return 0;
} xieglt 发表于 2020-10-15 15:12
结构体少个struct
typedef struct tagLinkList
{
int data;
struct tagLinkList * pNext;
}LL,*LPLL;
页:
[1]