// file9.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
#define NULL 0
#define TYPE struct stu
#define LEN sizeof (struct stu)
struct stu
{
int num;
int age;
struct stu *next;
};
struct stu *creat(int n)//返回指针的指针函数
{
struct stu *head, *p_last, *p_new;
int i;
for (i = 0; i<n; i++)
{
p_new = (struct stu*)malloc(sizeof(struct stu));//指针pb指向结构体指针函数
printf("%dth input Number and Age:\n", i + 1);
scanf("%d %d", &p_new->num, &p_new->age);//结构体成员赋值
if (i == 0)
p_last = head = p_new;//复制地址
else
p_last->next = p_new;//将地址赋给上一个地址的next
p_new->next = NULL;//清空
p_last = p_new;//更新
}
return(head);
}
void main()
{
puts("please input the number:");
int n;//几个节点
scanf("%d", &n);
struct stu *p = creat(n);//创建指向链表头的指针
for (int i = 0; i < n; i++)
{
printf("%d %d\n", p->num, p->age);
p = p->next;
}
}
动态链表实例
最新推荐文章于 2022-08-09 11:39:44 发布