【问题标题】:Linked Lists with vectors带向量的链表
【发布时间】:2014-06-28 15:17:34
【问题描述】:

我正在尝试通过向量上的链表执行某些操作。

我们得到了一个结构体类型向量

typedef struct{
int *array;   // a pointer to vector's storage
int size;     // the current number of elements in the vector
int cap;      // the current capacity of the vector;
int init_cap; // the initial capacity the vector was initialised with.
} vector;

现在,我想创建一个函数,它接收指向向量结构的指针,并使用给定的容量对其进行初始化。所有字段都将被初始化。我想使用链表来做到这一点。

这是我的代码

#include <iostream>
using namespace std;

typedef struct node {
int *array;     // a pointer to the vector's storage
int size;       // the current number of elements in the vector
int cap;        // the current capacity of the vector
int init_cap;   // the initial capacity the vector was initialised with
node *next;

} vector;

node *head = NULL;

我可以像我在上面编写的代码中尝试的那样从向量结构中创建节点吗?

void vector_init(vector *v, int capacity){

//initialising the vector with the given capacity
v->size     = capacity;
v->cap      = capacity;
v->init_cap = capacity;

//linked list with nodes created and values initialised

node *temp, temp2;
temp = head;
temp = new node;
temp->size     = capacity;
temp->cap      = capacity;
temp->init_cap = capacity;


temp->next = temp2
temp2 = new node;
temp2->size     = capacity;
temp2->cap      = capacity;
temp2->init_cap = capacity;
    temp2->next = NULL;
}

我是否创建了链表并正确初始化了值?如果我们不创建临时点 temp 和 temp2,而只是使用 v->size 等来初始化字段,那会不会使它成为一个链表?

【问题讨论】:

  • typedef struct,认真的吗?谁教你这些东西?
  • 这个问题更适合Code Review,只要您的代码没有遇到任何特殊问题。
  • 我建议不要将名称 vector 用于您自己的数据结构,因为这可能会与 std::vector 类冲突,尤其是因为您声明了 using namespace std;
  • 好的,如果我们删除 using namespace std;然后使用向量作为数据结构,可以吗? @djikay
  • 令人难以置信的是为什么一个所谓的“C++ 讲师”会将用户定义的类型称为vector

标签: c++ vector linked-list


【解决方案1】:

你的代码有很多问题。

  • 不要使用名称vector - 有一个结构叫std::vector,很容易混淆。
  • 如果你想初始化结构的值,不要为此创建一个外部的、单独的函数——它不是 c++'ish。改为创建一个结构构造函数来初始化所有值。
  • 您没有在代码中的任何位置初始化 array 变量。您应该根据构造函数中给定的容量为其分配空间。
  • 不要为变量使用名称“数组”。 C++ 中有一个叫做 std::array 的结构,它可能会让人困惑。
  • 您的实施对我来说意义不大。您现在有一个数组链接列表;如果您想在功能上用 int 链接列表 替换 int 数组,则每个节点都应包含一个 int 值。
  • 如果出于某种原因,您希望坚持使用此实现,您还需要某种更新函数,在添加时自动更新 sizecap 变量或从 array 中删除元素。否则,您肯定会最终忘记它,并且您的结构会一团糟。让这个函数成为结构的一部分 - 它不应该是一个外部函数。
  • 即使在将单词 vector 更改为其他词之后,那个 typedef struct node 也没有意义 - 您无论如何都不会在代码中使用它。
  • 您对两个不同的结构使用相同的名称; vector 最初定义为具有 4 个字段,在接下来的几行中定义为具有 5 个字段。

技术上是的,这是一个链表,但是您的 vector_init() 函数无法正常工作。除了我上面写的:

  • 您应该避免使函数依赖于全局变量,在本例中为 head。它可以作为参数传递。
  • 这两行:

温度=头;

temp = new node;

没有意义。第一个使变量 temp 指向 head;第二个告诉 temp 在您使用运算符 new 时开始指向新变量,该运算符分配空间并返回指向新创建变量的指针。因此,当您进行进一步操作时,您不会对变量头进行操作,而是对另一个在 temp 指针失效后将丢失的变量进行操作。

  • 您根本不需要 temptemp2 变量。他们只会膨胀代码。

  • 这两行:

temp->下一个 = temp2;

temp2 = new node;

应该切换位置,因为现在你分配了一个尚未初始化的指针。

  • 写完所有这些东西后,我意识到这个函数通常是不正确的。出于某种原因,您首先处理参数 v,然后执行与它无关的操作。

另外,您的讲师说您可以使用链表解决所有类型的问题,这并不正确。它可能会在某些情况下解决一些问题,或产生新的问题,具体取决于上下文。

我不想变得粗鲁,但你被赋予的任务本身的概念似乎存在根本性的错误。我想有人真的没有考虑清楚。

【讨论】:

  • new node;会调用默认构造函数,如果没有提供则编译代码时会弹出错误,但它是有效的C++
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-03
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多