详解使用函数创建链表

116 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第14天,点击查看活动详情

1、两种创建有头节点的函数

由头节点的链表,对于每一个节点都有前驱节点,首节点也是,他的前驱节点就是头节点。

1、以0结束输入

list Creat4()//有头节点
{
	int i = 1;//因为要以0结束输入,所以每次给节点赋值并能直接scanf给p->data,而是要先输入给一个中间变量,在进行判断它是否为0,不为零在赋值给data,否则就停止输入了。
	list L, p, r;
    //L:链表的名字,也是返回的对象
    //P:始终是链表上最后一个节点,所以随着链表的增长,p也在一点一点的移动 p=p->next       //r:我们给r的数据域赋值,让后将他挂在链表的尾端
	L = (struct Node*)malloc(sizeof(struct Node));//为其动态开辟内存空间
	L->next = NULL;
	p = L;
	scanf_s("%d", &i);
	while (i != 0)
	{
		r = (struct Node*)malloc(sizeof(struct Node));
		r->data = i;//赋值
		r->next = NULL;//指针域为空
		p->next = r;//将r挂在链表上
		p=r;//p后移
		scanf_s("%d", &i);
	}
	if (!p->next)//所以p->next为空,原因是 p一直都是是r复制来的,r->next=NULL; 
	{
		printf("dfghjkfghjkcvbnmcvbn\n");
	}
	return L;
}

2、已知长度时

list Creat3(int n)//有头节点
{
	int i = 0;
	list L, p, r;
	L = (struct Node*)malloc(sizeof(struct Node));
	L->next = NULL;
	p = L;
	while (i++<n)//到达特定长度撤出循环
	{
		r = (struct Node*)malloc(sizeof(struct Node));
		r->next = NULL;//指针域为空
		scanf_s("%d", &r->data);//可以直接对数据域赋值
		p->next = r;//将r挂在链表上
		p = r;//p向后移动
	}
	p->next = NULL;
	return L;
}

2、两种函数创建无头结点

无头节点的链表首节点没有前驱节点,所以有时候要对首元节点进行特殊处理。

1、以0结束输入

list Creat2()
{
	int i = 0;
	list L, p, r;
	L = (struct Node*)malloc(sizeof(struct Node));
	L->next = NULL;
	p = L;
	scanf_s("%d", &i);//使用中介变量为节点数据域赋值
	int s = 0;
	while (i!=0)
	{
		r = (struct Node*)malloc(sizeof(struct Node));
		r->next = NULL;
		if (s == 0) {//对首元节点进行特殊处理
			p->data = i;
			s++;
		}
		else{
			r->data = i;//赋值
			p->next = r;//挂r
			p = r;//p后移
		}
		scanf_s("%d", &i);
	}
	return L;
}

2、已知长度时

list Creat(int n)
{
	int i = 1;
	list L, p, r;
	L = (struct Node*)malloc(sizeof(struct Node));
	L->next = NULL;
	p = L;
	scanf_s("%d", &p->data);//对首元节点进行特殊处理
	while (i++ < n)
	{
		r = (struct Node*)malloc(sizeof(struct Node));
		r->next = NULL;
		p->next = r;
		p = r;
		scanf_s("%d", &r->data);
	}
	return L;
}

3、完整代码及测试

#include <stdio.h>
#include <stdlib.h>
typedef int DtatType;
typedef struct Node
{
	DtatType data;
	struct Node * next;
}Node,*list;
list Creat(int n)
{
	int i = 1;
	list L, p, r;
	L = (struct Node*)malloc(sizeof(struct Node));
	L->next = NULL;
	p = L;
	scanf_s("%d", &p->data);
	while (i++ < n)
	{
		r = (struct Node*)malloc(sizeof(struct Node));
		r->next = NULL;
		p->next = r;
		p = r;
		scanf_s("%d", &r->data);
	}
	return L;
}
list Creat2()
{
	int i = 0;
	list L, p, r;
	L = (struct Node*)malloc(sizeof(struct Node));
	L->next = NULL;
	p = L;
	scanf_s("%d", &i);
	int s = 0;
	while (i!=0)
	{
		r = (struct Node*)malloc(sizeof(struct Node));
		r->next = NULL;
		if (s == 0) {
			p->data = i;
			s++;
		}
		else{
			r->data = i;
			p->next = r;
			p = r;
		}
		scanf_s("%d", &i);
	}
	return L;
}
list Creat3(int n)//有头节点
{
	int i = 0;
	list L, p, r;
	L = (struct Node*)malloc(sizeof(struct Node));
	L->next = NULL;
	p = L;
	while (i++<n)
	{
		r = (struct Node*)malloc(sizeof(struct Node));
		r->next = NULL;
		scanf_s("%d", &r->data);
		p->next = r;
		p = r;
	}
	p->next = NULL;
	return L;
}
list search(list L,int n)
{
	list h = L, p;
	p = (struct Node*)malloc(sizeof(struct Node));
	int i = 0;
	while (i++ < n)
	{
		h = h->next;//这是往下查询的节奏
					//h->next=h这个是往下增加的节奏
	}
	return h;
}
void Print(list L)//无头结点输出函数
{
	list h = L;
	while (h != NULL)
	{
		printf("%d ", h->data);
		h = h->next;//往下传的时候是 h=h->next
	}
}
void Print2(list L)有头节点输出函数
{
	list h = L->next;
	while (h != NULL)
	{
		printf("%d ", h->data);
		h = h->next;//往下传的时候是 h=h->next
	}printf("\n");
}

int main()
{

}