image.png

头插法

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Book{
  4. char title[128];
  5. char author[128];
  6. struct Book* next;
  7. };
  8. void getInput(struct Book* book);
  9. void addBook(struct Book** library);
  10. void printLibrary(struct Book* library);
  11. void releaseLibrary(struct Book* library);
  12. void getInput(struct Book* book){
  13. printf("in put book tittle\n");
  14. scanf("%s",book->title);
  15. printf("in put author\n");
  16. scanf("%s",book->author);
  17. }
  18. void addBook(struct Book** library){ //因为要修改指针所以使用了二重指针!!!!
  19. struct Book *book,*tempBook;
  20. book=(struct Book*)malloc(sizeof(struct Book));
  21. if (book==NULL){
  22. printf("mem require fault\n");
  23. exit(1);
  24. }
  25. getInput(book);
  26. if(*library!=NULL){
  27. tempBook=*library;
  28. *library=book;
  29. book->next=tempBook;
  30. }else{
  31. *library=book;
  32. book->next=NULL;
  33. }
  34. }
  35. void printLibrary(struct Book* library){
  36. struct Book* book;
  37. int count=1;
  38. book=library;
  39. while(book!=NULL){
  40. printf("BOOK:%s \n",book->title);
  41. printf("auth:%s \n\n",book->author);
  42. book=book->next;
  43. count++;
  44. }
  45. }
  46. void releaseLibrary(struct Book*library){
  47. struct Book* tmpbook;
  48. while(*library=NULL){
  49. tmpbook=*library;
  50. *library=(*library)->next;
  51. free(tmpbook);// 此处要小心use after free 问题
  52. }
  53. }
  54. int main(){
  55. struct Book* library=NULL;
  56. int ch;
  57. while(1){
  58. printf("in put book(Y/N): \n");
  59. do
  60. {
  61. ch=getchar();
  62. } while (ch!='Y'&&ch!='N');
  63. if (ch=='Y'){
  64. addBook(&library);
  65. }else {
  66. break;
  67. }
  68. }
  69. printf("print book ?(Y/N): \n");
  70. do
  71. {
  72. ch=getchar();
  73. } while (ch!='Y'&&ch!='N');
  74. if (ch=='Y'){
  75. printLibrary(library);
  76. }
  77. releaseLibrary(library);
  78. return 0;
  79. }
#include <stdio.h>
#include <stdlib.h>
struct Book{
    char title[128];
    char author[128];
    struct Book* next;
};
void getInput(struct Book* book);
void addBook(struct Book** library);
void printLibrary(struct Book* library);
void releaseLibrary(struct Book** library);


void getInput(struct Book* book){
    printf("in put book tittle\n");
    scanf("%s",book->title);
    printf("in put author\n");
    scanf("%s",book->author);
}

void addBook(struct Book** library){  //因为要修改指针所以使用了二重指针!!!!
    struct Book *book,*tempBook;
    book=(struct Book*)malloc(sizeof(struct Book));
    if (book==NULL){
        printf("mem require fault\n");
        exit(1);
    }
    getInput(book);
    if(*library!=NULL){
        tempBook=*library;
        *library=book;
        book->next=tempBook;

    }else{
        *library=book;
        book->next=NULL;
    }
}

void printLibrary(struct Book* library){
    struct Book* book;
    int count=1;
    book=library;
    while(book!=NULL){
        printf("BOOK:%s \n",book->title);
        printf("auth:%s \n\n",book->author);
        book=book->next;
        count++;
    }

}

void releaseLibrary(struct Book **library){
    struct Book* tmpbook;
    while(*library!=NULL){
        tmpbook=*library;
        *library=(*library)->next;
        free(tmpbook);// 此处要小心use after free 问题
    }
}



int main(){
    struct Book* library=NULL;
    int ch;
    while(1){
        printf("in put book(Y/N): \n");
        do
        {
            ch=getchar();
        } while (ch!='Y'&&ch!='N');

        if (ch=='Y'){
            addBook(&library);
        }else {
            break;
        }


    }
    printf("print book ?(Y/N): \n");
    do
    {
        ch=getchar();
    } while (ch!='Y'&&ch!='N');
    if (ch=='Y'){
        printLibrary(library);

    }
    releaseLibrary(&library);

    return 0;
}

image.png

尾插法

#include <stdio.h>
#include <stdlib.h>
struct Book{
    char title[128];
    char author[128];
    struct Book* next;
};
void getInput(struct Book* book);
void addBook(struct Book** library);
void printLibrary(struct Book* library);
void releaseLibrary(struct Book** library);


