cpp线性表

Exercises 2
(1) the following exercises are only for your references
1-1 Describe the advantages and disadvantages of the sequence storage structure (顺序存储结构)
and the linked storage structure (链式存储结构); the difference among the head pointer, the first
node and the first element node. Outline the characteristics of an ordered list (有序表), and the
similarities and differences between a vector (向量 or 数组) and an ordered table, the functions (功
能)of the head pointer variables and the head node.
1-2 Design an algorithm to delete ith element in a vector, and estimate its time efficiency.
1-3 Try to design an algorithm for getting the node number and deleting the first node in a circular
linked list (循环链表).
1-4 Knowing that the elements in the linear list L= (a1, a2, …, an) is ordered increasingly, and using
a vector to store data, try to writing an algorithm to delete the elements between c and d (c≦d) in
the linear list L= (a1, a2, …, an).
1-5 Assuming that the length of a circular linked list is larger than 1, which has no head node or
head pointer, p is a pointer to some node in the circular linked list, try to write an algorithms to
delete the precursor node (直接前趋结点) of the node pointed by p.
1-6 In a double linked list (双向链表), implement the following basic operations for the double
linked list: ①Initialize; ②Locate; ③Insert; ④delete.
1-7 Knowing a linked list as shown in Fig.1.1, try to write an algorithm to make a backup for the
linked list.
在这里插入图片描述
在这里插入图片描述

C++中,线性表是一种基础的数据结构,通常可以使用数组(Array)或链表(Linked List)来实现。这里分别给出简单版本的数组和链表实现。 ### 1. 数组实现 (动态数组 - std::vector) ```cpp #include <iostream> #include <vector> // 数组版本的线性表 class ArrayLinearList { public: // 构造函数,初始化大小 ArrayLinearList(size_t size) : data(size), capacity(size) {} // 添加元素到列表尾部,自动扩容 void push_back(int value) { if (data.size() == capacity) { resize(capacity * 2); // 扩容两倍 } data.push_back(value); } // 访问指定索引的元素 int get(int index) const { return data[index]; } private: std::vector<int> data; size_t capacity; // 当前存储容量 void resize(size_t new_capacity) { data.resize(new_capacity); } }; int main() { ArrayLinearList list(5); list.push_back(1); list.push_back(2); std::cout << "Element at index 0: " << list.get(0) << std::endl; return 0; } ``` ### 2. 链表实现 (单向链表) ```cpp #include <iostream> // 单向链表节点 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; // 链表版本的线性表 class LinkedListLinearList { public: void push_back(int value) { ListNode* newNode = new ListNode(value); if (!head) { head = newNode; } else { ListNode* current = head; while (current->next) { current = current->next; } current->next = newNode; } } // 返回当前链表长度,如果为空则返回0 size_t size() const { size_t count = 0; for (ListNode* node = head; node != nullptr; ++node, ++count) {} return count; } private: ListNode* head; }; int main() { LinkedListLinearList list; list.push_back(1); list.push_back(2); std::cout << "Length of the list: " << list.size() << std::endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值