C语言相关问题
遇到的问题:输入所删掉对应作者后,不能输出。
题目:建立一个链表,每个结点包括:书号、书名、作者、出版社、价格、出版时间。输入一个作者,如果链表中结点所包含的作者等于此作者,则将此结点删去。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct author
{
char num[20];
char name[20];
char auth[20];
char public[20];
float price;
char time[20];
struct author* next;
}auth[10];
int main()
{
struct author* p, * pt, * head;
p = NULL;
head = NULL;
pt = NULL;
int i, length, flag = 1;
char iauth[20] = NULL;
int find = 0;
while (flag == 1)
{
printf("input length of list(<10)\n");
scanf("%d", &length);
if (length < 10)
{
flag = 0;
}
}
for (i = 0; i < length; i++)
{
p = (struct author*)malloc(sizeof(struct author));
if (i == 0)
head = pt = p;
else
pt->next = p;
pt = p;
printf("num.");
scanf("%s", p->num);
printf("name:");
scanf("%s", p->name);
printf("author:");
scanf("%s", p->auth);
printf("public:");
scanf("%s", p->public);
printf("price:");
scanf("%f", &p->price);
printf("time:");
scanf("%s", &p->time);
}
p->next = NULL;
p = head;
printf("\n num. name author public price time\n");
while (p != NULL)
{
printf("%4s%8s%6s%6s%6f%6s\n", p->num, p->name, p->auth, p->public, p->price, p->time);
p = p->next;
}
printf("input author:");
scanf("%s", iauth);
pt = head;
p = pt;
if (strcmp(pt->auth, iauth))
{
p = pt->next;
head = pt = p;
find = 1;
}
else
pt = pt->next;
while (pt != NULL)
{
if (strcmp(pt->auth, iauth))
{
p->next = pt->next;
find = 1;
}
else
{
p = pt;
pt = pt->next;
}
}
if (!find)
{
printf("not found %s", iauth);
}
p = head;
printf("\nnum name author public price time\n");
while (p != NULL)
{
printf("%4s%8s%6s%6s%6f%6s", p->num, p->name, p->auth, p->public, p->price, p->time);
p = p->next;
}
return 0;
}