O projeto completo entrega verificadores de Sequencia e GrafoMatriz. Na sequência, salva tamanho válido, faz tamanho=capacidade+1, diagnostica0 e restaura explicitamente antes de consultar. No grafo não dirigido, salva BA, zera apenas essa posição, detecta assimetria e restaura antes de seguir. Os diagnósticos são somente leitura; a restauração pertence ao teste. Não executam uma operação comum no estado propositalmente inválido e não prometem validar endereços arbitrários. As implementações e o caso completo acompanham a solução.
diagnosticos_demo.c
#include "sequencia.h"
#include "grafos.h"
#include <stdio.h>
int main(void)
{
Sequencia s;
GrafoMatriz g;
Pedido primeiro;
size_t tamanho_salvo;
int antes, quebrada, restaurada, salvo;
seq_iniciar(&s);
if (seq_inserir(&s, 0, (Pedido){2, 3}) != SEQ_OK) {
printf("Reserva inicial recusada: tamanho=%zu capacidade=%zu.\n",
s.tamanho, s.capacidade);
seq_destruir(&s);
return 1;
}
if (seq_inserir(&s, 1, (Pedido){5, 1}) != SEQ_OK) {
if (seq_consultar(&s, 0, &primeiro) != SEQ_OK) {
seq_destruir(&s);
return 2;
}
printf("Crescimento recusado: tamanho=%zu primeiro=%d.\n",
s.tamanho, primeiro.id);
seq_destruir(&s);
return 1;
}
antes = seq_invariante(&s);
tamanho_salvo = s.tamanho;
s.tamanho = s.capacidade + 1;
quebrada = seq_invariante(&s);
s.tamanho = tamanho_salvo;
restaurada = seq_invariante(&s);
if (seq_consultar(&s, 0, &primeiro) != SEQ_OK) {
seq_destruir(&s);
return 2;
}
printf("Sequencia: antes=%d quebrada=%d restaurada=%d primeiro=%d.\n",
antes, quebrada, restaurada, primeiro.id);
if (gm_iniciar(&g, 3, 0) != GR_OK ||
gm_adicionar(&g, 0, 1, 1) != GR_OK) {
seq_destruir(&s);
return 2;
}
antes = gm_conferir(&g);
salvo = g.aresta[1][0];
g.aresta[1][0] = 0;
quebrada = gm_conferir(&g);
g.aresta[1][0] = salvo;
restaurada = gm_conferir(&g);
printf("Matriz: antes=%d assimetrica=%d restaurada=%d AB=%d.\n",
antes, quebrada, restaurada, gm_estado(&g, 0, 1));
puts("Diagnosticos nao validam enderecos arbitrarios.");
seq_destruir(&s);
return 0;
}
grafos.c
#include "grafos.h"
#include <stddef.h>
#include <stdlib.h>
static int configuracao(int n, int dirigido)
{
return n >= 0 && n <= GR_CAP && (dirigido == 0 || dirigido == 1);
}
static int indice(int n, int v)
{
return v >= 0 && v < n;
}
static int forma_m(const GrafoMatriz *g)
{
return g != NULL && configuracao(g->vertices, g->dirigido);
}
static int forma_l(const GrafoLista *g)
{
return g != NULL && configuracao(g->vertices, g->dirigido);
}
int gm_iniciar(GrafoMatriz *g, int n, int dirigido)
{
if (g == NULL || !configuracao(n, dirigido)) {
return GR_INVALIDO;
}
for (int u = 0; u < GR_CAP; u++) {
for (int v = 0; v < GR_CAP; v++) {
g->aresta[u][v] = 0;
}
}
g->vertices = n;
g->dirigido = dirigido;
return GR_OK;
}
int gm_estado(const GrafoMatriz *g, int u, int v)
{
if (!forma_m(g) || !indice(g->vertices, u) || !indice(g->vertices, v)) {
return GR_INVALIDO;
}
return g->aresta[u][v];
}
int gm_adicionar(GrafoMatriz *g, int u, int v, int disponivel)
{
if (gm_estado(g, u, v) < 0 || u == v ||
(disponivel != 0 && disponivel != 1)) {
return GR_INVALIDO;
}
if (g->aresta[u][v] != 0) {
return GR_DUPLICADA;
}
g->aresta[u][v] = disponivel ? GR_ABERTA : GR_FECHADA;
if (!g->dirigido) {
g->aresta[v][u] = g->aresta[u][v];
}
return GR_OK;
}
int gm_remover(GrafoMatriz *g, int u, int v)
{
int estado = gm_estado(g, u, v);
if (estado < 0 || u == v) {
return GR_INVALIDO;
}
if (estado == 0) {
return GR_AUSENTE;
}
g->aresta[u][v] = 0;
if (!g->dirigido) {
g->aresta[v][u] = 0;
}
return GR_OK;
}
int gm_disponivel(GrafoMatriz *g, int u, int v, int disponivel)
{
int estado = gm_estado(g, u, v);
if (estado < 0 || (disponivel != 0 && disponivel != 1)) {
return GR_INVALIDO;
}
if (estado == 0) {
return GR_AUSENTE;
}
g->aresta[u][v] = disponivel ? GR_ABERTA : GR_FECHADA;
if (!g->dirigido) {
g->aresta[v][u] = g->aresta[u][v];
}
return GR_OK;
}
int gm_conferir(const GrafoMatriz *g)
{
if (!forma_m(g)) {
return 0;
}
for (int u = 0; u < GR_CAP; u++) {
for (int v = 0; v < GR_CAP; v++) {
int e = g->aresta[u][v];
if (e < 0 || e > 2 || (u == v && e != 0) ||
((u >= g->vertices || v >= g->vertices) && e != 0) ||
(!g->dirigido && e != g->aresta[v][u])) {
return 0;
}
}
}
return 1;
}
int gl_iniciar(GrafoLista *g, int n, int dirigido)
{
if (g == NULL || !configuracao(n, dirigido)) {
return GR_INVALIDO;
}
for (int u = 0; u < GR_CAP; u++) {
g->cabeca[u] = NULL;
}
g->vertices = n;
g->dirigido = dirigido;
return GR_OK;
}
static GrafoArco **posicao(GrafoLista *g, int u, int v)
{
GrafoArco **p = &g->cabeca[u];
while (*p != NULL && (*p)->destino < v) {
p = &(*p)->prox;
}
return p;
}
const GrafoArco *gl_vizinhos(const GrafoLista *g, int u)
{
if (!forma_l(g) || !indice(g->vertices, u)) {
return NULL;
}
return g->cabeca[u];
}
int gl_estado(const GrafoLista *g, int u, int v)
{
const GrafoArco *p;
if (!forma_l(g) || !indice(g->vertices, u) || !indice(g->vertices, v)) {
return GR_INVALIDO;
}
p = g->cabeca[u];
while (p != NULL && p->destino < v) {
p = p->prox;
}
return p != NULL && p->destino == v ? p->estado : GR_AUSENTE;
}
int gl_adicionar(GrafoLista *g, int u, int v, int disponivel)
{
GrafoArco **pu, **pv = NULL;
GrafoArco *a, *b = NULL;
int estado;
if (gl_estado(g, u, v) < 0 || u == v ||
(disponivel != 0 && disponivel != 1)) {
return GR_INVALIDO;
}
pu = posicao(g, u, v);
if (*pu != NULL && (*pu)->destino == v) {
return GR_DUPLICADA;
}
if (!g->dirigido) {
pv = posicao(g, v, u);
if (*pv != NULL && (*pv)->destino == u) {
return GR_INVALIDO;
}
}
a = malloc(sizeof *a);
if (a == NULL) {
return GR_MEMORIA;
}
if (!g->dirigido) {
b = malloc(sizeof *b);
if (b == NULL) {
free(a);
return GR_MEMORIA;
}
}
estado = disponivel ? GR_ABERTA : GR_FECHADA;
a->destino = v;
a->estado = estado;
a->prox = *pu;
if (b != NULL) {
b->destino = u;
b->estado = estado;
b->prox = *pv;
}
*pu = a;
if (b != NULL) {
*pv = b;
}
return GR_OK;
}
int gl_remover(GrafoLista *g, int u, int v)
{
GrafoArco **pu, **pv = NULL;
GrafoArco *a, *b = NULL;
int estado = gl_estado(g, u, v);
if (estado < 0 || u == v) {
return GR_INVALIDO;
}
if (estado == 0) {
return GR_AUSENTE;
}
pu = posicao(g, u, v);
if (!g->dirigido) {
pv = posicao(g, v, u);
if (*pv == NULL || (*pv)->destino != u) {
return GR_INVALIDO;
}
b = *pv;
}
a = *pu;
*pu = a->prox;
if (b != NULL) {
*pv = b->prox;
}
free(a);
if (b != NULL) {
free(b);
}
return GR_OK;
}
int gl_disponivel(GrafoLista *g, int u, int v, int disponivel)
{
GrafoArco **pu, **pv = NULL;
int estado = gl_estado(g, u, v);
if (estado < 0 || (disponivel != 0 && disponivel != 1)) {
return GR_INVALIDO;
}
if (estado == 0) {
return GR_AUSENTE;
}
pu = posicao(g, u, v);
if (!g->dirigido) {
pv = posicao(g, v, u);
if (*pv == NULL || (*pv)->destino != u) {
return GR_INVALIDO;
}
}
(*pu)->estado = disponivel ? GR_ABERTA : GR_FECHADA;
if (pv != NULL) {
(*pv)->estado = (*pu)->estado;
}
return GR_OK;
}
int gl_grau(const GrafoLista *g, int u)
{
const GrafoArco *p;
int grau = 0;
if (!forma_l(g) || !indice(g->vertices, u)) {
return GR_INVALIDO;
}
p = g->cabeca[u];
while (p != NULL) {
grau++;
p = p->prox;
}
return grau;
}
int gl_conferir(const GrafoLista *g)
{
int visto[GR_CAP][GR_CAP] = {{0}};
int total = 0;
if (!forma_l(g)) {
return 0;
}
for (int u = 0; u < GR_CAP; u++) {
const GrafoArco *p = g->cabeca[u];
int anterior = -1;
if (u >= g->vertices && p != NULL) {
return 0;
}
while (p != NULL) {
if (total >= GR_CAP * (GR_CAP - 1) ||
!indice(g->vertices, p->destino) || p->destino == u ||
p->destino <= anterior || p->estado < 1 || p->estado > 2) {
return 0;
}
visto[u][p->destino] = p->estado;
anterior = p->destino;
total++;
p = p->prox;
}
}
if (!g->dirigido) {
for (int u = 0; u < g->vertices; u++) {
for (int v = 0; v < g->vertices; v++) {
if (visto[u][v] != visto[v][u]) {
return 0;
}
}
}
}
return 1;
}
void gl_destruir(GrafoLista *g)
{
if (g == NULL) {
return;
}
for (int u = 0; u < GR_CAP; u++) {
while (g->cabeca[u] != NULL) {
GrafoArco *p = g->cabeca[u];
g->cabeca[u] = p->prox;
free(p);
}
}
g->vertices = 0;
g->dirigido = 0;
}
int gl_converter(const GrafoMatriz *origem, GrafoLista *destino)
{
GrafoLista temporario;
if (!gm_conferir(origem) || !forma_l(destino)) {
return GR_INVALIDO;
}
(void)gl_iniciar(&temporario, origem->vertices, origem->dirigido);
for (int u = 0; u < origem->vertices; u++) {
for (int v = 0; v < origem->vertices; v++) {
int estado = origem->aresta[u][v];
if (estado != 0 && (origem->dirigido || u < v)) {
int r = gl_adicionar(&temporario, u, v, estado == GR_ABERTA);
if (r != GR_OK) {
gl_destruir(&temporario);
return r;
}
}
}
}
gl_destruir(destino);
*destino = temporario;
(void)gl_iniciar(&temporario, 0, 0);
return GR_OK;
}
grafos.h
#ifndef GRAFOS_H
#define GRAFOS_H
#define GR_CAP 8
#define GR_OK 1
#define GR_AUSENTE 0
#define GR_INVALIDO (-1)
#define GR_DUPLICADA (-2)
#define GR_MEMORIA (-3)
#define GR_FECHADA 1
#define GR_ABERTA 2
typedef struct {
int vertices;
int dirigido;
int aresta[GR_CAP][GR_CAP];
} GrafoMatriz;
typedef struct GrafoArco {
int destino;
int estado;
struct GrafoArco *prox;
} GrafoArco;
typedef struct {
int vertices;
int dirigido;
GrafoArco *cabeca[GR_CAP];
} GrafoLista;
int gm_iniciar(GrafoMatriz *g, int vertices, int dirigido);
int gm_estado(const GrafoMatriz *g, int u, int v);
int gm_adicionar(GrafoMatriz *g, int u, int v, int disponivel);
int gm_remover(GrafoMatriz *g, int u, int v);
int gm_disponivel(GrafoMatriz *g, int u, int v, int disponivel);
int gm_conferir(const GrafoMatriz *g);
int gl_iniciar(GrafoLista *g, int vertices, int dirigido);
int gl_estado(const GrafoLista *g, int u, int v);
int gl_adicionar(GrafoLista *g, int u, int v, int disponivel);
int gl_remover(GrafoLista *g, int u, int v);
int gl_disponivel(GrafoLista *g, int u, int v, int disponivel);
const GrafoArco *gl_vizinhos(const GrafoLista *g, int u);
int gl_grau(const GrafoLista *g, int u);
int gl_conferir(const GrafoLista *g);
int gl_converter(const GrafoMatriz *origem, GrafoLista *destino);
void gl_destruir(GrafoLista *g);
#endif
sequencia.c
#include "sequencia.h"
#include <stdlib.h>
#include <stdint.h>
void seq_iniciar(Sequencia *s)
{
*s = (Sequencia){NULL, 0, 0};
}
void seq_destruir(Sequencia *s)
{
if (s != NULL) {
free(s->dados);
*s = (Sequencia){NULL, 0, 0};
}
}
int seq_invariante(const Sequencia *s)
{
if ((s == NULL) || (s->tamanho > s->capacidade)
|| (s->capacidade > SEQ_MAX)) {
return 0;
}
return (s->capacidade == 0) == (s->dados == NULL);
}
size_t seq_tamanho(const Sequencia *s)
{
return s->tamanho;
}
size_t seq_capacidade(const Sequencia *s)
{
return s->capacidade;
}
static int ampliar(Sequencia *s)
{
size_t nova = 1;
if (s->capacidade != 0) {
if (s->capacidade > SEQ_MAX / 2) {
nova = SEQ_MAX;
} else {
nova = s->capacidade * 2;
}
}
if ((nova == 0) || (nova > SIZE_MAX / sizeof *s->dados)) {
return SEQ_MEMORIA;
}
Pedido *novo = realloc(s->dados, nova * sizeof *s->dados);
if (novo == NULL) {
return SEQ_MEMORIA;
}
s->dados = novo;
s->capacidade = nova;
return SEQ_OK;
}
int seq_consultar(const Sequencia *s, int indice, Pedido *saida)
{
if (!seq_invariante(s) || (saida == NULL) || (indice < 0)
|| ((size_t)indice >= s->tamanho)) {
return SEQ_INVALIDO;
}
*saida = s->dados[(size_t)indice];
return SEQ_OK;
}
int seq_inserir(Sequencia *s, int indice, Pedido valor)
{
if (!seq_invariante(s) || (indice < 0)
|| ((size_t)indice > s->tamanho)
|| (valor.id < 1) || (valor.id > 99)
|| (valor.recursos < 0) || (valor.recursos > 20)) {
return SEQ_INVALIDO;
}
if (s->tamanho == SEQ_MAX) {
return SEQ_LIMITE;
}
if ((s->tamanho == s->capacidade) && (ampliar(s) != SEQ_OK)) {
return SEQ_MEMORIA;
}
size_t posicao = (size_t)indice;
for (size_t j = s->tamanho; j > posicao; j--) {
s->dados[j] = s->dados[j - 1];
}
s->dados[posicao] = valor;
s->tamanho++;
return SEQ_OK;
}
int seq_remover(Sequencia *s, int indice, Pedido *saida)
{
if (!seq_invariante(s) || (saida == NULL) || (indice < 0)
|| ((size_t)indice >= s->tamanho)) {
return SEQ_INVALIDO;
}
size_t posicao = (size_t)indice;
Pedido retirado = s->dados[posicao];
for (size_t j = posicao; j + 1 < s->tamanho; j++) {
s->dados[j] = s->dados[j + 1];
}
s->tamanho--;
*saida = retirado;
return SEQ_OK;
}
sequencia.h
#ifndef SEQUENCIA_H
#define SEQUENCIA_H
#include <stddef.h>
#define SEQ_MAX 32
#define SEQ_OK 1
#define SEQ_MEMORIA 0
#define SEQ_INVALIDO -1
#define SEQ_LIMITE -2
typedef struct {
int id;
int recursos;
} Pedido;
typedef struct {
Pedido *dados;
size_t tamanho;
size_t capacidade;
} Sequencia;
void seq_iniciar(Sequencia *s);
void seq_destruir(Sequencia *s);
int seq_invariante(const Sequencia *s);
size_t seq_tamanho(const Sequencia *s);
size_t seq_capacidade(const Sequencia *s);
int seq_consultar(const Sequencia *s, int indice, Pedido *saida);
int seq_inserir(Sequencia *s, int indice, Pedido valor);
int seq_remover(Sequencia *s, int indice, Pedido *saida);
#endif
Editar e executar esta soluçãoExemplo 1 · entrada e saída esperada
Entrada padrão
(sem entrada)
Saída esperada
Sequencia: antes=1 quebrada=0 restaurada=1 primeiro=2.
Matriz: antes=1 assimetrica=0 restaurada=1 AB=2.
Diagnosticos nao validam enderecos arbitrarios.
Estado de saída: 0.
Editar e testar o exemplo 1