C语言实现链表结构(二)链表的优化
2019-04-24 12:19
113 查看
版权声明:本文为转载文章,转载请务必注明出处和作者,谢谢合作! https://blog.csdn.net/zhanshen112/article/details/80721827
上一篇文章,已经初步会构造“链表”这种数据数据结构,本文主要讲一下构造的链表如何进行优化。
链表代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include “node.h”
- /* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
- int main(int argc, char *argv[]) {
- Node *head=NULL;
- int number;
- do{
- scanf("%d",number);
- if(number!=-1){
- //add to linked-list
- Node p=(Node)malloc(sizeof(Node));
- p->value=number;
- p->next=NULL;
- //Find the last
- Node last=head;
- if(last){
- while(last->next){
- last=last->next;
- }
- //attach
- last->next=p;
- } else{
- head=p;
- }
- }
- }while(number!=-1);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include “node.h”
- / run this program using the console pauser or add your own getch, system(“pause”) or input loop */
- void add(Node *head,int number);
- int main(int argc, char *argv[]) {
- Node *head=NULL;
- int number;
- do{
- scanf("%d",number);
- if(number!=-1){
- add(head,number);
- }while(number!=-1);
- return 0;
- }
- }
- void add(Node *head,int number)
- {
- //add to linked-list
- Node p=(Node)malloc(sizeof(Node));
- p->value=number;
- p->next=NULL;
- //Find the last
- Node last=head;
- if(last){
- while(last->next){
- last=last->next;
- }
- //attach
- last->next=p;
- } else{
- head=p;
- }
- }
这里,有些教科书会说到用全局变量head,将head定义在主函数外面。有两个原因导致我们不愿意这样使用:1、要尽可能避免使用全局变量,全局变量是有害的,很可能在程序的某个地方就会修改全局变量;2、全局变量head我们现在设想的是只在这个链表中使用,如果存在多个链表,那多个链表的head就无法使用这个全局变量。
另外一个方法是可以在add函数里面将head作为返回值,然后将add函数定义为一个结点型指针(Node add(Node head,int number)),并将函数返回值赋给head。
程序修改如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include “node.h”
- / run this program using the console pauser or add your own getch, system(“pause”) or input loop */
- void add(Node *head,int number);
- int main(int argc, char *argv[]) {
- Node head=NULL;
- int number;
- do{
- scanf("%d",number);
- if(number!=-1){
- head=add(head,number);
- }while(number!=-1);
- return 0;
- }
- }
- Node add(Node *head,int number)
- {
- //add to linked-list
- Node p=(Node)malloc(sizeof(Node));
- p->value=number;
- p->next=NULL;
- //Find the last
- Node last=head;
- if(last){
- while(last->next){
- last=last->next;
- }
- //attach
- last->next=p;
- } else{
- head=p;
- }
- return head;
- }
head=add(head,number);程序员使用add函数只是在链表上增加一个结点而已,实在是很难想到竟然通过add函数的返回值去修改head。因此,站在接口设计的角度,依然是存在问题。如果程序员忘了这个add赋给head的操作,那么对于空链表的add操作就是错的。
第三种方案:在add函数的输入参数中不再传入head结点,而是传入头结点的指针。即add(&head,number)。程序的其他部分也需要进行修改。
- #include <stdio.h>
- #include <stdlib.h>
- #include “node.h”
- / run this program using the console pauser or add your own getch, system(“pause”) or input loop */
- void add(Node *head,int number);
- int main(int argc, char *argv[]) {
- Node head=NULL;
- int number;
- do{
- scanf("%d",number);
- if(number!=-1){
- add(&head,number);
- }while(number!=-1);
- return 0;
- }
- }
- Node add(Node **pHead,int number)
- {
- //add to linked-list
- Node p=(Node)malloc(sizeof(Node));
- p->value=number;
- p->next=NULL;
- //Find the last
- Node *last=*pHead;
- if(last){
- while(last->next){
- last=last->next;
- }
- //attach
- last->next=p;
- } else{
- pHead=p;
- }
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include “node.h”
- / run this program using the console pauser or add your own getch, system(“pause”) or input loop /
- typedef struct _list{
- Node head;
- }List;
- void add(Node *head,int number);
- int main(int argc, char *argv[]) {
- int number;
- List list;
- list.head=NULL;
- do{
- scanf("%d",number);
- if(number!=-1){
- add(&list,number);
- }while(number!=-1);
- return 0;
- }
- }
- void add(List *pList,int number)
- {
- //add to linked-list
- Node p=(Node)malloc(sizeof(Node));
- p->value=number;
- p->next=NULL;
- //Find the last
- Node *last=pList->head;
- if(last){
- while(last->next){
- last=last->next;
- }
- //attach
- last->next=p;
- } else{
- pList->head=p;
- }
- }
相关文章推荐
- 数据结构基础(3)---C语言实现单链表
- C语言实现结构链表的选择排序
- 数据结构基础(3)---C语言实现单链表
- C语言 数据结构之链表实现代码
- c语言实现通用数据结构(一):通用链表
- 数据结构——链表实现(c语言)
- 数据结构基础(3)---C语言实现单链表
- C语言 双向循环链表的实现和结构 链表 线性表的链式实现
- C语言实现线性结构创建动态链表的学习心得(一)
- 数据结构:链表(C语言实现)
- [C语言]链表实现贪吃蛇及部分模块优化
- C语言实现贪吃蛇(三)----结构+链表实现
- 数据结构之链表结构——C语言实现
- 链表结构_C语言实现
- 数据结构.单链表(C语言实现)
- 数据结构 学习笔记之:静态链表--史上最简单的C语言实现——只为掌握概念——不清楚静态链表的鸟鸟们有福了!
- 数据结构之---C语言实现广义表头尾链表存储表示
- c语言实现单链表数据结构及其基本操作
- C语言实现动态链表结构
- 数据结构(Data structure):用链表实现多项式的表示和运算(C语言)