我正在學習創建鄰接表並且對此很新。我試圖在我的程序上測試一個。我想在鏈接列表中創建一個頂點,然後在該鏈接列表中創建一個列表或「邊」。我在這裏創建了一個鏈接,但不知道如何在鏈接列表中實際創建一個。我創建並測試了鏈表,我知道它的工作原理,我只需要創建一種方法將其實現到鄰接表中。另外,我不能使用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
我不明白。你也不應該在你的函數中使用靜態變量。 –