| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 376 人关注过本帖
标题:<数据结构>图存储问题,求高手指点
只看楼主 加入收藏
realm
Rank: 1
等 级:新手上路
帖 子:12
专家分:1
注 册:2010-11-15
结帖率:50%
收藏
已结贴  问题点数:30 回复次数:3 
<数据结构>图存储问题,求高手指点
我这个邻接表图存储有问题,不知道是哪里的问题,是顶点没存对还是边没存对?
*****************************************************************************
#include<iostream>
using namespace std;

#define INFINITY 65535
typedef enum{UNVISITED,VISITED};

/*******************邻接表图*********************************************************************************************/
//边类
class Edge
{
public:
    int from,to,weight;
    Edge()
    {
        from = -1;to = -1;weight = 0;
    }
    Edge(int f,int t,int w)
    {
        from = f;to = t;weight = w;
    }
};
//图基类
class Graph
{
public:
    int numVertex;//顶点数量
    int numEdge;//边的数量
    int * Mark;//顶点是否已被游历过
    int * Indegree;//顶点入度
    int * VertexName;//顶点编号
    Graph(int numVert)
    {
        numVertex = numVert;
        numEdge = 0;
        Mark       = new int[numVertex];      
        Indegree   = new int[numVertex];        
        VertexName = new int[numVertex];         
        for(int i = 0;i < numVertex;i ++)
        {
            Mark[i] = UNVISITED;
            Indegree[i] = 0;
            VertexName[i] = -1;
        }
    } //构造

    ~Graph()
    {
        delete[] Mark;
        delete[] Indegree;
        delete[] VertexName;
    }//析构

    bool IsEdge(Edge oneEdge)
    {
        if((oneEdge.weight > 0)&&(oneEdge.weight < INFINITY)&&(oneEdge.to >= 0))
            return true;
        else return false;
    }//是不是一条边

    void setVertex(Graph & G)
    {   
        for(int i = 0;i <= 9;i++)
            G.VertexName[i] = i;
        return;
    }//设置顶点编号
    /*****************


    貌似上面这里有问题


    ******************/
};


//以下为邻接表类图
struct listUnit
{
    int end_point;
    int weight;
};

template<class Elem>
class Link
{
public:
    Elem element;
    Link * next;
    Link(const Elem& elemval,Link *nextval = NULL)
    {
        element = elemval;
        next = nextval;
    }
    Link(Link * nextval = NULL)
    {
        next = nextval;
    }
};//构造

template<class Elem>
class LList
{
public:
    Link<Elem> * head;
    LList()
    {
        head = new Link<Elem>();//创建头指针以便于操作邻接表
    }
};
//以上为邻接表类


class Graphl:public Graph
{
private:
    LList<listUnit> * graList;
public:
   

    Graphl(int numVert):Graph(numVert)
    {
        graList = new LList<listUnit>[numVertex];//设置邻接表存储图
    }

    int ToVertex(Edge currEdge)
    {
        return currEdge.to;//返回边的终点
    }

    Edge FirstEdge(int oneVertex)
    {
        Edge myEdge;
        myEdge.from = oneVertex;
        Link<listUnit> * temp = graList[oneVertex].head;
        if(temp -> next != NULL)
        {
            myEdge.to = temp -> next -> element.end_point;
            myEdge.weight = temp -> next -> element.weight;
        }
        return myEdge;
    }//返回该顶点的第一条边

    Edge NextEdge(Edge preEdge)
    {
        Edge myEdge;
        myEdge.from = preEdge.from;
        Link<listUnit> * temp = graList[preEdge.from].head;
        while((temp -> next != NULL)&&(temp -> next -> element.end_point <= preEdge.to))
            temp = temp -> next;
        if(temp -> next != NULL)
        {
            myEdge.to = temp -> next -> element.end_point;
            myEdge.weight = temp -> next ->element.weight;
        }
        return myEdge;
    }//返回与改变相邻的边

    void setEdge(int from,int to,int weight)
    {
        Link<listUnit> * temp = graList[from].head;
        while((temp -> next != NULL)&&(temp -> next ->element.end_point < to))
            temp = temp -> next;
        if(temp -> next == NULL)
        {
            temp -> next = new Link<listUnit>;
            temp -> next -> element.end_point = to;
            temp -> next -> element.weight = weight;
            numEdge ++;
            Indegree[to] ++;
            return;
        }
        if(temp -> next -> element.end_point == to)
        {
            temp -> next -> element.weight = weight;
            return;
        }
        if(temp -> next -> element.end_point > to)
        {
            Link<listUnit> * other = temp -> next;
            temp -> next = new Link<listUnit>;
            temp -> next -> element.end_point = to;
            temp -> next -> element.weight = weight;
            temp -> next -> next = other;
            numEdge ++;
            Indegree[to] ++;
            return;
        }
    }//设置一条边

    void setAllEdge(Graphl & G)
    {
        G.setEdge(0,1,5);G.setEdge(0,2,6);
        G.setEdge(1,3,3);
        G.setEdge(2,3,6);G.setEdge(2,4,3);
        G.setEdge(3,4,3);G.setEdge(3,5,4);G.setEdge(3,6,5);
        G.setEdge(4,6,1);G.setEdge(4,7,4);
        G.setEdge(5,9,4);
        G.setEdge(6,8,5);
        G.setEdge(7,8,2);
        G.setEdge(8,9,2);
        return;
    }//设置边
}
搜索更多相关主题的帖子: 数据结构 
2010-11-23 14:38
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:15 
代码好长呢......

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2010-11-25 10:25
fightingsss
Rank: 6Rank: 6
等 级:侠之大者
帖 子:97
专家分:471
注 册:2010-11-12
收藏
得分:15 
好多啊。。。
2010-11-26 18:27
realm
Rank: 1
等 级:新手上路
帖 子:12
专家分:1
注 册:2010-11-15
收藏
得分:0 
不给力啊
2010-12-07 08:29
快速回复:<数据结构>图存储问题,求高手指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020221 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved