| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 779 人关注过本帖
标题:邻接表的输出问题~~~怎么解决
取消只看楼主 加入收藏
yangliangbin
Rank: 2
来 自:广西
等 级:论坛游民
帖 子:66
专家分:62
注 册:2009-6-3
结帖率:60%
收藏
 问题点数:0 回复次数:0 
邻接表的输出问题~~~怎么解决
#include<iostream>
using namespace std;
typedef int elemtype;
const int n=5;
const int e=8;

struct graph
{
    elemtype V[n+1];
    int arcs[n+1][n+1];
};
struct link
{
    elemtype data;
    float w;
    link *next;
};
struct node
{
    elemtype v;
    link *next;
}a[n+1];

void create1(graph &g)//建立无向图的邻接矩阵
{
    int i,j,k;
    cout<<"请输入顶点:";
    for(k=1;k<=n;k++)
        cin>>g.V[k];
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            g.arcs[i][j]=0;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"输入第"<<k<<"条边:";
        cin>>i>>j;
        g.arcs[i][j]=1;
        g.arcs[j][i]=1;
    }
}

void create2(graph &g)//建立有向图的邻接矩阵
{
    int i,j,k;
    cout<<"请输入顶点:";
    for(k=1;k<=n;k++)
        cin>>g.V[k];
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            g.arcs[i][j]=0;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"输入第"<<k<<"条边:";
        cin>>i>>j;
        g.arcs[i][j]=1;
    }
}

void create3(graph &g)//建立无向网的邻接矩阵
{
    int i,j,k;
    float w;
    cout<<"请输入顶点:";
    for(k=1;k<=n;k++)
        cin>>g.V[k];
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            if(i==j)
                g.arcs[i][j]=0;
            else
                g.arcs[i][j]=111;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"输入第"<<k<<"条边及权值:";
        cin>>i>>j>>w;
        g.arcs[i][j]=w;
        g.arcs[j][i]=w;
    }
}

void create4(graph &g)//建立有向网的邻接矩阵
{
    int i,j,k;
    float w;
    cout<<"请输入顶点:";
    for(k=1;k<=n;k++)
        cin>>g.V[k];
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            if(i==j)
                g.arcs[i][j]=0;
            else
                g.arcs[i][j]=111;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"输入第"<<k<<"条边及权值:";
        cin>>i>>j>>w;
        g.arcs[i][j]=w;
    }
}

void print1(graph g)//输出邻接矩阵
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cout<<g.arcs[i][j]<<" ";
        cout<<endl;
    }
}

void create5()//建立无向图的邻接表
{
    int i,j,k;
    link *s;
    for(i=1;i<=n;i++)
    {
        a[i].v=i;
        a[i].next=NULL;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"请输入"<<k<<"条边的两结点:";
        cin>>i>>j;

        s=new link;
        s->data=j;
        s->next=a[i].next;
        a[i].next=s;

        s=new link;
        s->data=i;
        s->next=a[j].next;
        a[j].next=s;
    }
}

void create6()//建立有向图的邻接表
{
    int i,j,k;
    link *s;
    for(i=1;i<=n;i++)
    {
        a[i].v=i;
        a[i].next=NULL;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"请输入"<<k<<"条边的两结点:";
        cin>>i>>j;

        s=new link;
        s->data=j;
        s->next=a[i].next;
        a[i].next=s;
    }
}

void create7()//建立无向网的邻接表
{
    float w;
    int i,j,k;
    link *s;
    for(i=1;i<=n;i++)
    {
        a[i].v=i;
        a[i].next=NULL;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"输入第"<<k<<"条边的两结点及边上权值:";
        cin>>i>>j>>w;
        s=new link;

        s->data=j;
        s->w=w;
        s->next=a[i].next;
        a[i].next=s;

        s->data=i;
        s->w=w;
        s->next=a[j].next;
        a[j].next=s;
    }
}
        
void create8(P)//建立有向网的邻接表
{
    float w;
    int i,j,k;
    link *s;
    for(i=1;i<=n;i++)
    {
        a[i].v=i;
        a[i].next=NULL;
    }
    for(k=1;k<=e;k++)
    {
        cout<<"输入第"<<k<<"条边的两结点及边上权值:";
        cin>>i>>j>>w;
        s=new link;

        s->data=j;
        s->w=w;
        s->next=a[i].next;
        a[i].next=s;

    }
}

void print2(P)//输出邻接表
{
    node *p;
    for(int i=1;i<=n;i++)
    {
        p=new node;
        p=&a[i];
        while(p->next!=NULL)
        {
            cout<<p->v<<" ";
            p=p->next ;            
        }
        cout<<p->v<<endl;
    }
}
int main()
{
    graph G;
    node *P;
    int i,j,k;
    do
    {
        cout<<"\n\n";
        cout<<"\t\t _________________图论_________________"<<endl;
        cout<<"\t\t ______________________________________"<<endl;
        cout<<"\t\t为方便验证,设置顶点为5,边值为6,输入 "<<endl;
        cout<<"\t\t前最好画出图或网以及它们的邻接矩阵、邻 "<<endl;
        cout<<"\t\t接表以方便输入和验证                   "<<endl;
        cout<<"\t\t ______________________________________"<<endl;
        cout<<"\t\t|    1.建立无向图的邻接矩阵           |"<<endl;
        cout<<"\t\t|    2.建立有向图的邻接矩阵           |"<<endl;
        cout<<"\t\t|    3.建立无向网的邻接矩阵           |"<<endl;
        cout<<"\t\t|    4.建立有向网的邻接矩阵           |"<<endl;
        cout<<"\t\t|    5.建立无向图的邻接表             |"<<endl;
        cout<<"\t\t|    6.建立有向图的邻接表             |"<<endl;
        cout<<"\t\t|    7.建立无向网的邻接表             |"<<endl;
        cout<<"\t\t|    8.建立有向网的邻接表             |"<<endl;
        cout<<"\t\t|    0.Exit System                    |"<<endl;
        cout<<"\t\t|_____________________________________|"<<endl;
        cout<<"\t\t请输入选项:";
        cin>>k;
        switch(k)
        {
        case 1:
            create1(G);
            print1(G);
            system("pause");
            break;

        case 2:
            create2(G);
            print1(G);
            system("pause");
            break;

        case 3:   
            create3(G);
            print1(G);
            system("pause");
            break;

        case 4:
            create4(G);
            print1(G);
            system("pause");
            break;
        case 5:
            create5();
            print2();
            system("pause");
            break;
        case 6:
            create6();
            print2();
            system("pause");
            break;
        case 7:
            create7();
            print2();
            system("pause");
            break;
        case 8:
            create8();
            print2();
            system("pause");
            break;
        case 0:
            break;
        }
    }while(k!=0);
    return 0;
}






搜索更多相关主题的帖子: 输出 
2009-12-01 22:28
快速回复:邻接表的输出问题~~~怎么解决
数据加载中...
 
   



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

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