python wed自动化面试题

时间: 2023-10-21 21:09:48 浏览: 34
链表基本操作的函数实现如下: ```c++ #include <iostream> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x): val(x), next(nullptr) {} }; class LinkedList { public: LinkedList() { head = new ListNode(0); } void insert(int val) { ListNode* node = new ListNode(val); node->next = head->next; head->next = node; } void remove(int val) { ListNode* pre = head; while (pre->next && pre->next->val != val) { pre = pre->next; } if (pre->next) { ListNode* delNode = pre->next; pre->next = delNode->next; delete delNode; } } bool find(int val) { ListNode* cur = head->next; while (cur && cur->val != val) { cur = cur->next; } if (cur) { return true; } else { return false; } } void print() { ListNode* cur = head->next; while (cur) { cout << cur->val << " "; cur = cur->next; } cout << endl; } ListNode* predecessor(int val) { ListNode* pre = head; while (pre->next && pre->next->val != val) { pre = pre->next; } return pre; } ListNode* successor(int val) { ListNode* cur = head->next; while (cur && cur->val != val) { cur = cur->next; } if (cur && cur->next) { return cur->next; } else { return nullptr; } } void merge(LinkedList& other) { ListNode* cur1 = head->next; ListNode* cur2 = other.head->next; ListNode* pre = head; while (cur1 && cur2) { if (cur1->val < cur2->val) { pre = cur1; cur1 = cur1->next; } else { ListNode* node = cur2; cur2 = cur2->next; node->next = pre->next; pre->next = node; pre = pre->next; } } if (cur2) { pre->next = cur2; other.head->next = nullptr; } } void clear() { ListNode* cur = head->next; while (cur) { ListNode* delNode = cur; cur = cur->next; delete delNode; } head->next = nullptr; } void destroy() { clear(); delete head; head = nullptr; } int length() { int len = 0; ListNode* cur = head->next; while (cur) { len++; cur = cur->next; } return len; } int get(int pos) { ListNode* cur = head->next; while (cur && pos > 0) { cur = cur->next; pos--; } if (cur) { return cur->val; } else { return -1; } } private: ListNode* head; }; ``` 其中,`ListNode` 表示链表的结点结构体,`LinkedList` 类实现了链表的基本操作,包括: - `insert`:在链表头插入一个元素; - `remove`:删除链表中第一个值等于给定值的结点; - `find`:查找链表中是否存在给定值的结点; - `print`:输出整个链表; - `predecessor`:查找给定值的前驱结点; - `successor`:查找给定值的后继结点; - `merge`:合并两个有序链表; - `clear`:将链表置为空表; - `destroy`:销毁链表; - `length`:求链表的长度; - `get`:获取某位置结点的内容。 下面是一个示例程序: ```c++ int main() { LinkedList list; list.insert(1); list.insert(3); list.insert(5); list.insert(7); list.print(); // 7 5 3 1 list.remove(3); list.print(); // 7 5 1 cout << list.find(5) << endl; // 1 cout << list.find(3) << endl; // 0 cout << list.length() << endl; // 3 cout << list.get(1) << endl; // 5 LinkedList other; other.insert(2); other.insert(4); other.insert(6); other.print(); // 6 4 2 list.merge(other); list.print(); // 7 6 5 4 2 1 list.destroy(); return 0; } ```

相关推荐

最新推荐

recommend-type

Python自动化测试笔试面试题精选

在本篇文章里小编给大家整理的是一篇关于Python自动化测试笔试面试时常见的编程题,需要的朋友们可以学习参考下。
recommend-type

python自动化办公手册.pdf

python自动化办公手册pdf ,更多Pyhon自动化学习可参考我的这篇文章:https://blog.csdn.net/qq_36816848/article/details/118077191
recommend-type

实用自动化运维Python脚本分享

今天小编就为大家分享一篇实用自动化运维Python脚本。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python面试面试宝典100题

整理的python面试宝典,100题,包你面试无忧,遇见python面试题,遇神杀神、遇佛杀佛,欢迎大家下载评价,谢谢。
recommend-type

2019 Python最新面试题及答案16道题

主要介绍了2019 Python最新面试题及答案16道题 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。