有向圖的十字鏈表存儲方法

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#define MAXSIZE 100
//十字鏈表中變表的弧節點結構
typedef struct node
{
    int tailvex;            //代表邊的起始下標(弧尾)
    int headvex;            //代表邊的有箭頭的下標(弧頭)
    int *info;              //代表邊的權值
    struct node *hlink;     //指向弧尾相同的下一條弧
    struct node *thilnk;    //指向弧頭相同的下一條弧
}ArcBox;
//十字鏈表中頂點表的節點結構
typedef struct
{
    char vertex;            //頂點值域
    ArcBox *firstin;        //弧頭的第一個節點
    ArcBox *firstout;       //弧尾的第一個節點
}VertexNode;
void Create(VertexNode g[],int n,int e)
{
    ArcBox *p;
    int i,j,k;
    printf("請輸入每個點的值:\n");
    i=0;
    while(i<n)
    {
        fflush(stdin);
        scanf("%c",&g[i].vertex);//輸入字母時候要輸入一個按回車鍵
        g[i].firstin=NULL;
        g[i].firstout=NULL;
        i++;
    }
    //頭插法
    for(k=0;k<e;k++)
    {
        printf("邊的兩個下標:");
        scanf("%d%d",&i,&j);
        p=(ArcBox*)malloc(sizeof(ArcBox));
        p->headvex=j;
        p->tailvex=i;
        p->info=NULL;
        p->thilnk=g[i].firstout;
        p->hlink=g[j].firstin;
        g[i].firstout=p;
        g[j].firstin=p;
    }
}
void DisplayGraphic(VertexNode g[],int n)
{
    int i;
    ArcBox *p;
    for(i=0;i<n;i++)
    {
        p=g[i].firstin;
        printf("%c做頭:",g[i].vertex);
        while(p!=NULL)
        {
            printf("%c->%c\t",g[p->tailvex].vertex,g[i].vertex);
            p=p->hlink;
        }
        p=g[i].firstout;
        printf("\n%c做尾:",g[i].vertex);
        while(p)
        {
            printf("%c->%c\t",g[i].vertex,g[p->headvex].vertex);
            p=p->thilnk;
        }
        printf("\n");
    }
}
int main()
{
    VertexNode g[MAXSIZE];
    Create(g,5,6);
    DisplayGraphic(g,5);
    return 0;
}

字符的輸入那裏還是有點問題,只能按照我寫的那種方法輸入,不知道怎麼可以改成一次輸入完,按回車鍵就可以,看到的可以給我說一下,不勝感激。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章