簡體   English   中英

C ++雙鏈表

[英]C++ doubly linked list

我是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

您需要先定義Edge然后才能在Node結構中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM