当前位置: 代码迷 >> 综合 >> day05 c语言 单链表插入
  详细解决方案

day05 c语言 单链表插入

热度:89   发布时间:2024-01-17 14:40:22.0

c语言

单链表插入

首插法


#include<stdio.h>
#include<stdlib.h>struct Book
{
    char title[128];char author[40];struct Book *next;
};void get(struct Book *book)
{
    printf("请输入书名:\n");scanf("%s",book->title);printf("请输入作者:\n");scanf("%s",book->author);}
void add(struct Book **library)
{
    struct Book *book, *temp;book = (struct Book *)malloc(sizeof(struct Book));if( book == NULL){
    printf("分配内存不足!\n");exit(0);}get(book);if(*library != NULL){
    temp = *library;*library = book;book->next = temp;}else{
    *library = book;book->next = NULL;}
}
void  print(struct Book *library)
{
    struct Book *book;int count = 1;book = library;while (book !=NULL){
    printf("Book%d\n",count);printf("书名:%s\n",book->title);printf("作者:%s\n",book->author);book = book->next;count++;}
}
void release(struct Book **library)
{
    struct Book *nsdd;while(*library != NULL){
    nsdd = *library;*library = (*library)->next;free(nsdd);}
}
int main (void)
{
    struct Book *library = NULL;char ch;while(1){
    printf("请问是否需要录入书籍信息?(Y\N)");do{
    ch = getchar();}while(ch != 'Y'&&ch !='N');if(ch =='Y'){
    add(&library);}else{
    break;}}printf("是否需要打印图书信息(Y\N)?");do{
    ch = getchar();}while(ch !='Y'&&ch != 'N');if(ch == 'Y'){
    print(library);}release(&library);return 0;
}

尾插法

#include<stdio.h>
#include<stdlib.h>struct Book
{
    char title[128];char author[40];struct Book *next;
};void get(struct Book *book)
{
    printf("请输入书名:\n");scanf("%s",book->title);printf("请输入作者:\n");scanf("%s",book->author);}
void add(struct Book **library)
{
    struct Book *book, *temp;book = (struct Book *)malloc(sizeof(struct Book));if( book == NULL){
    printf("分配内存不足!\n");exit(0);}get(book);if(*library != NULL){
    temp = *library;//定位单链表while(temp ->next != NULL){
    temp = temp->next;}//插入数据temp ->next = book;book ->next = NULL;}else{
    *library = book;book->next = NULL;}
}
void  print(struct Book *library)
{
    struct Book *book;int count = 1;book = library;while (book !=NULL){
    printf("Book%d\n",count);printf("书名:%s\n",book->title);printf("作者:%s\n",book->author);book = book->next;count++;}
}
void release(struct Book **library)
{
    struct Book *nsdd;while(*library != NULL){
    nsdd = *library;*library = (*library)->next;free(nsdd);}
}
int main (void)
{
    struct Book *library = NULL;char ch;while(1){
    printf("请问是否需要录入书籍信息?(Y\N)");do{
    ch = getchar();}while(ch != 'Y'&&ch !='N');if(ch =='Y'){
    add(&library);}else{
    break;}}printf("是否需要打印图书信息(Y\N)?");do{
    ch = getchar();}while(ch !='Y'&&ch != 'N');if(ch == 'Y'){
    print(library);}release(&library);return 0;
}
#include<stdio.h>
#include<stdlib.h>struct Book
{
    char title[128];char author[40];struct Book *next;
};void get(struct Book *book)
{
    printf("请输入书名:\n");scanf("%s",book->title);printf("请输入作者:\n");scanf("%s",book->author);}
void add(struct Book **library)
{
    struct Book *book;static struct Book *tail;book = (struct Book *)malloc(sizeof(struct Book));if( book == NULL){
    printf("分配内存不足!\n");exit(0);}get(book);if(*library != NULL){
    tail ->next = book;book ->next = NULL;}else{
    *library = book;book->next = NULL;}tail = book;
}
void  print(struct Book *library)
{
    struct Book *book;int count = 1;book = library;while (book !=NULL){
    printf("Book%d\n",count);printf("书名:%s\n",book->title);printf("作者:%s\n",book->author);book = book->next;count++;}
}
void release(struct Book **library)
{
    struct Book *nsdd;while(*library != NULL){
    nsdd = *library;*library = (*library)->next;free(nsdd);}
}
int main (void)
{
    struct Book *library = NULL;char ch;while(1){
    printf("请问是否需要录入书籍信息?(Y\N)");do{
    ch = getchar();}while(ch != 'Y'&&ch !='N');if(ch =='Y'){
    add(&library);}else{
    break;}}printf("是否需要打印图书信息(Y\N)?");do{
    ch = getchar();}while(ch !='Y'&&ch != 'N');if(ch == 'Y'){
    print(library);}release(&library);return 0;
}