| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 401 人关注过本帖
标题:以十字链表为存储方式的图中删除一个顶点 运行老出错 求高手帮忙啊
只看楼主 加入收藏
麦迪依然
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-3-22
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:3 
以十字链表为存储方式的图中删除一个顶点 运行老出错 求高手帮忙啊
#include<iostream>
using namespace std;
#include<string>
#define Max_Vertex_Num 20
typedef char elemtype;
typedef struct ArcNode
{
    int tailvex,headvex;
    ArcNode *hlink,*tlink;
}ArcBox;
struct VexNode
{
    elemtype data;
    ArcBox *firstin,*firstout;
};
struct OLGarph
{
    VexNode hlist[Max_Vertex_Num];
    int vexnum,arcnum;
};

int Locate(OLGarph &G,elemtype v)//顶点位置定位
{
    for(int i=0;i<G.vexnum;i++)
        if(G.hlist[i].data==v)
            return i;
    exit(0);
}

void Create_OLG( OLGarph &G)//以十字链表方式建立图
{
    cout<<"请输入有向图的顶点数:";
    cin>>G.vexnum;
    cout<<"请输入有向图的边数:";
    cin>>G.arcnum;
    int i;
   
    for(i=0;i<G.vexnum;i++)
    {
        cout<<"请输入各顶点的data值:";
        cin>>G.hlist[i].data;
        G.hlist[i].firstin=G.hlist[i].firstout=NULL;
    }
    cout<<"输入边时弧尾在前弧头之后.\n";\
    for(i=0;i<G.arcnum;i++)
    {
        cout<<"请输入各有向边所依附的顶点:";
        elemtype v1,v2;
        cin>>v1>>v2;
        int m,n;
        m=Locate(G,v1);
        n=Locate(G,v2);
        ArcNode *p=new ArcNode;
        p->tailvex=m;
        p->headvex=n;
        p->hlink=G.hlist[n].firstin;
        p->tlink=G.hlist[m].firstout;
        G.hlist[m].firstout=G.hlist[n].firstin=p;
    }
}

void InitNULL_1(OLGarph &G,int m,int n)
{
    ArcNode *p1,*p2;
    p1=G.hlist[m].firstin;
    while(p1->tailvex!=n)
    {
        p2=p1;
        p1=p1->hlink;
    }
    p2->hlink=p1->hlink;
}

void InitNULL_2(OLGarph &G,int m,int n)
{
    ArcNode *p1,*p2;
    p1=G.hlist[m].firstout;
    while(p1->headvex!=n)
    {
        p2=p1;
        p1=p1->tlink;
    }
    p2->tlink=p1->tlink;
}

void deleteout(OLGarph &G,int i)//删除以此顶点为弧尾的边
{
    ArcNode *p1,*p2;
    p1=G.hlist[i].firstout;
    while(p1)
    {
        p2=p1;
        p1=p1->tlink;
        InitNULL_1(G,p2->headvex,i);
        delete p2;
        G.arcnum--;   
    }

}

void deletein(OLGarph &G,int i)//删除以此顶点为弧头的边
{
    ArcNode *p1,*p2;
    p1=G.hlist[i].firstin;
    while(p1)
    {
        p2=p1;
        p1=p1->hlink;
        InitNULL_2(G,p2->tailvex,i);
        delete p2;
        G.arcnum--;
    }
}
void Delete_OLG(OLGarph &G)
{
    ArcNode *p,*q;
    elemtype key;
    cout<<"请输入您要删除的结点的data值:";
    cin>>key;
    int i=Locate(G,key);
    deleteout(G,i);//删除以此顶点为弧尾的边
    deletein(G,i);//删除以此顶点为弧头的边
    for(int j=i;i<G.vexnum;j++)
    {
       G.hlist[j]=G.hlist[j+1];
       for(p=G.hlist[j].firstin;p;p=p->hlink)
          p->headvex--;
       for(p=G.hlist[j].firstout;p;p=p->tlink)
           p->tailvex--;
    }
    G.vexnum--;
}

void Print_OLG(OLGarph G)
{
    cout<<"该图所有的顶点为:\n";
    for(int i=0;i<G.vexnum;i++)
        cout<<G.hlist[i].data<<' ';
    cout<<"该图所有的边为:\n";
     for(i=0;i<G.vexnum;++i)
    {   
        ArcNode *p=G.hlist[i].firstout;
        while(p)
        {
            cout<<G.hlist[i].data << "-->";
            cout<<G.hlist[p->headvex].data << endl;
            p = p->tlink;
        }
    }         
}

int main()
{
    OLGarph G;
    Create_OLG( G);
    Print_OLG(G);
    Delete_OLG(G);
    Print_OLG(G);
    cout<<endl;
    return 0;
}
搜索更多相关主题的帖子: include return 
2012-05-08 13:14
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
十字链表 在创建图(有向图)方式不正确
2012-05-10 23:07
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @概  述:以十字链表为存储方式的图

 * @文件名:j.c

 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define MAX_VERTEX_NUM    (20)//图中最大的顶点数
typedef char elem_type;

