c++ 标准库有没有链表
时间: 2024-01-06 13:05:28 浏览: 36
C++标准库提供了一组通用的编程数据结构和函数,包括链表。在C++标准库中,链表被实现为双向链表,即每个节点都包含指向前一个节点和后一个节点的指针。你可以使用C++标准库中的list容器来创建和操作链表。
以下是一个使用C++标准库中的list容器创建链表的示例代码:
```cpp
#include <iostream>
#include <list>
int main() {
std::list<int> myList; // 创建一个空的链表
// 在链表末尾插入元素
myList.push_back(10);
myList.push_back(20);
myList.push_back(30);
// 遍历链表并输出元素
for (auto it = myList.begin(); it != myList.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果为:
```
10 20 30
```
这段代码创建了一个空的链表`myList`,然后使用`push_back`函数在链表末尾插入了三个元素。最后,使用迭代器遍历链表并输出元素。
相关问题
调用C++标准库实现链表
以下是使用C标准库实现链表的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
void insertNode(Node** head, int data) {
Node* node = createNode(data);
if (*head == NULL) {
*head = node;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
}
void deleteNode(Node** head, int data) {
Node* current = *head;
Node* previous = NULL;
while (current != NULL && current->data != data) {
previous = current;
current = current->next;
}
if (current == NULL) {
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
insertNode(&head, 4);
insertNode(&head, 5);
printList(head);
deleteNode(&head, 3);
printList(head);
return 0;
}
```
在此示例中,我们使用C标准库的malloc()函数来分配内存,使用free()函数来释放内存。我们还使用了指向指针的指针来传递指针参数并修改指针的值。
c++标准库有没有环形数据结构
C++标准库中没有直接提供环形数据结构。然而,你可以使用其他数据结构来实现环形数据结构的功能。一种常见的方法是使用循环链表来模拟环形数据结构。循环链表是一种链表,其中最后一个节点指向第一个节点,形成一个闭环。通过这种方式,你可以在C++中实现环形队列、环形缓冲区等环形数据结构。
以下是一个使用循环链表实现环形队列的示例代码:
```cpp
#include <iostream>
template <typename T>
class CircularQueue {
private:
struct Node {
T data;
Node* next;
};
Node* front;
Node* rear;
public:
CircularQueue() {
front = nullptr;
rear = nullptr;
}
void enqueue(T value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
if (front == nullptr) {
front = newNode;
} else {
rear->next = newNode;
}
rear = newNode;
rear->next = front;
}
T dequeue() {
if (front == nullptr) {
throw std::runtime_error("Queue is empty");
}
T value = front->data;
Node* temp = front;
if (front == rear) {
front = nullptr;
rear = nullptr;
} else {
front = front->next;
rear->next = front;
}
delete temp;
return value;
}
bool isEmpty() {
return front == nullptr;
}
};
int main() {
CircularQueue<int> queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
while (!queue.isEmpty()) {
std::cout << queue.dequeue() << " ";
}
return 0;
}
```
这段代码演示了如何使用循环链表实现一个环形队列。enqueue函数用于将元素添加到队列中,dequeue函数用于从队列中移除并返回元素。isEmpty函数用于检查队列是否为空。