2014-05-24 189 views
-1

我是C++新手,我在尋求幫助!我正嘗試使用DLL實現帶有加權路徑的有向圖。我不能檢查我的執行情況,因爲我有一些麻煩,我不知道如何解決它。C++雙向鏈表

我的代碼:

#include <iostream> 
#include <vector> 
#include <set> 
#include <stdio.h> 
#include <time.h> 
#include <math.h> 
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <cstring> 
#include <climits> 
#include <algorithm> 

using namespace std; 
    typedef struct Node Node; 
typedef struct Edge Edge; 
typedef struct DLList DLList; 



struct Node 
{ 
    int id; 
    int dist; 
    Node* prev; 
    vector<Edge*> edges; 
}; 

struct Edge 
{ 
    int length; 
    Node* to; 
}; 

struct DLList 
{ 
    DLList *next; 
    DLList *prev; 
    Node* val; 
}; 

Node* g_firstNode = 0; 
Node* g_currentNode = 0; 
DLList* first = 0; 
DLList* last = 0; 

Node* getFirst() 
{ 
    if(first == 0) 
    { 
     cout << "Pusta lista " << endl; 
    } 
    return first->val; 
} 

DLList* add(Node* n) 
{ 
    DLList* newNode = new DLList; 
    if(first == 0) 
    { 
     last = 
     newNode->val = n; 
     newNode->next = 0; 
     newNode->prev=0; 
     first = newNode;  
    } 
    else 
    { 
     last = 
     newNode->val = n; 
     newNode->next = 0; 
     newNode->prev=last; 
     last->next=newNode; 
    } 
} 

void removeFirst() 
{ 
    if(first!=0) 
    { 
     first = first->next; 
     if(first != 0) 
     { 
      first->prev = 0;   
     } 
    } 
} 


void remove(DLList* e) 
{ 
    if(e==first) 
    { 
     removeFirst(); 
    } 
    else if(e==last) 
    { 
     last = last->prev; 
     last -> next = 0; 
    } 
    else 
    { 
     e->prev->next = e->next; 
     e->next->prev = e->prev; 
    } 
} 

void remove(Node* n) 
{ 
    if(n==first->val) 
    { 
     removeFirst(); 
    } 
    else if(n==last->val) 
    { 
     last = last->prev; 
     last->next = 0; 
    } 
    else 
    { 
     for(DLList* i=first; i!=0; i=i->next) 
     { 
      if(n==i->val) 
      { 
       i->prev->next = i->next; 
       i->next->prev = i->prev; 
       break; 
      }  
     } 
    } 
} 

bool isEmpty() 
{ 
    return first == 0; 
} 

int main() 
{ 
    Node* start = 0; 
    Node* end = 0; 
    Node* newNode = new Node; 
    vector<Node*> nodes(5); 
    int maxlen=0,i=0,j=0,from=0,n_edges=18,n_nodes=5; 

    for(i=0; i<n_nodes;i++) 
    { 
     newNode->id = n_nodes; 
     nodes[n_nodes] = newNode; 
    } 
    Edge* newEdge = new Edge; 
    newEdge->length=5; 
    newEdge->to=nodes[4]; 
    nodes[0]->edges.push_back(newEdge); 

    return 0; 
} 

錯誤:

graph.cpp:57:18: error: cannot convert ‘Node*’ to ‘DLList*’ in assignment 
graph.cpp:65:18: error: cannot convert ‘Node*’ to ‘DLList*’ in assignment 
+3

第一個編譯器錯誤非常簡單,爲什麼不解決它? – Inspired

+0

不知道第57行和第65行中裸體'last ='的用途是什麼。 – jsantander

+0

爲什麼不使用標準庫容器,比如'std :: vector'。 –

回答

2

你需要你在Node結構使用它之前定義Edge

+1

或者添加一個前向聲明... – jsantander

+0

好吧,我的錯誤,我的帖子編輯爲 – Krzysiek

+1

@ Coda17,他不能在節點之前定義Edge,因爲Node使用邊緣; Bart需要用前向聲明來聲明Edge,如:struct Edge;在Node中使用它之前。 – okaram