数据结构-链表习题(C++)

程序设计题: 单链表实验
1.从键盘输入若干大于0的整数,用这些整数构造一个单链表.当用户输入小于等于0的值时创建链表结束并在终端打印输出这个链表。
2.在链表中查找某结点,如果能找到输出这个结点是第几个结点,如果找不到,输出:找不到此结点。
3.删除指定结点: 如果指定的被删除结点存在就删除它,然后打印:已经删除,如果不存在,输出信息: 链表中没有此结点,无法删除。
4.删除重复结点:如果链表中有重复的元素,删除重复的元素,使得所有值最多只出现一次,例如,如果链表中有3个结点的值是100,那么需要删除两个值为100的结点。只保留一个值为100的结点。删除重复结点后打印输出整个链表

C++:


1------------------------------------------------------
#include <iostream>
using namespace std;struct Node {int data;Node* next;
};Node* createLinkedList() {Node* head = nullptr;Node* tail = nullptr;int input;while (true) {cin >> input;if (input <= 0) {break;}Node* newNode = new Node;newNode->data = input;newNode->next = nullptr;if (head == nullptr) {head = newNode;tail = newNode;} else {tail->next = newNode;tail = newNode;}}return head;
}void printLinkedList(Node* head) {Node* current = head;while (current != nullptr) {cout << current->data << " ";current = current->next;}
}int main() {Node* head = createLinkedList();printLinkedList(head);return 0;
}2------------------------------------------------------
int findNodePosition(Node* head, int value) {Node* current = head;int position = 1;while (current != nullptr) {if (current->data == value) {return position;}current = current->next;position++;}return -1; // Node not found
}
int main() {Node* head = createLinkedList();int valueToFind;cin >> valueToFind;int position = findNodePosition(head, valueToFind);if (position != -1) {cout << "The node is at position: " << position << endl;} else {cout << "Node not found" << endl;}return 0;
}
3------------------------------------------------------
int findNodePosition(Node* head, int value) {Node* current = head;int position = 1;while (current != nullptr) {if (current->data == value) {return position;}current = current->next;position++;}return -1; // Node not found
}int main() {Node* head = createLinkedList();int valueToFind;cin >> valueToFind;int position = findNodePosition(head, valueToFind);if (position != -1) {cout << "The node is at position: " << position << endl;} else {cout << "Node not found" << endl;}return 0;
}
4------------------------------------------------------
Node* removeDuplicates(Node* head) {Node* current = head;while (current != nullptr) {Node* runner = current;while (runner->next != nullptr) {if (runner->next->data == current->data) {Node* temp = runner->next;runner->next = runner->next->next;delete temp;} else {runner = runner->next;}}current = current->next;}return head;
}int main() {Node* head = createLinkedList();head = removeDuplicates(head);printLinkedList(head);return 0;
}

C:

#include <stdio.h>
#include <stdlib.h>struct Node {int data;struct Node* next;
};struct Node* createLinkedList() {struct Node* head = NULL;struct Node* tail = NULL;int input;while (1) {scanf("%d", &input);if (input <= 0) {break;}struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));newNode->data = input;newNode->next = NULL;if (head == NULL) {head = newNode;tail = newNode;} else {tail->next = newNode;tail = newNode;}}return head;
}void printLinkedList(struct Node* head) {struct Node* current = head;while (current != NULL) {printf("%d ", current->data);current = current->next;}
}int findNodePosition(struct Node* head, int value) {struct Node* current = head;int position = 1;while (current != NULL) {if (current->data == value) {return position;}current = current->next;position++;}return -1; // Node not found
}struct Node* removeDuplicates(struct Node* head) {struct Node* current = head;while (current != NULL) {struct Node* runner = current;while (runner->next != NULL) {if (runner->next->data == current->data) {struct Node* temp = runner->next;runner->next = runner->next->next;free(temp);} else {runner = runner->next;}}current = current->next;}return head;
}int main() {struct Node* head = createLinkedList();printLinkedList(head);int valueToFind;scanf("%d", &valueToFind);int position = findNodePosition(head, valueToFind);if (position != -1) {printf("The node is at position: %d\n", position);} else {printf("Node not found\n");}head = removeDuplicates(head);printLinkedList(head);return 0;
}

 

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

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