void getInput(struct Book* book){
    printf("in put book tittle\n");
    scanf("%s",book->title);
    printf("in put author\n");
    scanf("%s",book->author);
}

void addBook(struct Book** library){  //因为要修改指针所以使用了二重指针!!!!
    struct Book *book,*tempBook;
    book=(struct Book*)malloc(sizeof(struct Book));
    if (book==NULL){
        printf("mem require fault\n");
        exit(1);
    }
    getInput(book);
    if(*library!=NULL){
        tempBook=*library;
        //定位尾部
        while(tempBook->next!=NULL){
            tempBook=tempBook->next;
        }
        //插入数据
        tempBook->next=book;
        book->next=NULL;


    }else{
        *library=book;
        book->next=NULL;
    }
}

void printLibrary(struct Book* library){
    struct Book* book;
    int count=1;
    book=library;
    while(book!=NULL){
        printf("BOOK:%s \n",book->title);
        printf("auth:%s \n\n",book->author);
        book=book->next;
        count++;
    }

}

void releaseLibrary(struct Book **library){
    struct Book* tmpbook;
    while(*library!=NULL){
        tmpbook=*library;
        *library=(*library)->next;
        free(tmpbook);// 此处要小心use after free 问题
    }
}



int main(){
    struct Book* library=NULL;
    int ch;
    while(1){
        printf("in put book(Y/N): \n");
        do
        {
            ch=getchar();
        } while (ch!='Y'&&ch!='N');

        if (ch=='Y'){
            addBook(&library);
        }else {
            break;
        }


    }
    printf("print book ?(Y/N): \n");
    do
    {
        ch=getchar();
    } while (ch!='Y'&&ch!='N');
    if (ch=='Y'){
        printLibrary(library);

    }
    releaseLibrary(&library);

    return 0;
}

改进

尾插法每次都要遍历单链表一次找到尾部位置,改进:用一个变量每次保存链表尾部位置

#include <stdio.h>
#include <stdlib.h>
struct Book{
    char title[128];
    char author[128];
    struct Book* next;
};
void getInput(struct Book* book);
void addBook(struct Book** library);
void printLibrary(struct Book* library);
void releaseLibrary(struct Book** library);


void getInput(struct Book* book){
    printf("in put book tittle\n");
    scanf("%s",book->title);
    printf("in put author\n");
    scanf("%s",book->author);
}

void addBook(struct Book** library){  //因为要修改指针所以使用了二重指针!!!!
    struct Book *book;
    static struct Book *tail;
    book=(struct Book*)malloc(sizeof(struct Book));
    if (book==NULL){
        printf("mem require fault\n");
        exit(1);
    }
    getInput(book);
    if(*library!=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:%s \n",book->title);
        printf("auth:%s \n\n",book->author);
        book=book->next;
        count++;
    }

}

void releaseLibrary(struct Book **library){
    struct Book* tmpbook;
    while(*library!=NULL){
        tmpbook=*library;
        *library=(*library)->next;
        free(tmpbook);// 此处要小心use after free 问题
    }
}



int main(){
    struct Book* library=NULL;
    int ch;
    while(1){
        printf("in put book(Y/N): \n");
        do
        {
            ch=getchar();
        } while (ch!='Y'&&ch!='N');

        if (ch=='Y'){
            addBook(&library);
        }else {
            break;
        }


    }
    printf("print book ?(Y/N): \n");
    do
    {
        ch=getchar();
    } while (ch!='Y'&&ch!='N');
    if (ch=='Y'){
        printLibrary(library);

    }
    releaseLibrary(&library);

    return 0;
}

搜索单链表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book{
    char title[128];
    char author[128];
    struct Book* next;
};
void getInput(struct Book* book);
void addBook(struct Book** library);
void printLibrary(struct Book* library);
void releaseLibrary(struct Book** library);
struct Book* searchBook(struct Book *library,char *target);
void printbook(struct Book* book);

void getInput(struct Book* book){
    printf("in put book tittle\n");
    scanf("%s",book->title);
    printf("in put author\n");
    scanf("%s",book->author);
}

void addBook(struct Book** library){  //因为要修改指针所以使用了二重指针!!!!
    struct Book *book;
    static struct Book *tail;
    book=(struct Book*)malloc(sizeof(struct Book));
    if (book==NULL){
        printf("mem require fault\n");
        exit(1);
    }
    getInput(book);
    if(*library!=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:%s \n",book->title);
        printf("auth:%s \n\n",book->author);
        book=book->next;
        count++;
    }

}

