链表常见操作源代码

ch=getchar();

}while(ch!=‘Y’&&ch!=‘N’);

if(ch==‘Y’)

{

addBook(&library);

}

else

{

break;

}

}

printf(“please print the information for the book (Y/N)\n”);

do

{

ch=getchar();

}while(ch!=‘Y’&&ch!=‘N’);

if(ch==‘Y’)

{

printLibrary(library);

}

releaseLibrary(library);

return 0;

}

// 输出结果示例:

please enter the information for the book (Y/N)

Y

please input book name:

yrbiufjndkmsl

please input anthor

bvjnk

please enter the information for the book (Y/N)

Y

please input book name:

ybdjksl.viokms

please input anthor

vcb

please enter the information for the book (Y/N)

Y

please input book name:

vyubfh kj

please input anthor

please enter the information for the book (Y/N)

Y

please input book name:

yguvdbshjbj

please input anthor

bbvfhjn

please enter the information for the book (Y/N)

N

please print the information for the book (Y/N)

Y

Book : 1

Book name : yguvdbshjbj

Book anthor : bbvfhjn

Book : 2

Book name : vyubfh

Book anthor : kj

Book : 3

Book name : ybdjksl.viokms

Book anthor : vcb

Book : 4

Book name : yrbiufjndkmsl

Book anthor : bvjnk

Process returned 0 (0x0) execution time : 59.930 s

Press any key to continue.

// 尾插法:

#include<stdio.h>

#include<stdlib.h>

struct Book

{

char title [128];

char author [40];

struct Book *next;

};

void getInput(struct Book *book )

{

printf(“please input book name:\n”);

scanf(“%s”,book->title);

printf(“please input anthor\n”);

scanf(“%s”,book->author);

}

void addBook (struct Book **library)

