#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的语句是{ 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后插入 */
}
x = (struct node *)malloc(sizeof(struct node));
改为
x = (struct node)malloc(sizeof(struct node));
*H 是node指针
**H 是指向 node指针的指针