void releaseLibrary(struct Book **library){
    struct Book* tmpbook;
    while(*library!=NULL){
        tmpbook=*library;
        *library=(*library)->next;
        free(tmpbook);// 此处要小心use after free 问题
    }
}

struct Book* searchBook(struct Book* library, char* target){
    struct Book* book;
    book=library;
    while (book!=NULL){
        if (!strcmp(target,book->title)||!strcmp(target,book->author)){
            break;
        }
        book=book->next;


    }
    return book;
}

void printbook(struct Book* book){
    printf("tittle:%s\n",book->title);
    printf("author:%s\n",book->author);
}

int main(){
    struct Book* library=NULL;
    struct Book* book=NULL;
    int ch;
    char input[128];
    while(1){
        printf("in put book(Y/N): \n");
        do
        {
            ch=getchar();
        } while (ch!='Y'&&ch!='N');

        if (ch=='Y'){
            addBook(&library);
        }else {
            break;
        }


    }
    printf("print book ?(Y/N): \n");
    do
    {
        ch=getchar();
    } while (ch!='Y'&&ch!='N');
    if (ch=='Y'){
        printLibrary(library);

    }
    printf("please search book! ,input tittle or author\n");
    scanf("%s",input);
    book=searchBook(library,input);
    if (book==NULL){
        printf("book not found\n");
    }else{
        do
        {
            printf("book found\n");
            printbook(book);
        } while ((book=searchBook(book->next,input))!=NULL);

    }


    releaseLibrary(&library);

    return 0;
}

中间插入节点

  • 插入元素效率链表要比数组效率高
  • 链表只需要改动指针,而数组要后移多个节点或者重新申请内存,造成大量数据拷贝操作

image.png

#include <stdio.h>
#include <stdlib.h>

struct Node{
    int value;
    struct Node* next;
};
void insertNode(struct Node** head, int value){
    struct Node* previous;
    struct Node* current;
    struct Node* new;
    current = *head;
    previous = NULL;
    //定位
    while(current!=NULL && current->value<value){
        previous = current;
        current = current->next;

    }
    new =(struct Node*)malloc(sizeof(struct Node));
    if  (new == NULL){
        printf("mem malloc failure\n");

    }
    new->value = value;
    new->next =current;
    if (previous==NULL){//空链表情况
        *head = new;

    }else{
        previous->next =new;
    }
}

void printNode(struct Node *head){
    struct Node *current;
    current = head;
    while (current!=NULL){
        printf("%d\n",current->value);
        current = current->next;
    }
    putchar('\n');
}
int main(void){
    struct Node* head=NULL;
    int input;
    while(1){
        printf("input a number (-1 exit)\n");
        scanf("%d",&input);
        if (input==-1){
            break;
        }
        insertNode(&head,input);
        printNode(head);    
    }
    return 0;
}

删除元素

image.png
image.png

#include <stdio.h>
#include <stdlib.h>

struct Node{
    int value;
    struct Node* next;
};
void insertNode(struct Node** head, int value){
    struct Node* previous;
    struct Node* current;
    struct Node* new;
    current = *head;
    previous = NULL;
    //定位
    while(current!=NULL && current->value<value){
        previous = current;
        current = current->next;

    }
    new =(struct Node*)malloc(sizeof(struct Node));
    if  (new == NULL){
        printf("mem malloc failure\n");

    }
    new->value = value;
    new->next =current;
    if (previous==NULL){//空链表情况
        *head = new;

    }else{
        previous->next =new;
    }
}
void delNode(struct Node** head, int value){
    struct Node* previous;
    struct Node* current;
    current =*head;
    previous = NULL;
    while(current!=NULL&&current->value!=value){
        previous = current;
        current = current->next;
    }
    if (current==NULL)
    {
        printf("node not exited\n");
        return;
    }else{
        if(previous==NULL){
            *head = current->next;

        }else{
            previous->next = current->next;
        }
        free(current);

    }
}

void printNode(struct Node *head){
    struct Node *current;
    current = head;
    while (current!=NULL){
        printf("%d\n",current->value);
        current = current->next;
    }
    putchar('\n');
}
int main(void){
    struct Node* head=NULL;
    int input;
    printf("\ntest insert number\n");
    while(1){
        printf("\ninput a number (-1 exit)\n");
        scanf("%d",&input);

        if (input==-1){
            break;
        }
        insertNode(&head,input);
        printNode(head);    
    }

    printf( "\ndel number\n");
    while(1){
        printf("\ninput a number (-1 exit)\n");
        scanf("%d",&input);
        if (input==-1){
            break;
        }
        delNode(&head,input);
        printNode(head);    
    }

    return 0;
}

image.png