基于c++创建列表,从尾部添加数据

时间: 2023-02-06 13:52:58 浏览: 69
在 C 语言中,可以使用链表来创建列表,并从尾部添加数据。 链表是一种动态数据结构,允许您在运行时动态地添加或删除元素。它由节点组成,每个节点都包含数据和指向下一个节点的指针。 要创建链表,需要定义一个结构体来表示每个节点,包括数据域和指针域。例如: ``` struct node { int data; struct node *next; }; ``` 然后,可以使用 malloc 函数为新节点分配内存,并使用指针操作来更新指针。例如,以下代码演示了如何在链表的尾部添加一个新节点: ``` struct node *head = NULL; // 初始化头指针 // 创建新节点 struct node *new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = 5; new_node->next = NULL; // 如果链表为空,则将新节点设为头节点 if (head == NULL) { head = new_node; } else { // 否则,遍历链表,找到最后一个节点 struct node *current = head; while (current->next != NULL) { current = current->next; } // 将新节点添加到最后一个节点的后面 current->next = new_node; } ``` 注意:在使用链表时,需要注意内存管理
相关问题

LRU 缓存 基于哈希表 C++

LRU 缓存基于哈希表的实现可以使用 STL 中的 `std::unordered_map`,这个容器提供了 O(1) 的查找和插入操作,非常适合用于实现缓存。下面是一个基于哈希表实现的 LRU 缓存的 C++ 代码: ```cpp #include <unordered_map> #include <list> class LRUCache { public: LRUCache(int capacity) : capacity_(capacity) {} int get(int key) { auto iter = cache_.find(key); if (iter == cache_.end()) { return -1; } else { auto value = iter->second->second; cache_list_.splice(cache_list_.begin(), cache_list_, iter->second); iter->second = cache_list_.begin(); return value; } } void put(int key, int value) { auto iter = cache_.find(key); if (iter == cache_.end()) { if (cache_.size() == capacity_) { int key_to_remove = cache_list_.back().first; cache_.erase(key_to_remove); cache_list_.pop_back(); } cache_list_.push_front({key, value}); cache_[key] = cache_list_.begin(); } else { iter->second->second = value; cache_list_.splice(cache_list_.begin(), cache_list_, iter->second); iter->second = cache_list_.begin(); } } private: int capacity_; std::list<std::pair<int, int>> cache_list_; std::unordered_map<int, std::list<std::pair<int, int>>::iterator> cache_; }; ``` 在这个实现中,我们使用了一个双向链表 `cache_list_` 来保存缓存中的数据,并使用一个哈希表 `cache_` 来提高查找效率。在 `get` 操作中,我们先在哈希表中查找目标数据,如果不存在则返回 -1,否则将目标数据移到链表头部并返回其值。在 `put` 操作中,如果缓存中不存在目标数据则创建一个新的节点并将其添加到链表头部,如果缓存已满则删除链表尾部的节点。如果缓存中已存在目标数据则将其值更新并将其移动到链表头部。 需要注意的是,在 `get` 操作中,我们需要使用 `splice` 操作将目标节点移到链表头部,并更新哈希表中对应节点的指针。在 `put` 操作中,我们需要先判断缓存是否已满,如果已满则删除链表尾部的节点,并在哈希表中删除对应的键值对。然后我们将新节点添加到链表头部,并在哈希表中创建对应的键值对。 另外,由于我们使用了 STL 中的 `std::list`,所以我们可以直接使用 `push_front` 和 `pop_back` 操作来添加和删除链表节点,非常方便。

用C++实现一个基于模板的双向循环链表并加上详细的注释