相关文章

什么是Ajax,Ajax的优点和用处有什么

Ajax&#xff08;Asynchronous JavaScript and XML&#xff09;是一种用于创建快速动态网页的技术。它利用JavaScript和XML&#xff08;现在也可以使用JSON或HTML&#xff09;来进行异步通信&#xff0c;实现在网页上更新数据而无需重新加载整个页面。 Ajax的优点和用处有以下…

翻译: LLM工具使用和代理Tool use and agents

欢迎来到本周的最后一个视频。在这个视频中&#xff0c;我想与您分享LLM&#xff08;大型语言模型&#xff09;开始能够使用工具的情况&#xff0c;以及讨论一下前沿的“代理”主题&#xff0c;这是让LLM自己决定下一步采取什么行动的领域。让我们来看看。在早期的食物订单接收…

【C 剑指offer】有序整型矩阵元素查找 {杨氏矩阵}

目录 题目内容&#xff1a; 思路&#xff1a; 图形演示&#xff1a; 复杂度分析 C源码&#xff1a; /** *************************************************************************** ******************** ********************* ******…

遥感论文 | Scientific Reports | 一种显著提升遥感影像小目标检测的网络!

论文题目&#xff1a;MwdpNet: towards improving the recognition accuracy of tiny targets in high-resolution remote sensing image论文网址&#xff1a;https://www.nature.com/articles/s41598-023-41021-8 摘要 提出MwdpNet&#xff0c;以提高对高分辨率遥感&#xf…

安卓免Root做klipper上位机教程

软件说明&#xff1a;虚拟电脑可以在8.0以上没越狱的安卓系统中安装klipper上位机程序实现对已刷入klipper固件的3D打印控制板的控制欢迎下载安装测试&#xff0c;反馈碰到的问题。安装步骤&#xff1a;1). 在手机上打开浏览器&#xff0c;访问这个网址 http://droidvm.com/cn/…

connect: Network is unreachable问题解决

第一步&#xff1a;查看ifcfg-ens33配置文件 cd /etc/sysconfig/network-scripts/ cat ifcfg-ens33 发现问题&#xff1a;GATEWAY写错成GATWAY 第二步&#xff1a;修改 vim ifcfg-ens33 第三步&#xff1a;检测是否成功 ping baidu.com 成功&#xff01;

Mysql 的ROW_NUMBER() 和分区函数的使用 PARTITION BY的使用

Mysql 的ROW_NUMBER() 和分区函数的使用 PARTITION BY的使用 描述&#xff1a; 遇到了一个需求&#xff0c;需要查询用户id和计划id&#xff0c;但是人员id的是重复&#xff0c;我想把人员id去重&#xff0c;支取一个。自然而然的就想到了 SELECT DISTINCT prj_plan.last_mon…

分布式事务--初识Seata和TC部署

1.Seata介绍 Seata是 2019 年 1 月份蚂蚁金服和阿里巴巴共同开源的分布式事务解决方案。致力于提供高性能和简单易用的分布式事务服务&#xff0c;为用户打造一站式的分布式解决方案。 官网地址&#xff1a;Seata | Seata&#xff0c;其中的文档、播客中提供了大量的使用说明…

LoRA(Low-Rank Adaptation)

LoRA&#xff08;Low-Rank Adaptation&#xff09; LoRA&#xff08;Low-Rank Adaptation&#xff09;是一种针对深度学习模型的参数调整方法&#xff0c;特别适用于大型预训练模型如GPT-3或BERT。它通过在模型的原有权重上添加低秩&#xff08;low-rank&#xff09;矩阵&…

一文读懂算法中的时间复杂度和空间复杂度,O(1)、O(logn)、O(n)、O(n^2)、O(2^n) 附举例说明,常见的时间复杂度,空间复杂度

时间复杂度和空间复杂度是什么 时间复杂度&#xff08;Time Complexity&#xff09;是描述算法运行时间长短的一个度量。空间复杂度&#xff08;Space Complexity&#xff09;是描述算法在运行过程中所需要的存储空间大小的一个度量。 时间复杂度和空间复杂度是衡量算法性能…

