#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无用
悬赏问题
- ¥15 宇视监控服务器无法登录
- ¥15 PADS Logic 原理图
- ¥15 PADS Logic 图标
- ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
- ¥15 DruidDataSource一直closing
- ¥20 气象站点数据求取中~
- ¥15 如何获取APP内弹出的网址链接
- ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
- ¥50 STM32单片机传感器读取错误
- ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据