单向链表的创建、遍历及新节点的插入(头部、尾部、任意位置)

新建单向链表、遍历链表及新节点插入(头部、尾部、任意位置)、删除某节点

一、单向链表属于叫复杂的数据结构,在C语言编程中应用广泛。本篇文章记录了以下内容:

1. 新建单向链表

2. 链表的遍历

3. 从尾部插入新节点

4. 从任意位置插入新节点

5. 从头部之前插入新节点

6. 删除某指定位置节点

参考博文链接:https://www.cnblogs.com/passedbylove/p/11439213.html

二、代码及运行结果如下:


// 单向链表插入节点: 3种情形

// 1.从链表头部插入节点;
// 2.从链表中间插入节点;
// 3.从链表尾部插入节点;

#include <stdio.h>
#include <stdlib.h>  //该库函数包含malloc(),free()

struct Node *InitList(struct Node *head, int Data);
struct Node *AppendNode(struct Node *head, int new_node_data);
struct Node *PushNode(struct Node *head, int new_node_data);
struct Node *DeltNode(struct Node *head, int new_node_data);
struct Node *InsertNode(struct Node *head, int insert_num, int new_node_data);
void DisplayListNode(struct Node *head);


// 定义节点的结构体
struct Node
{
	int data;
	struct Node *next;
};


// main函数 - 链表主体
int main(void)
{
	//定义链表头指针
	struct Node *head = NULL; 
	int head_data = 100;
	int new_node_data = 200;
	
	//初始化链表头节点
	head = InitList(head,head_data);	//第0个节点 - 头节点
	
	//显示链表各个节点
	printf("\n***1-新建链表并初始化头节点信息******\n");
	DisplayListNode(head);
	
	//操作1:在链表尾部插入新节点	
	head = AppendNode(head,new_node_data); //第1个节点
	head = AppendNode(head,300); //第2个节点
	head = AppendNode(head,400); //第3个节点
	
	//显示链表各个节点
	printf("\n***2-尾部插入节点后,打印链表节点信息******\n");
	DisplayListNode(head);

	
	//操作2:在第2、3个节点之间插入一个新节点
	int insert_num = 2;
	new_node_data = 500;
	head = InsertNode(head,insert_num,new_node_data); //
	
	//显示链表各个节点
	printf("\n***3-链表指定位置插入节点后,打印链表节点信息******\n");
	DisplayListNode(head);

	
	//操作3:在链表头节点前插入新节点
	new_node_data = 0;
	head = PushNode(head,new_node_data);
	
	//显示链表各个节点
	printf("\n***4-链表头部插入新节点后,打印链表节点信息******\n");
	DisplayListNode(head);
	
	
	//操作4:删除某指定位置节点
	int delet_num = 3;
	head = DeltNode(head, delet_num);
	
	//显示链表各个节点
	printf("\n***5-链表删除第3个节点后,打印链表节点信息******\n");
	DisplayListNode(head);
	
	
	return 0;
}

//函数功能:新建并初始化链表头节点
//函数返回值:链表头节点指针
struct Node *InitList(struct Node *head, int Data)
{
	//定义一个空结构体指针
	struct Node *p = NULL;
	
	//申请分配堆内存地址
	p = (struct Node *)malloc(sizeof(struct Node));
	
	//判断内存空间是否充足
	if(NULL == p)
	{
		printf("内存空间不足\n");
		exit(0); //传入的参数0为函数退出时的状态码
	}
	
	//为头节点指针绑定内存地址
	head = p;
	head -> data = Data;
	head -> next = NULL;
	
	return head;
}


//函数功能:依次打印链表各个节点信息
//函数返回值: void
void DisplayListNode(struct Node *head)
{
	int i = 0;
	
	//初始化一个结构体指针
	struct Node *p = head;
	
	if(NULL == p)
	{
		printf("该链表为空\n");
		exit(0);
	}
	
	while(NULL != p)
	{
		printf("	第%d个节点, 该节点值为 %d\n", i, p->data);
		
	//	printf("	该节点地址信息为 %d\n",p);
		
		i += 1;
		
		p = p->next;
	}
	
}


//函数功能:链表尾部插入节点
//返回值:链表头节点指针
struct Node *AppendNode(struct Node *head, int Data)
{
	struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
	struct Node *last = head;
	
	//初始化新的尾节点
	new_node ->data = Data;
	new_node ->next =NULL;
	
	//判断当前链表是否为空
	if(NULL == head) //链表为空
	{
		head = new_node;
		return head;
	}
	
	//将头指针移动到链表尾部
	while(NULL != (last ->next))
	{
		last = last->next;
	}
	
	
	//将新节点设置为尾节点
	last ->next = new_node;
	
	return head;
}


//函数功能:在链表指定位置插入新节点
//返回值:链表头节点指针
struct Node *InsertNode(struct Node *head,int insert_num,int data)
{
	int i = 0;
	int j = 0;
	
	struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
	struct Node *prev_node = head;
	struct Node *next_node = NULL;
	
	//初始化新插入节点
	new_node ->data = data;
//	new_node ->next = next_node;
	
	//查询该链表是否存在该指定位置
	while(NULL != (prev_node ->next))
	{
		prev_node = prev_node ->next;
		i += 1;
	}	
	
	if(i < insert_num)
	{
		printf("\n***该链表节点数不足,无法在指定位置插入新节点***\n");
		exit(0);
	}	
	
	//指定位置插入新节点
	prev_node = head;
	while(NULL != (prev_node ->next))
	{
	//	next_node = prev_node ->next;
	//	prev_node = prev_node;
		j += 1;
		
		if(j == insert_num)
		{
			new_node ->next = prev_node ->next;
			prev_node ->next = new_node;
		//	printf("j = %d.\n", j);
			break;
		}
		
		prev_node = prev_node ->next;
	//	printf("j = %d.\n", j);
		
		
	}
	
	return head;
}


//函数功能:在链表头部插入新节点
//返回值:链表头指针
struct Node *PushNode(struct Node *head, int data)
{
	struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
	
	//判断内存空间是否充足
	if(NULL == new_node)
	{
		printf("内存空间不足\n");
		exit(0); //传入的参数0为函数退出时的状态码
	}
	
	//将该新节点设置为链表头节点
	new_node -> data = data;
	new_node -> next = head;
	
	//更新链表头节点指针
	head = new_node;
	return head;
	
}


//函数功能:删除链表某指定节点
//返回值:链表头节点指针
struct Node *DeltNode(struct Node *head,int delt_num)
{
	int node_num = 0;
	struct Node *p = (struct Node *)malloc(sizeof(struct Node));
	struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
	
	p = head;
	
	//当前链表是否包含该指定节点
	while(NULL != (p ->next))
	{
		node_num += 1;
		p = p ->next;
	}

	if((node_num-1) < delt_num)
	{
		printf("\n***该链表未包含所指定位置节点***\n");
		exit(0);
	}
	
	//删除指定位置节点
	p = head;
	node_num = 0;
	
	while(NULL != (p ->next))
	{
		if(node_num == (delt_num-1))
		{
			temp = p ->next;
			p ->next = temp ->next;
			break;
		}
		
		node_num += 1;
		p = p ->next;
	}
	
	return head;
}





 

三、运行结果如下:

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值