来自邻接列表的图表不能起作用

时间:2014-07-15 23:02:50

标签: c++ algorithm graph adjacency-list

我使用这个图表来获取命令“create 4”和“insert 3 2 1”,即创建4的邻接列表,并且节点3连接到具有权重1的节点2.

我似乎无法正确承受重量,我正在尝试制作一个邻接列表,如:

[0]
[1]
[2]
[3]->[2][1]

如果有人能提出一些解决方法,我将不胜感激。

这里是我的代码: main.cpp中

using namespace std;
#include "graph.h"

//Main Function 
int main(){

string command;
int src,dest,wght,size;
graph A(size);  //initial class instance

//handles all I/O
do{
    cout << "graph>";
    cin >> command;

    //Creates an array of size "size" for nodes to be added too
    if(command == "create"){

        cin>>size;
        graph A(size);
    }
    //Tests for user insertion of node
    else if(command == "insert"){

        cin >> src >> dest >> wght;
        if(src <= size && dest <= size){
            A.insert(src, dest, wght);
        }
        else{
            cout<<"Error! Node does not exist!"<<endl;
        }
    }

    //Tests for user removal of node
    else if(command == "remove"){

        cin >> src >> dest;
        if(src <= size && dest <= size){
            A.remove(src, dest);
        }
        else{
            cout<<"Error! Node does not exist!"<<endl;
        }
    }

    //Tests for user printing of graph
    else if(command == "print"){
        A.print();     
    }

    else{
        if(command != "quit")
            cout<<"Error! Command not supported."<<endl;
    }

}while(command != "quit" );


 return 0;
}

graph.h

//graph class file
using namespace std;
#include "node.h"

class graph{
    private:
        int h;
        class alist* array;
    public:

        //Initializes the Graph and fills it with NULLs
        graph(int h){
            this->h = h;
            array = new alist [h];
            for (int i = 0; i < h; ++i)
                array[i].head = NULL;
        }

        //Creates a new node by calling instance newlist of listnode
        listnode* newlist(int dest){
            listnode* newnode = new listnode;
                newnode->dest = dest;
                    newnode->next = NULL;
            return newnode;
        }

        //Connects the node of the Graph
        void insert(int src, int dest, int weight){

            listnode* newnode = newlist(dest);
                newnode->next = array[src].head;
                    array[src].head = newnode;
                        newnode = newlist(src);
                            newnode = newlist(weight);
                                newnode->next = array[dest].head;

        }

        //Removes a node from the graph
        void remove(int srcr, int destr){

            for (int i = 0; i < h; ++i){

                listnode* move = array[i].head;
                while (move){
                    if(destr == move->dest){
                        if(srcr==i)
                            array[i].head = NULL;
                    }
                move = move->next;
                }                 
            }
        }

        //Prints the Graph      
        void print(){

            for (int i = 0; i < h; ++i){

                listnode* move = array[i].head;
                while (move){
                    cout<<"("<<i <<","<< move->dest<< ","<< move->weight <<")";
                    move = move->next;
                }  
            }
            cout<<endl;
        }
};

node.h

//node class file
using namespace std;
#include "list.h"

//Nodes
class listnode
{
    public:
    int dest;
    int weight;
    class listnode* next;
};

list.h

//list class file
using namespace std;
#include <iostream>
#include <cstdlib>

//Adjacency List
class alist
{
    public:
    class listnode *head;
};

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题。

首先,永远不要在构造函数中使用单元化变量:

int src,dest,wght,size=10;   // put an intial value for size
                  ^^^^^^^^
graph A(size);               // because your constructor used an unitialised size so far

然后你必须检查你的create命令的实现,因为目前你这样做:

    if (command == "create"){
        cin >> size;
        graph A(size);    // this is a local object of the if bloc 
    }                     // which gets destroyed when you reach this brace !!!!!  

例如,您可以实现resize()函数并调用它,而不是重新创建一个立即消失的新图形。

然后使用正确的参数调用insert()函数,但不更新节点的权重。请参阅此处的提示:

void insert(int src, int dest, int weight){  
    listnode* newnode = newlist(dest); 
    newnode->next = array[src].head; 
    newnode->weight = weight;      // <========= Update your node weight with this...  
    array[src].head = newnode;  
    newnode = newlist(src); 
    //newnode = newlist(weight);   // <========  But what's this ?  Remove it ! 
    newnode->next = array[dest].head;
}

经过这些修正后,它可以正常工作:命令“insert 3 2 1”后跟“print”将导致正确的结果。

尽管如此,我强烈建议使用标准库list,这将使您的生活更轻松!