Evaluating bids

Implementação grafos lista de adjacência em C

Published on the April 02, 2017 in IT & Programming

About this project

Open

Gostaria que ambos os códigos fossem corrigidos para que compilassem e rodassem com sucesso

Código 1: Matriz de adjacência

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define V 5


void inicializar (int *g){

    int i,j;
    for (i=1; i<=V; i++)
        for(j=1;j<=V; j++)
            g[i][j]=0;
}
int inserir (int *g, int i, int j){
        g[i][j]=1;
        return 0;
}

void imprimir(int*g){
    for(int i=1; i<=V; i++){
        for(int j=1; j<=V; j++){
            printf("%d", g[i][j]);
        }
    }

}

int main()
{
    int g[V+1][V+1];
    inicializar(*g);
    inserir(*g, 2, 3);
    imprimir(*g);
    return 0;

}


____________________________________________


Código 2: Lista de adjacência

#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>

#define null ((void *) 0)
#define V 5

typedef struct aux{
    int v;
    struct aux * prox;
    struct aux * ant;
}No;


typedef struct s{
    int flag;
    No * inicio;
}Vertice;


void inicializar (Vertice *g){
  int i;
    for (i=1; i<=V; i++){
        g[i].inicio=null;
    }
}

bool arestaExiste(Vertice *g,int i, int j){
    No * p = g[i].inicio;
    while(p){
        if(p->v ==j) return true;
        p=p->prox;
    }
    return false;
}

void inserirAresta (Vertice *g, int j, int i){
    if (arestaExiste(g, j, i)) return;
    No * novo = (No *) malloc(sizeof(No));
    novo->v=j;
    novo->prox = g[i].inicio;
}

void imprimir (Vertice *g){
    int i;
      for (i=1; i<=V; i++){
            printf("%d", i);
        No * p = g[i].inicio;
        printf("%d",p->v);
      while(p){

          p=p->prox;

        }
    }

}


int main()
{

    Vertice * g = (Vertice *) malloc (sizeof(Vertice) * (V+1));

    inicializar(g);
    inserirAresta (g, 1, 50);
    inserirAresta (g, 2, 99);
    inserirAresta (g, 3, 40);
    inserirAresta (g, 4, 60);
    imprimir(g);
    system("pause");
}

Category IT & Programming
Subcategory Web development
What is the scope of the project? Small change or bug
Is this a project or a position? Project
Required availability As needed
Roles needed Developer

Delivery term: Not specified

Skills needed