C语言-链表-笔记1(链表结点的定义和插入)

一、链表结点的定义

node.h

#ifndef _node_h_
#define _node_h_
typedef struct _node{	//用typedef是为了后面不用再很麻烦地用struct _node这么长的去定义变量 
	int value;
	struct _node *next;// 这里不能用 Node *next;因为程序在读到这一行时下面那个被定义的Node还没有出现 
}Node; 

#endif

二、结点的插入
1.插入结点构造链表(尾插法) linked-list.c

#include<stdio.h>
#include"node.h"
#include<stdlib.h>
//typedef struct _node{
//	int value;
//	struct _node *next;
//}Node;

int main()
{
	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; 
}

2.将插入节点的操作
//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;
}
变成一个函数,放在main()之外
linked-list.c

#include<stdio.h>
#include"node.h"
#include<stdlib.h>
//typedef struct _node{
//	int value;
//	struct _node *next;
//}Node;

// 插入节点(尾插法)
Node* add(Node* head,int number); 
int main()
{
	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)
{
	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;
	}
}

因为传入add( )函数的参数head 在add()中有修改操作 head=p; ,在传入的head为空的条件下需要对Head指针进行修改,但add()函数只是对add()函数中的head进行了修改,并不会修改main()中head的值,故需要进行改进:

方法1,创建一个全局变量 Node *head;
缺点:在编写大程序时,很难保证只有一个链表,这就会导致和别的链表的head冲突和混乱,或者说,往往一个程序需要很多人来共同协作,这种情况下就要尽量避免全局变量的使用,因为这样会造成不必要的冲突和麻烦,这样的代码的质量是低下的,所以在初学代码时一定要兼顾代码的结构。

方法2,因为在add()中修改了head,那么不如让add()函数返回这个head,同时在main()中用head来接收这个返回值。

linked-list.c

#include<stdio.h>
#include"node.h"
#include<stdlib.h>
//typedef struct _node{
//	int value;
//	struct _node *next;
//}Node;

// 插入节点(尾插法)
Node* add(Node* head,int number); 
int main()
{
	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)
{
	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;
}

小缺点:要求使用add函数的程序员必须小心的在main()中用head=add(head,number)来接收返回值,如果直接使用add(head,number)就会错误。

方法3,将*head的地址传进add函数,即void add(Node** head,int number). 即指向指针的指针。
linked-list.c

#include<stdio.h>
#include"node.h"
#include<stdlib.h>
//typedef struct _node{
//	int value;
//	struct _node *next;
//}Node;

// 插入节点(尾插法)
void add(Node* head,int number); 
int main()
{
	Node *head=NULL;
	int number;
	do{										
		scanf("%d",&number);
		if(number!=-1){
			add(&head,number);
		}
	}while(number!=-1);
	return 0; 
}
void add(Node** pHead,int number)
{
	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;
	}
}

这里要注意
这里传入的参数类型为指针的指针,稍微有些绕,要时刻清楚 pHead,*pHead,**pHead的含义,(传入的头指针head的地址,头指针head,头指针head所指向的值)

方法4,自己创建一种数据结构
tpyedef struct _list{
Noed* head;
} List;

linked-list.c

#include<stdio.h>
#include"node.h"
#include<stdlib.h>
//typedef struct _node{
//	int value;
//	struct _node *next;
//}Node;

tpyedef struct _list{
	Node* head;
} List;
void add(Node* head,int number); 
int main()
{
	List list;
	list.head=NULL;
	int number;
	do{										
		scanf("%d",&number);
		if(number!=-1){
			add(&list,number);
		}
	}while(number!=-1);
	return 0; 
}
void add(List* pList,int number)
{
	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;
	}
}

优点:我们用了自己创建的数据结构来代表整个链表,为以后程序功能的扩充带来了空间,虽然现在在这个数据结构中只放了一个 头指针head,但是以后我们可以放更多的东西,以便于进行更多的操作,比如可以放入尾指针*tail…

方法5,基于方法4,可以在自己创建的数据结构里添加一个尾指针tail,可以更方便…(待续)

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值