//弧属性结构体
typedef struct _arc_node
{
    int tail_vex;//弧尾索引值
    int head_vex;//弧头索引值
    struct _arc_node *arc_head_next;//指向弧头
    struct _arc_node *arc_tail_next;//指向弧尾
}arc_node, *ptr_arc_node;
//顶点属性结构体
typedef struct _vex_node
{
    elem_type data;//顶点显示的数据值
    ptr_arc_node arc_head_next;//指向弧头
    ptr_arc_node arc_tail_next;//指向弧尾
}vex_node, *ptr_vex_node;
//有向图属性结构
typedef struct _OLGraph
{
    vex_node head_array[MAX_VERTEX_NUM];//链表头数组
    int vex_num;//顶点数
    int arc_num;//弧的个数
}OLGraph;

//在list单链表中插入p结点 相同的弧尾  不同的弧头
ptr_arc_node head_insert(ptr_arc_node list, ptr_arc_node p)
{
    if (NULL == list)
    {//直接插入到后面
        p->arc_head_next = list;
        list = p;
    }
    else
    {//找到位置后才插入
        ptr_arc_node fp = list;//
        ptr_arc_node tp = list->arc_head_next;
        //插入的位置 顶点下标比p大的前面
        while (NULL != tp)
        {
            if (tp->head_vex < p->head_vex)
            {
                fp = tp;
                tp = tp->arc_head_next;
            }
            else
            {
                break;
            }
        }
   
        if ((fp == list) && (fp->head_vex > p->head_vex))
        {
            p->arc_head_next = list;
            list = p;
        }
        else
        {
            p->arc_head_next = fp->arc_head_next;
            fp->arc_head_next = p;
        }
    }
    return list;
}

//在list单链表中插入p结点 相同的弧头 不同的弧尾
ptr_arc_node tail_insert(ptr_arc_node list, ptr_arc_node p)
{
    if (NULL == list)
    {//直接插入到后面
        p->arc_tail_next = list;
        list = p;
    }
    else
    {//找到位置后才插入
        ptr_arc_node fp = list;//
        ptr_arc_node tp = list->arc_tail_next;
        //插入的位置 顶点下标比p大的前面
        while (NULL != tp)
        {
            if (tp->tail_vex < p->tail_vex)
            {
                fp = tp;
                tp = tp->arc_tail_next;
            }
            else
            {
                break;
            }
        }
        if ((fp == list) && (fp->tail_vex > p->tail_vex))
        {
            p->arc_tail_next = list;
            list = p;
        }
        else
        {
            p->arc_tail_next = fp->arc_tail_next;
            fp->arc_tail_next = p;
        }
    }
    return list;
}

//顶点位置定位
int get_pos(OLGraph g, elem_type v)
{
    int i;
    for (i=0; i<g.vex_num; ++i)
    {
        if (g.head_array[i].data == v)
        {
            return i;
        }
    }
    exit(0);
}

//创建图
OLGraph * create_g(OLGraph *g)
{
    int i;

    g = malloc(sizeof(OLGraph));
    assert(NULL != g);
    printf ("请输入有向图的顶点数:");
    scanf("%d", &(g->vex_num));
    printf ("请输入有向图的边数:");
    scanf("%d", &(g->arc_num));
    fflush(stdin);
    for (i=0; i<g->vex_num; ++i)
    {
        printf ("请输入各顶点的data值:");
        scanf("%c", &(g->head_array[i].data));
        fflush(stdin);
        g->head_array[i].arc_head_next = NULL;
        g->head_array[i].arc_tail_next = NULL;
    }
    printf ("输入边时弧尾在前弧头之后.\n");
    for (i=0; i<g->arc_num; ++i)
    {
        elem_type hv, tv;
        int hv_pos, tv_pos;
        ptr_arc_node p = NULL;
        printf ("请输入各有向边所依附的顶点:");
        scanf("%c,%c", &tv, &hv);
        fflush(stdin);

        tv_pos = get_pos(*g, tv);
        hv_pos = get_pos(*g, hv);
        //创建弧结构
        p = malloc (sizeof(arc_node));
        assert(NULL != p);
        p->tail_vex = tv_pos;
        p->head_vex = hv_pos;
        //修改图
        g->head_array[tv_pos].arc_head_next =
            head_insert(g->head_array[tv_pos].arc_head_next, p);
        g->head_array[hv_pos].arc_tail_next =
            tail_insert(g->head_array[hv_pos].arc_tail_next, p);
    }
    return g;
}

//打印输出
void print_g(OLGraph g)
{
    int i;
    for (i=0; i<g.vex_num; ++i)
    {
        ptr_arc_node p = g.head_array[i].arc_head_next;
        while (NULL != p)
        {
            printf ("<%c, %c>\n",
                g.head_array[p->tail_vex].data,
                g.head_array[p->head_vex].data);
            p = p->arc_head_next;
        }
    }
}

int main(int argc, char *argv[])
{
    OLGraph *G = NULL;//定义图指针

    //创建图
    G = create_g(G);
    //输出图
    print_g(*G);

    return 0;
}
2012-05-11 07:07
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
删除可以仿照插入的模式实现
2012-05-11 07:19
快速回复:以十字链表为存储方式的图中删除一个顶点 运行老出错 求高手帮忙啊
数据加载中...
 
   



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

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