{

struct Book *book,*temp;

book=(struct Book *)malloc(sizeof(struct Book));

if(book==NULL)

{

printf(“Failed to allcoate memory.\n”);

exit(1);

}

getInput(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 printLibrary (struct Book *library)

{

struct Book *book;

int count=1;

book=library;

while(book!=NULL)

{

printf(“Book : %d\n”,count);

printf(“Book name : %s\n”,book->title);

printf(“Book anthor : %s\n”,book->author);

book=book->next;

count++;

}

}

void releaseLibrary (struct Book *library)

{

struct Book *temp;

while(library!=NULL)

{

temp=library->next;

free(library);

library=temp;

}

}

int main()

{

struct Book *library=NULL;

char ch;

while(1)

{

printf(“please enter the information for the book (Y/N)\n”);

do

{

ch=getchar();

}while(ch!=‘Y’&&ch!=‘N’);

if(ch==‘Y’)

{

addBook(&library);

}

else

{

break;

}

}

printf(“please print the information for the book (Y/N)\n”);

do

{

ch=getchar();

}while(ch!=‘Y’&&ch!=‘N’);

if(ch==‘Y’)

{

printLibrary(library);

}

releaseLibrary(library);

return 0;

}

// 这种方法每次都要去遍历一片链表,效率很低;

// 输出结果:

please enter the information for the book (Y/N)

Y

please input book name:

gyeuwihojpkd

please input anthor

ygvhdun

please enter the information for the book (Y/N)

Y

please input book name:

f7fteybwiduvnioml,

please input anthor

fevydc

please enter the information for the book (Y/N)

Y

please input book name:

gyubdcius;l,lvbwuno

please input anthor

eyftwvsbd

please enter the information for the book (Y/N)

Y

please input book name:

tevuydbvijnkml

please input anthor

vgyg

please enter the information for the book (Y/N)

N

please print the information for the book (Y/N)

Y

Book : 1

Book name : gyeuwihojpkd

Book anthor : ygvhdun

Book : 2

Book name : f7fteybwiduvnioml,

Book anthor : fevydc

Book : 3

Book name : gyubdcius;l,lvbwuno

Book anthor : eyftwvsbd

Book : 4

Book name : tevuydbvijnkml

Book anthor : vgyg

Process returned 0 (0x0) execution time : 42.575 s

Press any key to continue.

// 好,下面是一种改进的尾插法,可以大大的提高效率:

// 尾插法,该进版:

#include<stdio.h>

#include<stdlib.h>

struct Book

{

char title [128];

char author [40];

struct Book *next;

};

void getInput(struct Book *book )

{

printf(“please input book name:\n”);

scanf(“%s”,book->title);

printf(“please input anthor\n”);

scanf(“%s”,book->author);

}

void addBook (struct Book **library)

{

struct Book *book;

static struct Book *tail; // 静态变量,每一次调用addBook函数时,tail指针的值不变

book=(struct Book *)malloc(sizeof(struct Book));

if(book==NULL)

{

printf(“Failed to allcoate memory.\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 : %d\n”,count);

printf(“Book name : %s\n”,book->title);

printf(“Book anthor : %s\n”,book->author);

book=book->next;

count++;

}

}

void releaseLibrary (struct Book *library)

{

struct Book *temp;

while(library!=NULL)

{

temp=library->next;

free(library);

library=temp;

}

}

int main()

{

struct Book *library=NULL;

char ch;

while(1)

{

printf(“please enter the information for the book (Y/N)\n”);

do

{

ch=getchar();

}while(ch!=‘Y’&&ch!=‘N’);

if(ch==‘Y’)

{

addBook(&library);

}

else

{

break;

}

}

printf(“please print the information for the 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 Node{

int value;

struct Node *next;

};

void insertNode(struct Node **head,int value){

struct Node *previous;

struct Node *current;

struct Node *newp;

current=*head;

previous=NULL;

while(current!=NULL&&current->value<value){

previous=current;

current=current->next;

}

newp=(struct Node *)malloc(sizeof(struct Node));

if(newp==NULL){

printf(“内存分配失败\n”);

exit(1);

}

newp->value=value;

newp->next=current;

if(previous==NULL){

*head=newp;

}

else{

previous->next=newp;

}

}

void printNode(struct Node *head){

struct Node *current;

current=head;

while(current!=NULL){

printf("%d ",current->value);

current=current->next;

}

printf(“\n”);

}

int main()

{

struct Node *head=NULL;

int input;

printf(“开始输入插入整数:\n”);

while(1){

printf(“请输入一个整数(输入-1代表结束):”);

scanf(“%d”,&input);

if(input==-1){

break;

}

insertNode (&head,input);

printNode(head);

}

return 0;

}

// 输出结果:

开始输入插入整数:

请输入一个整数(输入-1代表结束):9

9

请输入一个整数(输入-1代表结束):4

4 9

请输入一个整数(输入-1代表结束):5

4 5 9

请输入一个整数(输入-1代表结束):2

2 4 5 9

请输入一个整数(输入-1代表结束):4

2 4 4 5 9

请输入一个整数(输入-1代表结束):1

1 2 4 4 5 9

请输入一个整数(输入-1代表结束):3

1 2 3 4 4 5 9

请输入一个整数(输入-1代表结束):

10

1 2 3 4 4 5 9 10

请输入一个整数(输入-1代表结束):-1

Process returned 0 (0x0) execution time : 21.320 s

Press any key to continue.

// 下面是删除操作源码:

#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 *newp;

current=*head;

previous=NULL;

while(current!=NULL&&current->value<value){

previous=current;

current=current->next;

}

newp=(struct Node *)malloc(sizeof(struct Node));

if(newp==NULL){

printf(“内存分配失败\n”);

exit(1);

}

newp->value=value;

newp->next=current;

if(previous==NULL){

*head=newp;

}

else{

previous->next=newp;

}

}

void deleteNode(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(“找不到匹配的节点\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 ",current->value);

current=current->next;

}

printf(“\n”);

}

int main()

{

struct Node *head=NULL;

int input;

printf(“开始输入插入整数:\n”);

while(1){

printf(“请输入一个整数(输入-1代表结束):”);

scanf(“%d”,&input);

if(input==-1){

break;

}

insertNode (&head,input);

printNode(head);

}

printf(“开始输入要删除的整数:\n”);

while(1){

printf(“请输入一个整数(输入-1代表结束):”);

scanf(“%d”,&input);

if(input==-1){

break;

}

deleteNode (&head,input);

printNode(head);

}

return 0;

}

// 输出结果源码:

开始输入插入整数:

请输入一个整数(输入-1代表结束):9

9

请输入一个整数(输入-1代表结束):5

5 9

请输入一个整数(输入-1代表结束):1

1 5 9

请输入一个整数(输入-1代表结束):4

1 4 5 9

请输入一个整数(输入-1代表结束):5

1 4 5 5 9

请输入一个整数(输入-1代表结束):7

1 4 5 5 7 9

请输入一个整数(输入-1代表结束):6

1 4 5 5 6 7 9

请输入一个整数(输入-1代表结束):2

1 2 4 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 2 4 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 2 4 5 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):-1

开始输入要删除的整数:

请输入一个整数(输入-1代表结束):2

1 4 5 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):4

1 5 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 6 7 9

请输入一个整数(输入-1代表结束):1

6 7 9

请输入一个整数(输入-1代表结束):6

7 9

请输入一个整数(输入-1代表结束):7

9

请输入一个整数(输入-1代表结束):9

请输入一个整数(输入-1代表结束):-1

Process returned 0 (0x0) execution time : 43.842 s

Press any key to continue.

// 下面是融合了多中基本操作的源码:

#include

#include

最后

给大家分享一份移动架构大纲,包含了移动架构师需要掌握的所有的技术体系,大家可以对比一下自己不足或者欠缺的地方有方向的去学习提升;

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

1 5 9

请输入一个整数(输入-1代表结束):4

1 4 5 9

请输入一个整数(输入-1代表结束):5

1 4 5 5 9

请输入一个整数(输入-1代表结束):7

1 4 5 5 7 9

请输入一个整数(输入-1代表结束):6

1 4 5 5 6 7 9

请输入一个整数(输入-1代表结束):2

1 2 4 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 2 4 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 2 4 5 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):-1

开始输入要删除的整数:

请输入一个整数(输入-1代表结束):2

1 4 5 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):4

1 5 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 5 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 5 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 5 6 7 9

请输入一个整数(输入-1代表结束):5

1 6 7 9

请输入一个整数(输入-1代表结束):1

6 7 9

请输入一个整数(输入-1代表结束):6

7 9

请输入一个整数(输入-1代表结束):7

9

请输入一个整数(输入-1代表结束):9

请输入一个整数(输入-1代表结束):-1

Process returned 0 (0x0) execution time : 43.842 s

Press any key to continue.

// 下面是融合了多中基本操作的源码:

#include

#include

最后

给大家分享一份移动架构大纲,包含了移动架构师需要掌握的所有的技术体系,大家可以对比一下自己不足或者欠缺的地方有方向的去学习提升;

[外链图片转存中…(img-N3h3ACfI-1715421531693)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值