我正在学习创建邻接表并且对此很新。我试图在我的程序上测试一个。我想在链接列表中创建一个顶点,然后在该链接列表中创建一个列表或“边”。我在这里创建了一个链接,但不知道如何在链接列表中实际创建一个。我创建并测试了链表,我知道它的工作原理,我只需要创建一种方法将其实现到邻接表中。另外,我不能使用C++库中的任何列表函数。创建一个邻接表
我的代码是否正确?
#include "Vertex.h"
Vertex::Vertex(){
neighbors = new LinkedList();
discover = 0;
finish = 0;
pi = NULL;
color = "white";
}
Vertex::~Vertex(){
delete neighbors;
}
void Vertex::insert(Vertex* vertex){
LinkedList *temp = new LinkedList();
if(index == 0){
temp->insertElement(vertex);
index++;
if(index != 0){
neighbors->insertElement(vertex);
}
}
} 这是我的主要问题。提前致谢!
#include <cstdlib>
#include <iostream> //to use cin and cout
#include <string> //to use strings
#include "LinkedList.h"
using namespace std;
int main(){
Vertex *vertex1 = new Vertex();
for (int i =0; i < 10; i++){
vertex1->insert(vertex1);
}
编辑固定的几件事情
代码中有太多'new's。你的拷贝构造函数等在哪里? –
我会创建一个while循环或一个索引,并增加创建的对象?@NeilKirk – Slae
我不明白。你也不应该在你的函数中使用静态变量。 –