如何实现一个简单的链表或栈结构

在C++中实现一个简单的链表或栈结构,我们可以分别来看。首先,链表是一种线性数据结构,其中每个元素都包含数据本身以及指向列表中下一个元素的指针(对于双向链表,还会有指向前一个元素的指针)。而栈是一种后进先出(LIFO, Last In First Out)的数据结构,它只允许在栈顶进行添加(push)或删除(pop)元素的操作。

链表(单向链表)

下面是一个简单的单向链表的实现:

复制代码

cpp复制代码

|---|-----------------------------------------------|
| | #include <iostream> |
| | |
| | struct ListNode { |
| | int val; |
| | ListNode *next; |
| | ListNode(int x) : val(x), next(nullptr) {} |
| | }; |
| | |
| | class LinkedList { |
| | public: |
| | ListNode *head; |
| | |
| | LinkedList() : head(nullptr) {} |
| | |
| | // 在链表末尾添加元素 |
| | void append(int val) { |
| | ListNode* newNode = new ListNode(val); |
| | if (!head) { |
| | head = newNode; |
| | return; |
| | } |
| | ListNode* temp = head; |
| | while (temp->next) { |
| | temp = temp->next; |
| | } |
| | temp->next = newNode; |
| | } |
| | |
| | // 遍历链表并打印元素 |
| | void print() { |
| | ListNode* temp = head; |
| | while (temp) { |
| | std::cout << temp->val << " "; |
| | temp = temp->next; |
| | } |
| | std::cout << std::endl; |
| | } |
| | |
| | // 析构函数,释放链表占用的内存 |
| | ~LinkedList() { |
| | while (head) { |
| | ListNode* temp = head; |
| | head = head->next; |
| | delete temp; |
| | } |
| | } |
| | }; |
| | |
| | int main() { |
| | LinkedList list; |
| | list.append(1); |
| | list.append(2); |
| | list.append(3); |
| | list.print(); // 输出: 1 2 3 |
| | return 0; |
| | } |

栈的实现可以使用数组或链表。这里我们使用链表来实现栈,因为链表在动态扩展方面更加灵活:

复制代码

cpp复制代码

|---|-----------------------------------------------------------------------------------------------------------|
| | #include <iostream> |
| | |
| | struct StackNode { |
| | int val; |
| | StackNode *next; |
| | StackNode(int x) : val(x), next(nullptr) {} |
| | }; |
| | |
| | class Stack { |
| | private: |
| | StackNode *top; |
| | |
| | public: |
| | Stack() : top(nullptr) {} |
| | |
| | // 判断栈是否为空 |
| | bool isEmpty() { |
| | return top == nullptr; |
| | } |
| | |
| | // 向栈顶添加元素 |
| | void push(int val) { |
| | StackNode* newNode = new StackNode(val); |
| | newNode->next = top; |
| | top = newNode; |
| | } |
| | |
| | // 从栈顶删除元素 |
| | int pop() { |
| | if (isEmpty()) { |
| | throw std::runtime_error("pop from empty stack"); |
| | } |
| | StackNode* temp = top; |
| | int val = temp->val; |
| | top = top->next; |
| | delete temp; |
| | return val; |
| | } |
| | |
| | // 获取栈顶元素(不移除) |
| | int peek() { |
| | if (isEmpty()) { |
| | throw std::runtime_error("peek from empty stack"); |
| | } |
| | return top->val; |
| | } |
| | |
| | // 析构函数 |
| | ~Stack() { |
| | while (!isEmpty()) { |
| | pop(); |
| | } |
| | } |
| | }; |
| | |
| | int main() { |
| | Stack s; |
| | s.push(1); |
| | s.push(2); |
| | std::cout << "Top element is " << s.peek() << std::endl; // 输出: Top element is 2 |
| | s.pop(); |
| | std::cout << "After pop, top element is " << s.peek() << std::endl; // 输出: After pop, top element is 1 |
| | return 0; |
| | } |

在这两个示例中,我们定义了数据结构和基本操作,包括链表的添加、打印和析构,以及栈的push、pop、peek和析构。这些操作是这些数据结构的核心功能。

相关推荐
sulikey19 分钟前
一文彻底理解:如何判断单链表是否成环(含原理推导与环入口推算)
c++·算法·leetcode·链表·floyd·快慢指针·floyd判圈算法
孤廖1 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
豆沙沙包?2 小时前
2025年--Lc197-077. 排序链表(链表,尾插法)--Java版
java·数据结构·链表
2201_758875443 小时前
LeetCode:19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
Wenhao.3 小时前
LeetCode 合并K个升序链表
leetcode·链表·golang
杨福瑞3 小时前
C语言数据结构:算法复杂度(2)
c语言·开发语言·数据结构
涤生z4 小时前
list.
开发语言·数据结构·c++·学习·算法·list
雾时之林5 小时前
数据结构--------顺序表
数据结构
靠近彗星6 小时前
3.3栈与队列的应用
数据结构·算法
while(1){yan}8 小时前
数据结构之链表
数据结构·链表