C ++双链表

时间:2014-05-24 16:57:01

标签: c++

我是C ++的新手,我正在寻求帮助!我正在尝试使用DLL实现带有加权路径的有向图。我无法检查我的实施,因为我遇到了一些麻烦,我不知道如何解决它。

我的代码:

#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

1 个答案:

答案 0 :(得分:2)

Edge结构中使用它之前,您需要定义Node