链表的回顾与总结(一)正序、逆序、有序、插入、修改、删除

news2024/10/10 19:24:54
  1. 逆序链表
    在这里插入图片描述
#include<iostream>
using namespace std;
struct node
{
	int data;
	node* next;
};//最好不要全局定义指针,很麻烦
void show(node* head)
{
	while (head)
	{
		cout << head->data << ' ';
		head = head->next;
	}
}
int main()
{
	int i, j, k, ch;
	node* head = NULL;
	node* p, * s;
	cin >> ch;//这个相比cin>>p->num更方便
	while (ch != 0)
	{
		p = new node;
		p->data = ch;
		p->next = head;
		head = p;
		cin >> ch;//当最后一位是0的时候连新的结点都不需要建立
	}
	show(head);
	return 0;
}

升级版:

#include<iostream>
using namespace std;
struct node
{
	int data;
	node* next;
};
void show(node*head)
{
	while (head)
	{
		cout << head->data << ' ';
		head = head->next;
	}
}
int main()
{
	int n, i, j, k;
	node* head = NULL;
	node* q=NULL, * p;;
	cin >> n;
	while (n--)
	{
		cin >> k;
		p = new node;
		p->data = k;
		p->next = head;
		head = p;
	}
	show(head);
	return 0;
}
  1. 正序链表
    在这里插入图片描述
方法一:(逆序建立链表,再逆序输出链表)
struct node
{
	int data;
	node* next;
};
void show(node* head)//指针传参新建一个匿名对象
{
	if (head == NULL)
	{
		return;//代码块的结束
	}
	else
	{
		show(head->next);//递归:入栈出栈的方式
		cout << head->data << ' ';
	}
}
int main()
{
	int i, j, k, ch;
	node* head = NULL;
	node* p, * s;
	cin >> ch;
	while (ch != 0)
	{
		p = new node;
		p->data = ch;
		p->next = head;
		head = p;
		cin >> ch;
	}
	show(head);
	return 0;
}
方法二:(正序建立链表,正序输出)
int main()
{
	int n, i, j, k;
	node* head = NULL;
	node* q = NULL;//把q初始化为空指针的原因是VS会显示“使用了可能未初始化的本地指针变量”
	node* p;
	cin >> k;
	while (k!=0)
	{
		p = new node;
		p->data = k;
		p->next = NULL;
		if (head == NULL)
			head = p;
		else q->next = p;
		q = p;
		cin >> k;
	}
	show(head);
	return 0;
}

升级版:

while(head)
{
cout<<head->data<<' ';
head=head->next;
}
int main()
{
int n,i,j,k;
cin>>n;
node*head=NULL;
node*q=NULL;//把q初始化为空指针的原因是VS会显示“使用了可能未初始化的本地指针变量”
node *p;
while(n--)
{
cin>>k;
p=new node;
p->data=k;
p->next=NULL;
if(head==NULL)
head=p;
else q->next=p;
q=p;
}
show(head);
return 0;
}
  1. 有序链表,输入一串数。
#include<iostream>
using namespace std;
struct node
{
	int data;
	node* next;
};
void insert(node*& head, int k)
{
	node* s, * p, * q;
	s = new node;
	s->data = k;
	s->next = NULL;
	if (head == NULL)
	{
		head = s;
		return;//头是空的
	}
	if (head->data > s->data)
	{
		s->next = head;
		head = s;
		return;
	}
	for (q = head, p = head->next; p; q = p, p = p->next)
	{
		if (p->data > s->data)
		{
			s->next = p;
			q->next = s;
			return;
		}
	}
	q->next = s;
	return;
}

void show(node*head)
{
	while (head)
	{
		cout << head->data << ' ';
		head = head->next;
	}
}
int main()
{
	int n, i, j, k;
	node* head = NULL;
	cin >> k;
	while (k!=0)
	{
		insert(head, k);
		cin >> k;
	}
	show(head);
	return 0;
}
  1. 建立有序链表,为该有序链表中插入值,插值结束后链表仍然有序。
#include<iostream>
using namespace std;
struct node
{
	int data;
	node* next;
};
void insert(node*& head, int k)
{
	node* s, * p, * q;
	s = new node;
	s->data = k;
	s->next = NULL;
	if (head == NULL)
	{
		head = s;
		return;//头是空的
	}
	if (head->data > s->data)
	{
		s->next = head;
		head = s;
		return;
	}
	for (q = head, p = head->next; p; q = p, p = p->next)
	{
		if (p->data > s->data)
		{
			s->next = p;
			q->next = s;
			return;
		}
	}
	q->next = s;
	return;
}
void show(node*head)
{
	while (head)
	{
		cout << head->data << ' ';
		head = head->next;
	}
}
int main()
{
	int n, i, j, k;
	node* head = NULL;
	node* q = NULL;//把q初始化为空指针的原因是VS会显示“使用了可能未初始化的本地指针变量”
	node* p;
	cin >> n;
	while (n--)
	{
		cin >> k;
		insert(head, k);
	}
	cin >> j;
	insert(head, j);
	show(head);
	return 0;
}
  1. 节点修改
