#include <stdio.h>
#include <stdlib.h>
struct Book{
int x;
struct Book *next;
};
void InsertNode(struct Book **L,int x){
struct Book* new;
struct Book* qian;
struct Book* hou;
qian=*L;
hou=NULL;
while(qian!=NULL&&qian->x<x){
hou=qian;
qian=qian->next;
}
new=(struct Book*)malloc(sizeof(struct Book));
if(new==NULL){
printf("内存分配失败!");
exit(1);
}
new->x=x;
new->next=qian;
if(hou==NULL){
*L=new;
}else{
hou->next=new;
}
}
void delete(struct Book**L,int d){
struct Book*qian;
struct Book*hou;
qian=*L;
hou=NULL;
while(qian!=NULL&&qian->x!=d){
hou=qian;
qian=qian->next;
}
if(qian==NULL){
printf("找不到");
return ;
}else{
if(hou==NULL)
{
*L=qian->next;
}else{
hou->next=qian->next;
free(qian);
}
}
}
void BookPrint(struct Book*L){
struct Book* qian;
qian=L;
while(qian!=NULL){
printf(" %d ",qian->x);
qian=qian->next;
}
putchar('\n');
}
int main(int argc, char** argv) {
struct Book *L=NULL;
int x,d;
while(1){
printf("请输入x\n");
scanf("%d",&x);
if(x==-1){
break;
}
InsertNode(&L,x);
printf("打印结果是\n");
BookPrint(L);
printf("删除开始 输入删除数\n");
scanf("%d",&d);
delete(&L,d);
printf("\n执行完毕\n");
BookPrint(L);
}
return 0;
}