建立结构体Linker,其中有两个成员,为结构体Goods类型的变量和指向Linker的指针变量next。遍历输出链表?
发布网友
发布时间:2022-05-23 05:07
我来回答
共1个回答
热心网友
时间:2023-10-14 00:18
#include<stdlib.h>
#include<stdio.h>
struct Goods
{
int number; //商品编号
int typenumber; // 商品类别编号
char name[10]; //商品名称
int storagenum; //库存数量
int price; //单价
};
struct Linker
{
struct Goods g;
struct Linker *next;
};
int main()
{
struct Linker lk1 = {{1, 1001, "Book", 10, 25}, NULL};
struct Linker lk2 = {{2, 1002, "Pen", 12, 2}, NULL};
struct Linker lk3 = {{3, 1003, "food", 15, 15}, NULL};
lk1.next = &lk2;
lk2.next = &lk3;
struct Linker *head = &lk1;
while(head)
{
printf("%2d %6d %10s %6d %3d\n",
head->g.number, head->g.typenumber, head->g.name, head->g.storagenum, head->g.price);
head = head->next;
}
return 0;
}