#include <stdlib.h>
#include <stdio.h>
#define LEN sizeof(struct node)
struct node
{ int Data;
struct node *Link;
} *H;
void InsertNode(struct node **H, struct node *p, int b)
{ struct node *x;
x = (struct node *)malloc(sizeof(struct node));
/* 取空结点 */
x->Data = b; x->Link = 0; /* 值为b */
if (p == 0)
{ x->Link = *H; *H = x;} /* 表头插入 */
else
{ x->Link = p->Link; p->Link = x;} /* p后插入 */
}
int main()
{ int A[8]={25,73,60,37,98,90,24,30};
int i;
struct node *p;
H = (struct node *)malloc(sizeof(struct node));
H->Data = A[0]; H->Link = 0;
p = H;
for (i=1; i<8; i++)
{ p->Link = (struct node *)malloc(sizeof(struct node));
p=p->Link; p->Data = A[i]; p->Link = 0;
}
p = H;
printf("打印结点中元素值:\n");
while (p!=0)
{ printf("%5d",p->Data); p = p->Link; }
printf("\n");
p=0;
InsertNode(&H, p, 15);
printf("打印新表中结点元素值:\n");
p=H;
while (p!=0)
{ printf("%5d",p->Data); p = p->Link; }
printf("\n");
}
想链表中插入结点,为什么函数声明用的是**H而不是*H?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
八云黧 2021-07-26 10:45关注原函数中使用到H的语句是
{ x->Link = *H; *H = x;},如果H是struct node *H,*H就是struct node,既不能通过x->Link = *H赋值(类型不匹配),也不能用*H = x保存x。
其实也应该可以写成一次指针void InsertNode(struct node *H, struct node *p, int b) { struct node *x; x = (struct node *)malloc(sizeof(struct node)); /* 取空结点 */ x->Data = b; x->Link = 0; /* 值为b */ if (p == 0) { x->Link = H; H = x;} /* 表头插入 */ else { x->Link = p->Link; p->Link = x;} /* p后插入 */ }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用