#include<iostream>
using namespace std;
struct node
{
	int num;
	string name;
	int age;
	node* next;
};
void show(node*head)
{
	while (head)
	{
		cout << head->num << ' ' << head->name << ' ' << head->age << ' ' << endl;
		head = head->next;
	}
}
node *change(node* head, int m, string ming, int agee)
{
	node* h1 = head;
	while (head)
	{
		if (head->num == m)
		{
			head->name = ming;
			head->age = agee;
			break;
		}
		head = head->next;
	}
	return h1;
}
int main()
{
	int n, i, j, k, m, agee;
	string ming;
	cin >> n;
	node* head = NULL;
	node* q=NULL, * p;
	while (n--)
	{
		p = new node;
		cin >> p->num >> p->name >> p->age;
		p->next = NULL;
		if (head == NULL)
			head = p;
		else q->next = p;
		q = p;
	}
			cin >> m >> ming >> agee;
			head=change(head, m, ming, agee);
	show(head);
	return 0;
}
  1. 删除结点
#include<iostream>
using namespace std;
struct node
{
	int num;
	string name;
	int age;
	node* next;
};
void show(node*head)
{
	while (head)
	{
		cout << head->num << ' ' << head->name << ' ' << head->age << ' ' << endl;
		head = head->next;
	}
}
node* del(node* head, int k)
{
	node* pre = head;
	node* cur = head->next;
	while (cur!=NULL)//先遍历除头结点之外的部分
	{
		if (cur->age == k)
		{
			pre->next = cur->next;
		}
		else pre = cur;
		cur = cur->next;
	}
	if (head->age == k)//最后看节点
		head = head->next;
	return head;
}
int main()
{
	int n, i, j, k, m, agee;
	string ming;
	cin >> n;
	node* head = NULL;
	node* q=NULL, * p;
	while (n--)
	{
		p = new node;
		cin >> p->num >> p->name >> p->age;
		p->next = NULL;
		if (head == NULL)
			head = p;
		else q->next = p;
		q = p;
	}
	cin >> k;
	head=del(head, k);
	show(head);
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2202830.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

链表的回顾与总结(二)有序链表合并、约瑟夫问题

两个有序链表的合并 #include<iostream> using namespace std; struct node {int num;node* next; }; void show(node* head) {while (head){cout << head->num << ;head head->next;} } void add(node* head1, node* head2,node*&head3) {hea…

东营市自闭症寄宿学校,专注提供个性化教育

自闭症&#xff0c;这个听起来遥远而陌生的词汇&#xff0c;实际上正在影响着越来越多的家庭。自闭症儿童&#xff0c;被亲切地称为“星星的孩子”&#xff0c;他们活在自己的世界里&#xff0c;对周围的一切往往视而不见、听而不闻。然而&#xff0c;在广州的星贝育园自闭症儿…

万恶之源:端口不通

解决方案 本地端口开放&#xff0c;第三方使用者无法访问&#xff0c;原因无非有三&#xff1a; 1、基础网络不通 网不通&#xff0c;别说端口了&#xff0c;连ping都没办法&#xff1a;因此&#xff0c;请首先检查双方网络基础环境是否互通 2、防火墙拦截 新开的服务器&#x…

P4可编程技术详解:从理论到硬件实现

P4的诞生 为打破传统的固定封装模式&#xff0c;充分解放数据平面的编程能力&#xff0c;Nick McKeown领导的斯坦福大学研究团队于2014年提出可编程处理语言P4。借助P4的数据平面编程能力&#xff0c;用户可在网卡、交换机、路由器等网络设备上实现包括VXLAN、MPLS等在内的各种…

Docker容器不断重启问题

1. compose配置文件 docker-compose.yml services:node_app:image: dockerproxy.cn/node:lts-bullseye-slimcontainer_name: node_appnetworks:macvlan_net:ipv4_address: 192.168.2.3# 挂载的卷标volumes:- app:/app# 工作目录working_dir: /app# 自动重启restart: always# …

系统移植三

一、设备树 设备树&#xff08;Device Tree&#xff09;是 Linux 系统中用于描述硬件信息的一种机制&#xff0c;尤其在 ARM 平台上广泛使用。在早期版本的 Linux&#xff08;如 2.6 及之前&#xff09;&#xff0c;设备的硬件信息通常是通过硬编码方式写在平台相关的文件中。…

python爬虫--tx动漫完整信息抓取

python爬虫--tx动漫完整信息抓取 一、采集主页信息二、采集详情页信息三、完整代码一、采集主页信息 先看一下采集到的信息,结果保存为csv文件: 打开开发者工具,找到数据接口。 使用xpath提取详情页url。 二、采集详情页信息 如上图所示,使用xpath提取详情页的标题、作…

CPU超线程技术是什么,怎么启用超线程技术

超线程技术是一种允许单个物理CPU核心模拟成两个逻辑核心的技术&#xff0c;从而提升处理器的并行性能和效率。以下是对超线程技术的详细介绍&#xff1a; 基本概念&#xff1a;超线程&#xff08;Hyper-Threading&#xff0c;HT&#xff09;是Intel公司研发的一种技术&#x…

mbist之sharebus知识小结

本文摘录至微信公众号 搞搞芯意思 为何用sharebus? CPU/NPU/GPU等关键模块对性能要求很高,对功耗、时序、面积敏感,是整个芯片设计瓶颈。常规mbist设计插入的额外电路会对function时序收敛带来负面影响,而且会造成布线拥堵,给芯片性能和pd设计带来挑战。sharebus方…

【环境搭建】远程服务器搭建ElasticSearch

参考&#xff1a; 非常详细的阿里云服务器安装ElasticSearch过程..._阿里云服务器使用elasticsearch-CSDN博客 服务器平台&#xff1a;AutoDL 注意&#xff1a; 1、切换为非root用户&#xff0c;su 新用户名&#xff0c;否则ES无法启动 2、安装过程中没有出现设置账号密码…

python发送邮件带附件:配置全指南与步骤?

python发送邮件带附件教程&#xff1f;python如何发邮件带附件&#xff1f; 无论是工作报告、项目文档还是个人通知&#xff0c;邮件都能快速传递信息。而当这些信息需要附带文件时&#xff0c;Python发送邮件带附件的功能就显得尤为重要。AokSend将详细介绍如何使用Python发送…

线程属性详解

目录 catch语句 catch语句简介 catch语句中的内容 线程的属性 Thread提供的属性和方法 线程的属性方法 前台线程 后台线程 是否存活 catch语句 catch语句简介 我们首先认识一下catch语句&#xff0c;catch语句是用来捕获异常&#xff0c;在catch代码块中可以在捕获异…

Spark第一天

MapReduce过程复习 Spark由五部分组成 RDD五大特征 1、 Spark -- 代替MapReduce <<<<< scala是单机的&#xff0c;spark是分布式的。>>>>> 开源的分布式计算引擎 可以快速做计算 -- 因为可以利用内存来做一些计算 (1) 分为5个库(模块) : 1、…

C# 创建Windows服务,bat脚本服务注册启动删除

1、创建Windows服务&#xff0c;如下图。.NET Framework 4。Visual Studio 2019 。 2、在项目文件夹下创建文件夹LogConfig用于配置log4net&#xff0c;在LogConfig文件夹下新建log4net.config文件&#xff0c;如下图。 log4net.config文件内容如下。 <?xml version"…

能自动铲屎的自动猫砂盆是智商税吗?双十一热门自动猫砂盆推荐

大家平时一天要给猫咪铲几次屎呢&#xff1f;大多数应该都是早中晚各一次吧&#xff0c;在家的时候尚且能办到&#xff0c;但是一到了上班、出差、旅游的日子&#xff0c;我们又要如何保证猫咪的猫砂盆得到及时的清洁呢&#xff1f;要知道小猫咪的屎也很臭&#xff0c;猫砂盆长…

Linux云计算 |【第四阶段】RDBMS2-DAY4

主要内容&#xff1a; MHA概述、部署MHA集群 一、MHA概述 1、MHA简介 MHA&#xff08;Master High Availability&#xff09;是一款开源的MySQL的高可用程序&#xff0c;由日本DeNA公司youshimaton开发&#xff0c;是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的…

Vue组件继承与扩展

Vue组件继承与扩展 前言 与Class继承类似&#xff0c;在Vue中可以通过组件继承来达到复用和扩展基础组件的目的&#xff0c;虽然它可能会带来一些额外的性能损耗和维护成本&#xff0c;但其在解决一些非常规问题时有奇效。本文将通过一些非常规的功能需求来讨论其实现过程。 …

广州自闭症寄宿学校有哪些?选择最适合孩子的学校

在广州这座繁华而充满人文关怀的城市里&#xff0c;有一群特殊的孩子&#xff0c;他们被称为“星星的孩子”——自闭症儿童。他们生活在自己的世界里&#xff0c;对外界的刺激反应迟钝或过度敏感&#xff0c;社交互动困难&#xff0c;语言表达受限。然而&#xff0c;在广州&…

高中数学基础

1.1函数的定义与性质 01函数定义 D是一个非&#xff0c;空时数集&#xff0c;对于D中的每一个X都有一个对应的规则f&#xff0c;能相应只对应唯一的一个实数Y&#xff0c;那么可以称Yf(X)&#xff0c;是一个函数。 判断是否为同一个函数&#xff0c;需要满足两个条件&#xff…

基于springboot和vue.js 养老院管理系统设计与实现

博主介绍&#xff1a;专注于Java&#xff08;springboot ssm springcloud等开发框架&#xff09; vue .net php phython node.js uniapp小程序 等诸多技术领域和毕业项目实战、企业信息化系统建设&#xff0c;从业十五余年开发设计教学工作 ☆☆☆ 精彩专栏推荐订阅☆☆☆…