链表的练习

来源:百度知道 编辑:UC知道 时间:2024/06/21 23:28:48
编写一个菜单驱动的仓库管理程序,要求如下:
1、 能输入并显示n个货品的货号、类别、名称、价格、产地、批号(按链表存储)
2、 任意输入一个类别,按该类别的价格由高到低排序
3、 任意输入一个货号,能显示该货品的货号、类别、名称、价格、产地、批号
4、 增加和删除某货品
我做的是这样的:
编译没有错误,可是运行就会出问题,有高手解答吗?
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct thing)
#define NULL 0
struct thing
{
long num;
int leibie;
char name;
long price;
char from;
long NO;
struct thing *next;
};
int n;
struct thing *creat(void)
{
struct thing *head;
struct thing *p1,*p2;
n=0;
p1=p2=(struct thing*)malloc(LEN);
scanf("%d,%d,%c,%d,%c,%d",&p1->num,&p1->leibie,&p1->name,&p1->price,&p1->from,&p1->NO);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct thing*)malloc(LEN);
scanf("%d,%d,%c,%d,%c,%d",&p1->num,&p1

倒数第二行的paixu()的返回值是void,不需要给head赋值。
中间定义的 i ,j都没用上,可以去掉。
struct thing *creat(void) 这个里面的第一个if好像没有endif.
算法我还没看,应该没问题吧。

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct thing)
#define NULL 0
struct thing
{
long num;
int leibie;
char name;
long price;
char from;
long NO;
struct thing *next;
};
int n;
struct thing *creat(void)
{
struct thing *head;
struct thing *p1,*p2;
n=0;
p1=p2=(struct thing*)malloc(LEN);
scanf("%d,%d,%c,%d,%c,%d",&p1->num,&p1->leibie,&p1->name,&p1->price,&p1->from,&p1->NO);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct thing*)malloc(LEN);
scanf("%d,%d,%c,%d,%c,%d",&p1->num,&p1->leibie,&p1->name,&p1->price,&p1->from,&p1->NO)