chapter_array_and_linkedlist/list/ #32
Replies: 76 comments 102 replies
-
|
仿照大佬Java写了个c++本地运行没啥问题,不会上传 |
Beta Was this translation helpful? Give feedback.
-
|
介绍数组缺点一项,在数组中插入或删除元素效率低下课题下,丢失元素或是否是语病? |
Beta Was this translation helpful? Give feedback.
-
|
线性表的顺序存储,就是您这章所讲的列表吗? |
Beta Was this translation helpful? Give feedback.
-
|
在列表末尾添加元素是不是也不严格为O(1),比如某些情况下列表正好需要先扩容再添加,这时候时间复杂度就会是O(N)。请问Python中的list是否也会存在这种问题? |
Beta Was this translation helpful? Give feedback.
-
|
如果创建多线程,并发的情况下,对同一个List操作,它怎么判断size的执行顺序 |
Beta Was this translation helpful? Give feedback.
-
|
cpp 中,vector 成员函数 size 的返回值类型为 size_type vector<int> list = { 1, 3, 2, 5, 4 };
...
int count = 0;
- for (int i = 0; i < list.size(); i++) {
+ for (size_t i = 0; i < list.size(); i++)
count++;
} |
Beta Was this translation helpful? Give feedback.
-
|
在最后的my_list.cpp中的 void extendCapacity() 方法似乎没有考虑整型溢出的风险(int newCapacity = capacity() * extendRatio;),由于extendRatio和numsCapacity都是int型,我认为这个风险还是蛮大的。或者说在这部分针对初学者的内容中暂时不用考虑这么细致? |
Beta Was this translation helpful? Give feedback.
-
|
大佬,C语言版本的动态数组没有嘛,我记得C primer plus 有讲 |
Beta Was this translation helpful? Give feedback.
-
|
大佬牛逼,这对新手很友好 |
Beta Was this translation helpful? Give feedback.
-
|
这行太棒了,直接补充了我几个知识点~ |
Beta Was this translation helpful? Give feedback.
-
|
写得好好啊!看了一堆课都不如大佬清清爽爽的梳理,但知识点又很全面了~新手老手都友好! |
Beta Was this translation helpful? Give feedback.
-
|
不好意思我想请问一下,在列表类实现中,“# 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组”是否应该为“新建一个长度为 self.__nums 的数组“? |
Beta Was this translation helpful? Give feedback.
-
could you give me some explanation about the "new" and "delete[]" operator? why we double "new" int[] for nums,why need delete[] tmp? the below is comment by CHATGPT: To fix this, you can simply remove the line delete[] tmp;, as it is unnecessary and could lead to issues if you intend to continue using the nums array. |
Beta Was this translation helpful? Give feedback.
-
里面的注释,索引i应该是j。 |
Beta Was this translation helpful? Give feedback.
-
|
看评论就学到了很多 |
Beta Was this translation helpful? Give feedback.
-
|
vector nums = { 1, 3, 2, 5, 4 }; |
Beta Was this translation helpful? Give feedback.
-
|
请问列表实现中的构造函数newMyList里,malloc前面为什么不用强转类型? |
Beta Was this translation helpful? Give feedback.
-
|
请问列表实现的c++代码那里,最后为什么是将列表转换为 Vector 用于打印 ,而不是和其他语言一样转换为数组打印? |
Beta Was this translation helpful? Give feedback.
-
|
TS实现List中 |
Beta Was this translation helpful? Give feedback.
-
|
十分感谢!!想请教一个小问题,本节中对于拼接列表的介绍使用了加号运算符,这种方式的时间复杂度可否简单介绍一下,是平方阶吗,谢谢:) |
Beta Was this translation helpful? Give feedback.
-
|
请教大家,在java语境下,判断插入元素时列表扩容条件为: |
Beta Was this translation helpful? Give feedback.
-
C#namespace _1.Console;
public class CustomList<T>
{
private T[] _array;
private readonly int _capacity = 10;
private int _count;
public int Count => _count;
public T this[int index]
{
get
{
VerifyIndex(index);
return _array[index];
}
set
{
VerifyIndex(index);
_array[index] = value;
}
}
public CustomList() => _array = new T[_capacity];
public void Add(T item)
{
EnsureCapacity();
_array[_count++] = item;
}
public void Insert(int index, T item)
{
VerifyIndex(index);
EnsureCapacity();
for (int i = _count - 1; i >= index; i--)
_array[i + 1] = _array[i];
_array[index] = item;
_count++;
}
public T RemoveAt(int index)
{
VerifyIndex(index);
var element = _array[index];
for (int i = index; i < _count; i++)
{
_array[i] = _array[i + 1];
}
_array[_count] = default!;
_count--;
return element;
}
public int FindIndex(T value)
{
if (value == null)
return -1;
for (int i = 0; i < _count; i++)
{
if (_array[i] == null)
continue;
if (_array[i]!.Equals(value))
return i;
}
return -1;
}
private void EnsureCapacity()
{
if (_count >= _array.Length)
Array.Resize(ref _array, _array.Length + _capacity);
}
private void VerifyIndex(int index)
{
if (index < 0 || index >= _count)
throw new IndexOutOfRangeException("索引超出数组范围");
}
} |
Beta Was this translation helpful? Give feedback.
-
|
原来js中的数组是列表而不是数组。 |
Beta Was this translation helpful? Give feedback.
-
|
Rust也许用reserve扩容会好一些? let new_capacity = self.capacity * self.extend_ratio;
self.arr.reserve(new_capacity - self.arr.capacity());
// 这里不改变 len(),只是提前申请内存
self.capacity = new_capacity; |
Beta Was this translation helpful? Give feedback.
-
2025.7.13 18:50朱斌のおとうさん关于列表在c++中的实现#include <iostream>
#include <vector>
class MyList {
private:
int *arr;
int arrCapacity = 10;
int arrLength = 0;
int extendRatio = 2;
public:
//Constructor
MyList() : arr(new int[arrCapacity]) {}
//Deconstructor
~MyList() {
delete[] arr;
}
//Lenth of Array
int length() {
return arrLength;
}
//Capacity of Array
int capacity() {
return arrCapacity;
}
//Accessing Elements
int get(int index) {
if(index < 0 || index >= length()) {
throw std::out_of_range("index out of range!");
}
return arr[index];
}
//Updating Elements
void set(int index, int num) {
if(index < 0 || index >= length()) {
throw std::out_of_range("index out of range!");
}
arr[index] = num;
}
//Adding Elements at the End;
void add(int num) {
//When the number of elements exceeds capacity, trigger extending mechanism
if(length() == capacity()) {
extendCapacity();
}
arr[length()] = num;
arrLength++;
}
//Inserting Elements
void insert(int index, int num) {
if(index < 0 || index >= length()) {
throw std::out_of_range("index out of range!");
}
//When the number of elements exceeds capacity, trigger extending mechanism
if(length() == capacity()) {
extendCapacity();
}
//Moving the Elements
for(int j = length() - 1; j >= index; j--) {
arr[j + 1] = arr[j];
}
arr[index] = num;
arrLength++;
}
//Deleting Elements
int remove(int index) {
if(index < 0 || index >= length()) {
throw std::out_of_range("index out of range!");
}
int num = arr[index];
//Moving Elements
for(int j = index; j < length() - 1; j++) {
arr[j] = arr[j + 1];
}
arrLength--;
return num;
}
//Extending the List
void extendCapacity() {
//Initializing the Capacity of the Extended List
int newCapacity = capacity() * extendRatio;
int *tmp = arr;
arr = new int[newCapacity];
//Copying Elements to the New List
for(int i = 0; i < length(); i++) {
arr[i] = tmp[i];
}
//Releasing Memory
delete[] tmp;
arrCapacity = newCapacity;
}
//Convert to Vector
std::vector<int> toVector() {
//Only Convert the Elements within the Effective Range
std::vector<int> vec(length());
for(int i = 0; i < length(); i++) {
vec[i] = arr[i];
}
return vec;
}
}; |
Beta Was this translation helpful? Give feedback.
-
|
二元的列表怎么实现呢 |
Beta Was this translation helpful? Give feedback.
-
|
某种意义上来说,列表和数组是等价的 |
Beta Was this translation helpful? Give feedback.
-
|
有没有链表版 |
Beta Was this translation helpful? Give feedback.
-
|
让我来试试 |
Beta Was this translation helpful? Give feedback.
-
|
高能代码:链表版列表-简化版 class LinkedList int main() } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_array_and_linkedlist/list/
Your first book to learn Data Structure And Algorithm.
https://www.hello-algo.com/chapter_array_and_linkedlist/list/
Beta Was this translation helpful? Give feedback.
All reactions