#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include<string.h>
#define N 10
#define error 0
#define ok 1
typedef struct{
char id[N];
char name[N];
float price;
}book;
typedef book elemtype;
typedef struct lnode{
elemtype data;
struct lnode *next;
}lnode,*linklist;
//初始化
void intlist(linklist l){
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
}
//尾插法
linklist creatlist(int n){
linklist l;
lnode *p;
lnode *r;
int i;
r = l;
for (i=1; i<=n; i++) {
p = (linklist)malloc(sizeof(lnode));
printf("输入数据%d:",i);
scanf("%s %s %f",&p->data.id,&p->data.name,&p->data.price);
p->next = r->next;
r->next = p;
r = p;
}
return l;
}
//头插法
linklist creatlist1(int n){
lnode *p;
linklist l;
int i;
for(i=0;i<n;i++)
{
p=(linklist)malloc(sizeof(lnode));
printf("输入数据 %d:", i + 1);
scanf("%s %s %f",&p->data.id,&p->data.name,&p->data.price);
p->next=l->next;
l->next=p;
}
return l;
}
//输出
int printlist(linklist l)
{
lnode *p;
int i;
p=l->next;
while(p!=NULL)
{
printf("data:%s %s %f",p->data.id,p->data.name,p->data.price);
p = p->next;
}
return ok;
}
int main()
{
linklist l;
int n;
int i;
printf("请输入表L元素个数n:");
scanf("%d",&n);
intlist(l);
creatlist1(n);
printlist(l);
}
无论用头插还是尾插法可以运行但都没办法输出 求解答
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include<string.h>
#define N 10
#define error 0
#define ok 1
typedef struct {
char id[N];
char name[N];
float price;
}book;
typedef book elemtype;
typedef struct lnode {
elemtype data;
struct lnode* next;
}lnode, * linklist;
//初始化
void intlist(linklist* l) { //修改
(*l) = (linklist)malloc(sizeof(lnode)); //修改
(*l)->next = NULL; //修改
}
//尾插法
linklist creatlist(linklist l,int n) { //修改
//linklist l; //修改
lnode* p;
lnode* r;
int i;
r = l;
for (i = 1; i <= n; i++) {
p = (linklist)malloc(sizeof(lnode));
printf("输入数据%d:", i);
scanf("%s %s %f", p->data.id, p->data.name, &p->data.price);//修改
//scanf("%s %s %f", &p->data.id, &p->data.name, &p->data.price);
p->next = r->next;
r->next = p;
r = p;
}
return l;
}
//头插法
linklist creatlist1(linklist l, int n) { //修改
lnode* p;
//linklist l; //修改
int i;
for (i = 0; i < n; i++)
{
p = (linklist)malloc(sizeof(lnode));
printf("输入数据 %d:", i + 1);
scanf("%s %s %f", p->data.id, p->data.name, &p->data.price);//修改
//scanf("%s %s %f", &p->data.id, &p->data.name, &p->data.price);
p->next = l->next;
l->next = p;
}
return l;
}
//输出
int printlist(linklist l)
{
lnode* p;
int i;
p = l->next;
while (p != NULL)
{
printf("data:%s %s %f\n", p->data.id, p->data.name, p->data.price);
p = p->next;
}
return ok;
}
int main()
{
linklist l;
int n;
int i;
printf("请输入表L元素个数n:");
scanf("%d", &n);
intlist(&l); //修改
creatlist1(l, n);//修改
printlist(l);
}
代码修改如下:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include<string.h>
#define N 10
#define error 0
#define ok 1
typedef struct {
char id[N];
char name[N];
float price;
}book;
typedef book elemtype;
typedef struct lnode {
elemtype data;
struct lnode* next;
}lnode, * linklist;
//初始化
linklist intlist(linklist l) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
return l;
}
//尾插法
linklist creatlist(linklist l,int n) {
//linklist l;
lnode* p;
lnode* r;
int i;
r = l;
for (i = 1; i <= n; i++) {
p = (linklist)malloc(sizeof(lnode));
printf("输入数据%d:", i);
scanf("%s %s %f", p->data.id, p->data.name, &p->data.price);
p->next = 0;
r->next = p;
r = p;
}
return l;
}
//头插法
linklist creatlist1(linklist l,int n) {
lnode* p;
//linklist l;
int i;
for (i = 0; i < n; i++)
{
p = (linklist)malloc(sizeof(lnode));
printf("输入数据 %d:", i + 1);
scanf("%s %s %f", p->data.id, p->data.name, &p->data.price);
p->next = l->next;
l->next = p;
}
return l;
}
//输出
int printlist(linklist l)
{
lnode* p;
int i;
p = l->next;
while (p != NULL)
{
printf("data:%s %s %f", p->data.id, p->data.name, p->data.price);
p = p->next;
}
return ok;
}
int main()
{
linklist l=0;
int n;
int i;
printf("请输入表L元素个数n:");
scanf("%d", &n);
l = intlist(l);
creatlist1(l,n);
printlist(l);
return 0;
}