马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
听了小甲鱼两节单链表,在原本代码增加删除和修改函数#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getInput(struct Book *book);//添加数据
void addBook(struct Book **library);//定位指针
void printLibrary(struct Book *library);//显示所有数据
void printBook(struct Book *book);//显示查找数据
void library_free(struct Book **library);//释放内存
struct Book *searchBook(struct Book *library, char *target);//搜索数据
void delBook(struct Book *book); //删除数据
void reviseBook(struct Book *book);//修改数据
struct Book *delsearchBook(struct Book *library, char *target);//搜索删除数据的前一个
struct Book{
char number[30];
char title[128];
char author[40];
struct Book *next;
};
void getInput(struct Book *book)
{
printf("请输入书号:");
scanf("%s", book->number);
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
}
void addBook(struct Book **library)
{
struct Book *book, *temp;
static struct Book *tail;
book = (struct Book *)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(book);
if (*library != NULL)
{
/*temp = *library;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = book;
book->next = NULL;*/
tail->next = book;
book->next= NULL;
}
else
{
*library = book;
book->next = NULL;
}
tail = book;
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1;
book = library;
while(book != NULL)
{
printf("book%d\n",count);
printf("书号:%s\n", book->number);
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
book = book->next;
count++;
}
}
struct Book *searchBook(struct Book *library, char *target)
{
struct Book *book;
book = library;
while(book != NULL)
{
if(!strcmp(book->number, target))
{
break;
}
book = book->next;
}
return book;
}
void printBook(struct Book *book)
{
printf("书号:%s\n", book->number);
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
}
struct Book *delsearchBook(struct Book *library, char *target)
{
struct Book *book;
book = library;
if(book == NULL){
printf("链表为空,删除失败");
return book;
}
while(book->next != NULL)
{
if(!strcmp(book->next->number, target))
{
break;
}
book = book->next;
book->next = book->next->next;
}
if(book->next == NULL){
printf("没有这个数据,删除失败");
return book;
}
return book;
}
void delBook(struct Book *book)
{
struct Book *del;
del = book->next;
book->next = del->next;
free(del);
printf("删除数据成功\n");
}
void reviseBook(struct Book *book)
{
char a1[128];
char a2[40];
printf("书名修改为:");
scanf("%s",a1);
printf("作者修改为:");
scanf("%s",a2);
strcpy(book->title, a1);
strcpy(book->author, a2);
printf("修改后\n");
printf("书号:%s\n", book->number);
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
}
void library_free(struct Book **library)
{
struct Book *temp;
while(*library != NULL)
{
temp = *library;
*library = (*library)->next;
free(temp);
}
}
int main(void)
{
struct Book *library = NULL;
struct Book *book;
struct Book *delbook;
int ch, a;
char target[30];
while(1)
{
printf("1录入信息,2打印信息,3搜索数据,4删除数据,5修改数据,6退出 :");
scanf("%d", &a);
switch(a)
{
case 1 :
while(1)
{
printf("请问是否需要输入信息(Y/N):");
do
{
ch = getchar();
}while(ch != 'Y' && ch != 'N');
if(ch == 'Y')
{
addBook(&library);
}
else
{
break;
}
}
break;
case 2 :
printLibrary(library);
/*printf("请问是否需要输出信息(Y/N):");
do
{
ch = getchar();
}while(ch != 'Y' && ch != 'N');
if(ch == 'Y')
{
printLibrary(library);
}*/
break;
case 3 :
printf("请输入书号:");
scanf("%s", target);
book = searchBook(library, target);
if(book == NULL)
{
printf("很抱歉,没有找到这个数据\n");
}
else
{
printBook(book);
}
break;
case 4 :
printf("请输入删除的数据的书号:");
scanf("%s",target);
book = delsearchBook(library, target);
delBook(book);
break;
case 5:
printf("请输入修改的数据的书号:");
scanf("%s",target);
book = searchBook(library, target);
reviseBook(book);
break;
case 6:
printf("再见!\n");
library_free(&library);
exit(1);
}
}
return 0;
}
|