| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 381 人关注过本帖
标题:求助!我的图的邻接链表构建与删除哪错了????
只看楼主 加入收藏
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
结帖率:70.59%
收藏
已结贴  问题点数:20 回复次数:1 
求助!我的图的邻接链表构建与删除哪错了????
语法上都过了,算法上还有问题,请高手帮忙!!!



#include<stdio.h>
#include<stdlib.h>
#define MAX 20
struct node
{ int position;
struct node*next;
};
struct Vnode
{
    int data;
    struct node*first;
};
struct graph
{ int vex;
int arc;
struct Vnode S[MAX];
};
void creat(struct graph&M)
{
   
    int i;
    printf("请输入点个数\n");
    scanf("%d",&M.vex);
    printf("请输入边数\n");
    scanf("%d",&M.arc);
    getchar();
    for(i=0;i<M.vex;i++)
    {
        printf("请输入第%d个点\n",i+1);
        scanf("%c",&M.S[i].data);
        getchar();
        printf("节点%c",M.S[i].data);
        M.S[i].first=NULL;
        
    }
}
void insert(struct graph &t)
{
    struct node*q;
        char ch;
    int i,j,p;
    for(i=0;i<t.vex;i++)
    {
        printf("请输入与%c组成边的头节点\n",t.S[i].data);
    while(((ch=getchar())!='\n'))
        
    { for(j=0;j<t.vex;++j)

    {
       printf("ch的值%c\n",ch);
       printf("t的值%c\n",t.S[j].data);
        if(ch==t.S[j].data)就是这句话,很简单的一个判等,明明相等,他居然不执行!!!直接执行后面的了,为什么啊?
        {   p=j;
        break;}
    }
    q=(struct node*)malloc(sizeof(struct node));
    q->position=p;
    q->next=t.S[j].first;

    t.S[j].first=q;
    printf("%d",q->position);
    }
    }
}
void delet(struct graph &t)
{
    struct node*p,*q;
    int i,s,w;
    char tail,head;
    printf("请输入要删除的边,如a->b\n");
    scanf("%c->%c",&tail,&head);
    for(i=0;i<t.vex;i++)
    {
        if(tail==t.S[i].data)
            w=i;
        if(head==t.S[i].data)
            s=i;
        break;
    }
    p=q=t.S[w].first;
    while(p!=NULL)
    {
        if(p->position!=s)
        {
            p=q;
            p=p->next;
        }
        else
        {
            q->next=p->next;
            free(p);
        }
    }
}
void print(struct graph t)
{
    int i;
    struct node*p;
    for(i=0;i<t.vex;i++)
    {    p=t.S[i].first;
    while(p!=NULL)
    {    printf("%c->%c",t.S[i].data,t.S[p->position].data);
    p=p->next;
    }
    printf("\n");
    }
}
void main()
{
    struct graph T;
    int i,j;
    creat(T);
    insert(T);
    print(T);
    printf("请输入删出边的条数\n");
    scanf("%d",&i);
    for(j=0;j<i;j++)
        delet(T);
    print(T);
}
搜索更多相关主题的帖子: 链表 构建 删除 
2010-12-02 21:17
fightingsss
Rank: 6Rank: 6
等 级:侠之大者
帖 子:97
专家分:471
注 册:2010-11-12
收藏
得分:20 
似乎有点长。。。不知道
2010-12-04 22:51
快速回复:求助!我的图的邻接链表构建与删除哪错了????
数据加载中...
 
   



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

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