Dynamic vector using Struct
Dynamic vector using Struct
我有这个结构:
struct Node {
int number;
Node *next;
};
和这个类插入元素并显示向量:
// Classe DynamicVector :
// e' la classe che consente di inserire elementi
// e visualizzare il vettore di strutture
class DynamicVector
{
public:
DynamicVector();
void InsertNumber(int number);
void ShowVector();
protected:
Node *p;
};
这就是实现:
DynamicVector::DynamicVector() {
this->p = NULL;
}
void DynamicVector::InsertNumber(int number) {
Node *temporary = new Node;
// Possiamo avere due possibili casi:
// non e' stato ancora inserito nessun elemento
// ...
if (this->p == NULL) {
temporary->number = number;
temporary->next = NULL;
this->p = temporary;
// ...
// oppure dobbiamo aggiungerne uno alla fine
// In questo caso meglio dire, lo "accodiamo"
} else {
// Sfogliamo le strutture fino a giungere
// all' ultima creata
while (this->p->next != NULL) {
this->p = this->p->next;
}
temporary->number = number;
temporary->next = NULL;
// In questo passaggio copiamo la struttura
// temporanea "temporary" nell' ultima struttura "p"
this->p->next = temporary;
}
}
void DynamicVector::ShowVector() {
while (this->p != NULL) {
std::cout << this->p->number << std::endl;
this->p = this->p->next;
}
}
在主要功能中,我写道:
#include <iostream>
#include <conio.h>
#include "dynamic_vector.h"
int main() {
DynamicVector *vector = new DynamicVector();
vector->InsertNumber(5);
vector->InsertNumber(3);
vector->InsertNumber(6);
vector->InsertNumber(22);
vector->ShowVector();
delete vector;
getch();
return 0;
}
我不知道为什么,但它只显示了最后两个数字。有人知道为什么吗?
它只显示最后两个数字,因为当您插入新数字时,您将头移动到下一个节点。有两个选项可以打印整个矢量。
if (this->p == NULL) {
temporary->number = number;
temporary->next = NULL;
this->p = temporary;
// ...
// oppure dobbiamo aggiungerne uno alla fine
// In questo caso meglio dire, lo "accodiamo"
} else {
// Sfogliamo le strutture fino a giungere
// all' ultima creata
Node* temp2 = this->p;
while (temp2->next != NULL) {
temp2 = temp2->next;
}
temporary->number = number;
temporary->next = NULL;
// In questo passaggio copiamo la struttura
// temporanea "temporary" nell' ultima struttura "p"
temp2->next = temporary;
}
或者在main()中,存储向量的第一个节点的位置,并将其用于打印
DynamicVector *vector = new DynamicVector();
DynamicVector *vector2 = vector;
...
vector2->ShowVector();
while (this->p->next != NULL) {
this->p = this->p->next;
}
在这段代码中,您可以跳过所有现有节点,这些节点将丢失。即当p不为NULL时调用它,然后将p重置为NULL。该代码没有意义,它等于p=NULL。
要么改变这个,要么做
vector->InsertNumber(5)->InsertNumber(3)->InsertNumber(6)->InsertNumber(22);
(您必须从InsertNumber返回"this");
或者两者都可以。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- C++Union/Struct位域的实现和可移植性
- "using namespace std;"在C++的作用是什么?
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- vector<vector<double>> to mxArray using memcpy
- Using C++ CryptAPI:CNG Create CSR
- STLPort using C++11
- 函数中的 c++ using 语句,后跟函数名称(对于 ADL?
- Using getchar_unlocked()
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 错误:"Left of getValue must have class/struct/union"
- 在 SVM-Struct 中未定义对 sqrt 的引用(已使用 -lm)
- 嵌套命名空间的"using"指令,但需要命名内部命名空间
- 为什么"using System;"不被视为不良做法?
- 数据类型"struct seq<0, 1, 2>{}"含义是什么?
- 为什么我需要做'using namespace std'而不是"使用 std::cout"?
- Dynamic vector using Struct