Đổi đồ thị vô hướng thành có hướng

Cho em hỏi muốn đổi đồ thị vô hướng thành có hướng trong code này thì làm như thế nào ạ?

#include<stdio.h>
#include<stdlib.h>
struct AdjListNode
{
    int value;
    struct AdjListNode *next;
    struct AdjListNode *prev;
};
//-------------------------------------------
typedef struct AdjListNode AdjListNode;
struct AdjList
{
    AdjListNode *head;
    AdjListNode *tail;
};
typedef struct AdjList AdjList;
//---------------------------------------------
struct Graph
{
    int v;
    AdjList* array;
};
typedef struct Graph Graph;
//---------------------------------------------
AdjListNode* NewAdjListNode(int value)
{
    AdjListNode* newNode =(AdjListNode*)malloc(sizeof(AdjListNode));
    newNode->value=value;
    newNode->prev = NULL;
    newNode->next=NULL;
    return newNode;
}
Graph* CreateGraph(int v)
{
    Graph* graph=(Graph*)malloc(sizeof(Graph));
    graph->v=v;
    graph->array=(AdjList*)malloc(20*sizeof(AdjList));
    for(int i=0;i<v;i++)
    {
        graph->array[i].head=NULL;

    }
    return graph;
}

void PrintGraph(Graph* graph)
{
    for(int i=1;i<=graph->v;i++)
    {
        AdjListNode* pCrawl=graph->array[i].head;
        printf("danh sach ke cua node %d\n head",i+1);
        while(pCrawl!=NULL)
        {
            printf("->%d",pCrawl->value);
            pCrawl=pCrawl->next;
        }
        printf("\n");
    }
}

void InsertEdge(Graph* graph,int source,int destination)
{
    AdjListNode* newNode=NewAdjListNode(destination);
    newNode->next=graph->array[source].head;
    graph->array[source].head=newNode;
    newNode=NewAdjListNode(source);
    newNode->next=graph->array[destination].head;
    graph->array[destination].head=newNode;
}

void InsertNode(int v,Graph* graph)
{
    int x,node,count =0;
    printf("node %d ke voi bao nhieu node\n",v+1);
    scanf("%d",&x);
    while(count<x)
    {
        printf("node %d ke voi node nao?\n",v+1);
        scanf("%d",&node);
        InsertEdge(graph,v+1,node);
        count++;
    }
    graph->v++;
}



int main()
{
    int v,e;
    int source,destination;
    printf("do thi co bao nhieu dinh?\n");
    scanf("%d",&v);
    Graph* graph=CreateGraph(v);
    printf("do thi co bao nhieu canh\n");
    scanf("%d",&e);
    for(int i=0;i<e;i++)
    {
        printf("canh thu %d:\t",i+1);
        scanf("%d %d",&source,&destination);
        InsertEdge(graph,source,destination);
    }
    PrintGraph(graph);
    InsertNode(v,graph);
    v++;
    PrintGraph(graph);

    return 0;
}

Đọc nguyên câu hỏi mà không hểu gì.

  • Đổi đồ thị?
  • "file này" là file nào?
  • Đổi dựa trên quy tắc nào?
4 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?