学点 C 语言(26): 数据类型 - 结构的更多可能(2)
printf("%d,%d,%d,%d", R.ID, R.num.x, R.num.y, R.num.z);
getchar();
return 0;
}
3. 链表(结构中的指针):
#include
int main(void)
{
struct Rec {
int x;
int y;
struct Rec *next; /* 结构中的指针, 这是建立链表的基础 */
} *p=NULL, *pFirst=NULL, *pPrec=NULL;
/* 建立链表 */
int i;
for (i = 0; i < 10; i++) {
p = (struct Rec *)malloc(sizeof(struct Rec));
p->x = i;
p->y = i*i;
p->next = NULL;
if (!pFirst) {
pFirst = p;
pPrec = p;
} else {
pPrec->next = p;
pPrec = p;
}
}
/* 遍历链表 */
p = pFirst;
while (p) {
printf("%d, %dn", p->x, p->y);
p = p->next;
}
/* 释放链表 */
p = pFirst;
while (p) {
pPrec = p;
p = p->next;
free(pPrec);
}
getchar();
return 0;
}