我試圖構造一個鄰接表。我寫的代碼如下。無法將'list *'轉換爲'Node *'
struct Node
{
int dest;
struct Node* next;
};
struct list
{
struct list *head;
};
的類定義爲:
class Graph
{
private:
int vertix;
list *arr;
public:
Graph(int v)
{
vertix = v;
arr = new list [vertix];
for(int i=0;i<vertix;i++)
{
arr[i].head=NULL;
}
}
Node* getNewNode(int destination)
{
Node* newNode = new Node;
newNode->dest = destination;
newNode->next = NULL;
return newNode;
}
的錯誤是在這些功能:
void addEdge(int src, int dest)
{
Node* newNode = getNewNode(dest);
newNode->next = arr[src].head;
arr[src].head = newNode;
newNode = getNewNode(src);
newNode->next = arr[dest].head;
arr[dest].head = newNode;
}
void print()
{
cout<<"Adjacency list of vertix: "<<endl;
for(int i = 0; i< vertix; i++)
{
Node *ptr = arr[i].head;
cout<< i << "-->";
while(ptr)
{
cout<< "-->"<<ptr->dest;
ptr=ptr->next;
}
cout<<endl;
}
}
};
錯誤消息我得到的是: [錯誤]不能轉換名單* 'to'節點*' [錯誤]無法在初始化中將'list *'轉換爲'Node *'
你並不需要執行一個鏈接列表中,你也可以使用std :: list。 – KimKulling
你在哪些行上收到這些消息? – alexeykuzmin0
看起來你的'list'結構應該指向'Node',而不是'list'。 – alexeykuzmin0