|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
小甲鱼视频中线性表12讲循环链表的初始化程序,我改了下,就是把void ds_init(node **pNode)换成void ds_init(node *phead),链表就出错了
1.这是小甲鱼的程序
int main()
{
node *pHead = NULL;
ds_init(&pHead);
printf("\n");
printf("%d",pHead->data);
}
void ds_init(node **pNode)
{
int item;
node *temp;
node *target;
printf("输入结点的值,输入0完成初始化\n");
while(1)
{
scanf("%d", &item);
fflush(stdin);
if(item == 0)
return;
if((*pNode) == NULL)
{ /*循环链表中只有一个结点*/
*pNode = (node*)malloc(sizeof(struct CLinkList));
if(!(*pNode))
exit(0);
(*pNode)->data = item;
(*pNode)->next = *pNode;
}
else
{
/*找到next指向第一个结点的结点*/
for(target = (*pNode); target->next != (*pNode); target = target->next)
;
/*生成一个新的结点*/
temp = (node *)malloc(sizeof(struct CLinkList));
if(!temp)
exit(0);
temp->data = item;
temp->next = *pNode;
target->next = temp;
}
}
}
2.改调二级指针的程序
node * ds_init(node *pHead)
{
int item;
node *temp;
node *target;
while(1)
{
printf("输入结点的值(输入0时完成初始化)\n");
fflush(stdin);
scanf("%d",&item);
if (item == 0)
{
return;
}
if (pHead == NULL)
{/*循环链表只有一个节点*/
pHead = (node *)malloc(sizeof(struct CLinklist));
if (!pHead)
{
exit(0);
}
pHead->data = item;
pHead->next = pHead;
}
else
{
/*找到next指向的第一个节点的结点*/
for (target = pHead;target->next != pHead;target = target->next)
{
;
}
temp = (node *)malloc(sizeof(struct CLinklist));
if (!temp)
{
exit(0);
}
temp->data = item;
temp->next = pHead;
target->next = temp;
}
}
return pHead;
}
int main()
{
node *pHead = NULL;
ds_init(pHead);
printf("\n");
printf("%d",pHead->data);
}
扼言abc 发表于 2019-11-11 18:12
还是有点疑问,这个对循环链表的操作,我的理解是只需要知道头结点就好了,不需要指向头结点的指针
如果想在函数中修改结构数据,不可以传入节点,必须传入指向节点的指针,除非你并不打算修改数据。
|
|