【单向链表实现增删改查的操作】更多资料请点击:我的目录
本篇仅用于记录自己所学知识及应用,代码仍可优化,仅供参考,如果发现有错误的地方,尽管留言于我,谢谢!
此篇是单向链表实现增删改查的操作,其中:
"增"包括在链尾、链中增加节点
"删"包括在链头、链中、链尾删除单个或多个节点
"改"包括在链头、链中、链尾更改单个或多个节点数值
"查"只是显示整个链表所有节点数值
#include
#include //设计节点
struct node
{
int data;
struct node *next;
};
//链表初始化
struct node *list_init()
{
struct node *head = malloc(sizeof (struct node));
head->next = NULL;
return head;
}//创建新节点
struct node *newnode(int data)
{
struct node *new = malloc(sizeof(struct node));
new->data = https://www.it610.com/article/data;
new->next = NULL;
}//链尾加入节点
int addtail(int newdata , struct node *list)
{
struct node *p = list;
while(p->next != NULL)
{
p = p->next;
}
struct node *new = newnode(newdata);
p->next = new;
}//链中插入节点
int insert(int olddata,int newdata,struct node *list)
{
int b = 0;
//判断标志
struct node *p = list;
while(p->next != NULL)
{
if(p->data =https://www.it610.com/article/= olddata)
{
struct node *new = newnode(newdata);
new->next = p->next;
p->next = new;
b = 1;
//当新旧节点数据相等时,跳过新节点,防止进入无限循环
if(olddata =https://www.it610.com/article/= newdata)
{
p = p->next;
}
}
p = p->next;
}
//链尾加入节点
if(p->next == NULL && p->data =https://www.it610.com/article/= olddata)
{
struct node *new = newnode(newdata);
p->next = new;
b = 1;
}
printf("\n=====在链表%d后加入节点%d=====\n",olddata,newdata);
if(b == 0)
printf("原链表节点没有%d这个数值!\n",olddata);
}//删除节点
int delnode(int deldata,struct node *list)
{
int b = 0, m = 0;
//判断标志
struct node *q = list;
struct node *p = list->next;
printf("\n=====删除节点%d=====\n",deldata);
while(p != NULL)
{
if(p->data =https://www.it610.com/article/= deldata)
{
q->next = p->next;
p->next = NULL;
free(p);
b = 1;
m = 1;
}if(m == 1)
{
p = q->next;
m = 0;
}else
{
p = p->next;
q = q->next;
}
}
if(b == 0)
printf("原链表节点没有%d这个数值!\n",deldata);
}//更改节点数值
int change(int olddata, int newdata, struct node *list)
{
int b = 0;
struct node *p = list->next;
while(p != NULL)
{
if(p->data =https://www.it610.com/article/= olddata)
{
p->data = https://www.it610.com/article/newdata;
b = 1;
}
p = p->next;
}
printf("\n=====更改节点%d为%d=====\n",olddata,newdata);
if(b == 0)
printf("原链表节点没有%d这个数值!\n",olddata);
}//显示所有节点
int show(struct node *list)
{
printf("链表各节点:\t");
struct node *p = list->next;
{
while(p != NULL)
{
printf("%d\t",p->data);
p= p->next;
}
printf("\n");
}
}int main(int argc, char **argv)
{
struct node *list = list_init();
addtail(350,list);
addtail(100,list);
addtail(200,list);
addtail(200,list);
addtail(250,list);
addtail(800,list);
addtail(200,list);
addtail(100,list);
show(list);
insert(100,150,list);
show(list);
delnode(200,list);
show(list);
delnode(150,list);
show(list);
delnode(350,list);
show(list);
change(250,500,list);
show(list);
insert(700,150,list);
show(list);
delnode(700,list);
show(list);
change(700,150,list);
show(list);
return 0;
}
运行结果:
文章图片
推荐阅读
- 笔记|C语言数据结构——二叉树的顺序存储和二叉树的遍历
- C语言学习(bit)|16.C语言进阶——深度剖析数据在内存中的存储
- 数据结构和算法|LeetCode 的正确使用方式
- 先序遍历 中序遍历 后序遍历 层序遍历
- 数据结构|C++技巧(用class类实现链表)
- 数据结构|贪吃蛇代码--c语言版 visual c++6.0打开
- 算法|算法-二分查找
- 数据结构学习指导|数据结构初阶(线性表)
- leetcode题解|leetcode#106. 从中序与后序遍历序列构造二叉树
- java|ObjectOrientedProgramming - 面向对象的编程(多态、抽象类、接口)- Java - 细节狂魔