Python读写arxml文件

文章目录 前言一、XML简介二、XML文件结构三、Python读取xml文件安装ElementTree库读取xml文件四、Python写入xml文件前言 本文主要通过介绍arxml文件,为后续python脚本开发奠定基础。 arxml是AUTOSAR XML的简称,是一个通用的配置/数据库文件,实质是一个xml文件。 ①更规范…

Mysql查询条件为大于时,不走索引失效场景

如下 where a>1 and b2 是不走索引。 因为a>1 &#xff0c; (这里说的是&#xff0c;a是走了索引&#xff0c;但是b没有走&#xff0c;为什么了&#xff1f;因为b只有在a相同时&#xff0c;b才有序,也就是说这个联合索引只用到了一半)的数据是无序的(1 4 1 2)&#xff0…

WGAN 优势小结

我在上一篇博文为什么 GAN 不好训练中&#xff0c;分析了原始 GAN 难以训练的原因&#xff0c;本篇博文将分析下 WGAN 的优势。 1. Wasserstein 距离 W 是指 Wasserstein&#xff0c;Wasserstein 距离又叫Earth-Mover&#xff08;EM&#xff09;距离。Wasserstein距离相比KL散…

ubuntu18.04 安装yolov5环境及推理环境

文章目录 1、安装anaconda31.2、环境变量配置1.3、添加/更换 conda 清华源 2、安装pytorch1.63、CUDA安装4、安装cuDNN5、安装tensorRT6、安装opencv4.67、tensorRT部署yolov5模型推理 1、安装anaconda3 官方网网址 https://www.anaconda.com/download#downloads去到下载的文…

心理测试网站源码,知己心理React心理健康测试

源码介绍 React心理健康测试网站源码&#xff0c;帮助需要的人更好地了解自已的心理健康状态和人格特征。 React可以在Vite中启用HMR&#xff0c;并且包含了几人EsLint规则。只需要使用react antd-mobile即可 轻松部署完成。

数据分析为何要学统计学(9)——总体不服从正态分布时使用什么假设检验方法?

大多数情况下&#xff0c;我们都假设样本所在总体服从正态分布&#xff0c;然后使用t检验、方差分析等假设检验方法。但是总体如果不服从正态分布&#xff0c;那么就得使用非参数检验方法&#xff0c;如Mann-Whitney U检验和Wilcoxon秩和检验。其中Mann-Whitney U检验适用于独立…

千梦网创:逮住一闪而过的机会疯狂摩擦

我这个人平时想的就多&#xff0c;睡觉也在想事情&#xff0c;有时候睡觉里想的事情往往都是很纯粹的、很绝妙的&#xff0c;但是经常性一醒过来就忘了&#xff0c;再去回忆怎么也想不起来了。 灵感只在特定的环境下产生&#xff0c;这类环境是不可再生和模拟的。 机会只因特…

【C++11特性篇】盘点C++11中三种简化声明的方式【auto】【decltype】【nullptr】(3)

前言 大家好吖&#xff0c;欢迎来到 YY 滴C系列 &#xff0c;热烈欢迎&#xff01; 本章主要内容面向接触过C的老铁 主要内容含&#xff1a; 欢迎订阅 YY滴C专栏&#xff01;更多干货持续更新&#xff01;以下是传送门&#xff01; 目录 一.auto&#xff06;范围for二.decltyp…

用Python快速从深层嵌套 JSON 中找到特定的 Value

有时候&#xff0c;我们拿到一个JSON数据的时候&#xff0c;会难以看出其逻辑层次结构。 这时候就需要我们进行代码解析了。 代码&#xff1a; import jsondef find_json_value(data_json, value, path""):if isinstance(data_json, dict):for k, v in data_json.…

JS解构赋值:

数组解构&#xff1a; 将数组的单元值快速批量的赋值给一系列变量的简洁语法 变量的顺序对应数组单元值的位置依次进行赋值操作 const arr [100,60,80];//数组解构 赋值const [max,min,avg] arr;//const [max,min,avg] [100,60,80];// const max arr[0];// const min ar…