写点什么

链表指定位置插入,指定位置删除,逆置链表的练习

用户头像
大忽悠
关注
发布于: 2021 年 02 月 23 日
链表指定位置插入,指定位置删除,逆置链表的练习

指定位置插入:

insert_pos_val(lk headNode, int pos, int val);


#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>typedef struct LinkNode{	int num;	LinkNode* next;}Lk, * lk;//初始化头节点lk init_headNode(){	lk headNode = (lk)malloc(sizeof(Lk));	if (headNode == NULL)	{		return NULL;	}	headNode->next = NULL;	//指针遍历输出	lk endNode = headNode;	//输入-1结束向链表赋值	int val = -1;	while (1)	{		printf("请输入:\n");		scanf("%d", &val);		if (val == -1)		{			break;		}		lk newNode = (lk)malloc(sizeof(Lk));		newNode->num = val;		newNode->next = NULL;		//尾插法		endNode->next = newNode;		endNode = newNode;	}	return headNode;}//遍历输出void foreach_linkList(lk headNode){	if (headNode == NULL)	{		return;	}	lk curNode = headNode->next;	while (curNode)	{		printf("%d\n", curNode->num);		curNode = curNode->next;	}}//指定位置插入一个值void insert_pos_val(lk headNode, int pos, int val){	if (headNode == NULL)	{		return;	}	lk prveNode = headNode;	lk curNode = headNode->next;	//索引	int index = 0;	while (curNode)	{		if (index == pos)		{			break;		}		prveNode = curNode;		curNode = curNode->next;		index++;	}	if (pos > index)	{		printf("无法插入当前位置\n");		return;	}	lk newNode = (lk)malloc(sizeof(Lk));	newNode->num = val;	newNode->next = NULL;	prveNode->next = newNode;	newNode->next = curNode;}int main(){	lk headNode = init_headNode();	printf("打印输出链表:\n");	foreach_linkList(headNode);	printf("插入后打印输出链表:\n");	insert_pos_val(headNode, 10, 521);	foreach_linkList(headNode);	return 0;}
复制代码


指定位置删除:


#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>typedef struct LinkNode{	int num;	LinkNode* next;}Lk, * lk;//初始化头节点lk init_headNode(){	lk headNode = (lk)malloc(sizeof(Lk));	if (headNode == NULL)	{		return NULL;	}	headNode->next = NULL;	//指针遍历输出	lk endNode = headNode;	//输入-1结束向链表赋值	int val = -1;	while (1)	{		printf("请输入:\n");		scanf("%d", &val);		if (val == -1)		{			break;		}		lk newNode = (lk)malloc(sizeof(Lk));		newNode->num = val;		newNode->next = NULL;		//尾插法		endNode->next = newNode;		endNode = newNode;	}	return headNode;}//遍历输出void foreach_linkList(lk headNode){	if (headNode == NULL)	{		return;	}	lk curNode = headNode->next;	while (curNode)	{		printf("%d\n", curNode->num);		curNode = curNode->next;	}}//指定位置删除void delete_pos_val(lk headNode, int pos, int val){	if (headNode == NULL)	{		return;	}	lk prveNode = headNode;	lk curNode = headNode->next;	//索引:跟据curNode位置移动	int index = 1;	while (curNode)	{		if (index == pos)		{			break;		}		prveNode = curNode;		curNode = curNode->next;		index++;	}	if (pos > index)	{		printf("没有当前元素可供删除\n");		return;	}	//改变prveNode指针指向	prveNode->next = curNode->next;	//释放curNode节点在堆区开辟的数据	free(curNode);}int main(){	lk headNode = init_headNode();	printf("打印输出链表:\n");	foreach_linkList(headNode);	printf("删除后打印输出链表:\n");	delete_pos_val(headNode, 2, 521);	foreach_linkList(headNode);	return 0;}
复制代码


逆置链表方式:只对当前链表进行操作,不借用新链表




#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>typedef struct LinkNode{	int num;	LinkNode* next;}Lk, * lk;//初始化头节点lk init_headNode(){	lk headNode = (lk)malloc(sizeof(Lk));	if (headNode == NULL)	{		return NULL;	}	headNode->next = NULL;	//指针遍历输出	lk endNode = headNode;	//输入-1结束向链表赋值	int val = -1;	while (1)	{		printf("请输入:\n");		scanf("%d", &val);		if (val == -1)		{			break;		}		lk newNode = (lk)malloc(sizeof(Lk));		newNode->num = val;		newNode->next = NULL;		//尾插法		endNode->next = newNode;		endNode = newNode;	}	return headNode;}//遍历输出void foreach_linkList(lk headNode){	if (headNode == NULL)	{		return;	}	lk curNode = headNode->next;	while (curNode)	{		printf("%d\n", curNode->num);		curNode = curNode->next;	}}//逆置链表void reverse_pos_val(lk headNode){	if (headNode == NULL)	{		return;	}	lk prveNode = headNode;	lk curNode = prveNode->next;	lk nextNode = curNode->next;	//第一步:	prveNode->next = NULL;	curNode->next = NULL;	//第二步到第五步	while (nextNode != NULL)	{		//辅助指针往后移动		prveNode = curNode;		curNode = nextNode;		nextNode = nextNode->next;		//移动过后改变链表中curNode节点next指针指向		curNode->next = prveNode;	}   //第六步	headNode->next = curNode;	}int main(){	lk headNode = init_headNode();	printf("打印输出链表:\n");	foreach_linkList(headNode);	printf("逆置后打印输出链表:\n");	reverse_pos_val(headNode);	foreach_linkList(headNode);	return 0;}
复制代码


方式二:准备一个新链表,取出之前链表中的元素,用头插法的方式插入新准备的链表中,这里不做演示


发布于: 2021 年 02 月 23 日阅读数: 10
用户头像

大忽悠

关注

还未添加个人签名 2021.02.23 加入

还未添加个人简介

评论

发布
暂无评论
链表指定位置插入,指定位置删除,逆置链表的练习