指定位置插入: 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;
}
方式二:准备一个新链表,取出之前链表中的元素,用头插法的方式插入新准备的链表中,这里不做演示