int addAt(Node * l, int data, int position) { if (position < 0) return 0; Node * aux = l; int cont = 0; while (cont < position && aux != NULL) { if (aux->proximo != NULL) aux = aux->proximo; cont++; } Node * newNode = novo(data); if (aux->anterior == NULL) { newNode->proximo = l; l = newNode; return 1; } else { Node * temp1 = aux->anterior; temp1->proximo = newNode; newNode->anterior = temp1; newNode->proximo = aux; aux->anterior = newNode; return 1; } }
int main(int argc, const char * argv[]) { //Lista * minhaLista = (Lista *) malloc(sizeof(Lista)); Node *p = novo(1); //addAt(p, 2, 0); //addAt(p, 3, 3); p = adiciona(p, 2); p = adiciona(p, 3); imprime(p); return 0; }
grafo *recebe_grafo(FILE * fp) { if ( !fp ) return NULL; FILE *f = fp; // busca dimensoes da matriz de representacao int lin = 0, col = 0, col_atual = 0; char c; while ( (c=fgetc(f)) != EOF ) { if ( c == '\n' ) { lin++; col_atual = 0; } else { col_atual++; if ( col_atual > col ) col = col_atual; } } // preenche matriz de representacao int **r = malloc(lin * sizeof(int *)); if ( !r ) return NULL; int i, j; for ( i=0; i<lin; i++ ) if ( !(r[i] = calloc(col_atual, sizeof(int))) ) return NULL; rewind(f); int k = 0; i = 0; j = 0; while ( (c=fgetc(fp)) != EOF ) { if ( c == 'C' ) r[i][j] = ++k; if ( c == '\n' ) { i++; j = 0; } else j++; } //imprime_matriz(r, 0, lin-1, 0, col-1); grafo *G = novo(k); // preenche a matriz de adjacencias for ( i=0; i<lin; i++ ) for ( j=0; j<col; j++ ) if ( r[i][j] > 0 ) { // testa para baixo if ( i+1 < lin && r[i+1][j] > 0 ) { G->A[ r[i][j] ][ r[i+1][j] ] = 1; G->A[ r[i+1][j] ][ r[i][j] ] = 1; E(G)++; } // testa para direita if ( j+1 < col && r[i][j+1] > 0 ) { G->A[ r[i][j] ][ r[i][j+1] ] = 1; G->A[ r[i][j+1] ][ r[i][j] ] = 1; E(G)++; } /*// testa para esquerda if ( j-1 >= 0 && r[i][j-1] > 0 ) { G->A[ r[i][j] ][ r[i][j-1] ] = 1; G->A[ r[i][j-1] ][ r[i][j] ] = 1; } // testa para cima if ( i-1 >= 0 && r[i-1][j] > 0 ) { G->A[ r[i][j] ][ r[i-1][j] ] = 1; G->A[ r[i-1][j] ][ r[i][j] ] = 1; }*/ } // imprime matriz de adjacencias //imprime_matriz(G->A, 1, V(G), 1, V(G)); return G; }
Node * adiciona (Node *l, int novoDado) { Node *novoNode = novo(novoDado); novoNode->proximo = l; l->anterior = novoNode; return novoNode; }