头插法
#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;}
#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;
}

尾插法
#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;
}
中间插入节点
- 插入元素效率链表要比数组效率高
- 链表只需要改动指针,而数组要后移多个节点或者重新申请内存,造成大量数据拷贝操作

#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;
}
删除元素


#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&¤t->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;
}