好的,以下是基于模板的双向循环链表的实现代码,注释详细: ```c++ #include <iostream> using namespace std; // 定义节点结构体 template<typename T> struct Node { T data; // 节点中存储的数据 Node<T>* next; // 指向下一个节点的指针 Node<T>* prev; // 指向前一个节点的指针 }; // 定义双向循环链表类 template<typename T> class DoublyLinkedList { private: Node<T>* head; // 头节点指针 Node<T>* tail; // 尾节点指针 int size; // 链表大小 public: // 构造函数 DoublyLinkedList() { head = nullptr; tail = nullptr; size = 0; } // 析构函数 ~DoublyLinkedList() { Node<T>* current = head; while (current != nullptr) { Node<T>* temp = current; current = current->next; delete temp; } size = 0; } // 获取链表大小 int getSize() { return size; } // 判断链表是否为空 bool isEmpty() { return size == 0; } // 在链表头部添加元素 void addFirst(T data) { Node<T>* newNode = new Node<T>; newNode->data = data; if (head == nullptr) { // 如果链表为空 head = newNode; tail = newNode; head->prev = tail; tail->next = head; } else { // 如果链表不为空 newNode->next = head; head->prev = newNode; head = newNode; head->prev = tail; tail->next = head; } size++; } // 在链表尾部添加元素 void addLast(T data) { Node<T>* newNode = new Node<T>; newNode->data = data; if (tail == nullptr) { // 如果链表为空 head = newNode; tail = newNode; head->prev = tail; tail->next = head; } else { // 如果链表不为空 newNode->prev = tail; tail->next = newNode; tail = newNode; head->prev = tail; tail->next = head; } size++; } // 在指定位置插入元素 void add(int index, T data) { if (index < 0 || index > size) { cout << "Index out of range." << endl; return; } if (index == 0) { // 在链表头部插入元素 addFirst(data); return; } if (index == size) { // 在链表尾部插入元素 addLast(data); return; } Node<T>* current = head; for (int i = 0; i < index - 1; i++) { current = current->next; } Node<T>* newNode = new Node<T>; newNode->data = data; newNode->next = current->next; current->next->prev = newNode; current->next = newNode; newNode->prev = current; size++; } // 删除链表头部元素 void removeFirst() { if (head == nullptr) { cout << "LinkedList is empty." << endl; return; } if (head == tail) { // 链表中只有一个节点 delete head; head = nullptr; tail = nullptr; } else { // 链表中有多个节点 Node<T>* temp = head; head = head->next; head->prev = tail; tail->next = head; delete temp; } size--; } // 删除链表尾部元素 void removeLast() { if (tail == nullptr) { cout << "LinkedList is empty." << endl; return; } if (head == tail) { // 链表中只有一个节点 delete tail; head = nullptr; tail = nullptr; } else { // 链表中有多个节点 Node<T>* temp = tail; tail = tail->prev; tail->next = head; head->prev = tail; delete temp; } size--; } // 删除指定位置的元素 void remove(int index) { if (index < 0 || index >= size) { cout << "Index out of range." << endl; return; } if (index == 0) { // 删除链表头部元素 removeFirst(); return; } if (index == size - 1) { // 删除链表尾部元素 removeLast(); return; } Node<T>* current = head; for (int i = 0; i < index; i++) { current = current->next; } current->prev->next = current->next; current->next->prev = current->prev; delete current; size--; } // 获取指定位置的元素 T get(int index) { if (index < 0 || index >= size) { cout << "Index out of range." << endl; return NULL; } Node<T>* current = head; for (int i = 0; i < index; i++) { current = current->next; } return current->data; } // 修改指定位置的元素 void set(int index, T data) { if (index < 0 || index >= size) { cout << "Index out of range." << endl; return; } Node<T>* current = head; for (int i = 0; i < index; i++) { current = current->next; } current->data = data; } // 打印链表 void printList() { if (head == nullptr) { cout << "LinkedList is empty." << endl; return; } Node<T>* current = head; while (current != tail) { cout << current->data << " "; current = current->next; } cout << current->data << endl; } }; int main() { // 创建双向循环链表对象 DoublyLinkedList<int> dll; // 在链表头部添加元素 dll.addFirst(1); dll.addFirst(2); dll.addFirst(3); dll.printList(); // 输出: 3 2 1 // 在链表尾部添加元素 dll.addLast(4); dll.addLast(5); dll.addLast(6); dll.printList(); // 输出: 3 2 1 4 5 6 // 在指定位置插入元素 dll.add(2, 7); dll.printList(); // 输出: 3 2 7 1 4 5 6 // 删除链表头部元素 dll.removeFirst(); dll.printList(); // 输出: 2 7 1 4 5 6 // 删除链表尾部元素 dll.removeLast(); dll.printList(); // 输出: 2 7 1 4 5 // 删除指定位置的元素 dll.remove(2); dll.printList(); // 输出: 2 7 4 5 // 获取指定位置的元素 cout << dll.get(2) << endl; // 输出: 4 // 修改指定位置的元素 dll.set(2, 8); dll.printList(); // 输出: 2 7 8 5 return 0; } ``` 这里实现了双向循环链表的基本操作,包括在链表头部、尾部和指定位置插入元素,删除链表头部、尾部和指定位置的元素,获取指定位置的元素和修改指定位置的元素等。在实现过程中,我们采用了模板化编程,使得该链表可以存储任意类型的数据。同时,为了更好地理解和使用该链表,我们在每个函数中都添加了详细的注释。

相关推荐

zip

最新推荐

recommend-type

基于QT C++实现的数据结构软件设计报告

哈工大(威海)计算机科学与技术学院 软件设计程序II的实验报告,基于QT,C++实现的简单饮食健康助手小程序,具有一定的数据结构知识的构建。原作者,可私聊源码。
recommend-type

C++从文本文件读取数据到vector中的方法

主要给大家介绍了利用C++如何从文本文件读取数据到vector中,文章通过实例给出示例代码,相信会对大家的理解和学习很有帮助,有需要的朋友们下面来一起看看吧。
recommend-type

C++实现string存取二进制数据的方法

主要介绍了C++实现string存取二进制数据的方法,针对STL中string的用法进行了较为详细的分析,需要的朋友可以参考下
recommend-type

c++获取进程信息列表和进程所调用的dll列表

主要介绍了c++获取进程信息列表和进程所调用的dll列表,大家参考使用吧
recommend-type

基于C++实现读取指定路径文件

主要介绍了基于C++实现读取指定路径文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

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