void GRA_Del (Graph *g) { #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ CNT_CONTAR("GRA_Del - Inicializao"); IrInicioLista ( g->nodes ); if ( !g->currentNode || !LIS_AvancarElementoCorrente ( g->nodes , 1)){ CNT_CONTAR("GRA_Del - Grafo vazio"); return; } IrInicioLista ( g->nodes ); do { CNT_CONTAR("GRA_Del - Deletando nos"); g->currentNode = LIS_ObterValor( g->nodes ); GRA_DelNode( g ); } while ( LIS_AvancarElementoCorrente ( g->nodes , 1) == LIS_CondRetOK ); /* == 0 ); */ CNT_CONTAR("GRA_Del - Destruindo, finalizao"); LIS_DestruirLista( g->nodes ); free(g); return; }
MTZ_tpCondRet MTZ_DestruirMatriz ( LIS_tppLista Tab ) { LIS_tppLista Col; PLH_tppPilha p; while ( Tab != NULL) { IrFinalLista( Tab ); Col = (LIS_tppLista) LIS_ObterValor( Tab ); while (Col != NULL) { IrFinalLista( Col ); p = (PLH_tppPilha) LIS_ObterValor( Col ); PLH_Libera( p ); LIS_ExcluirElemento( Col ); IrInicioLista( Col ); } LIS_ExcluirElemento( Tab ); IrInicioLista( Tab ); } free ( Tab ); return MTZ_CondRetOK; }
LIS_tppLista MTZ_achaCoor(LIS_tppLista Tab, int x, int y) { LIS_tppLista coor;// = LIS_CriarLista(0);/* Armazena copia da coluna*/ LIS_AvancarElementoCorrente(Tab, x); coor = (LIS_tppLista) LIS_ObterValor(Tab); IrInicioLista(coor); LIS_AvancarElementoCorrente(coor, y); IrInicioLista(Tab); return coor; }
MTZ_tpCondRet MTZ_pushCoor ( LIS_tppLista Tab, int x, int y, char valor ) { PLH_tppPilha p;// = PLH_CriarTopo(); // ISSO PRECISA SER DESTRUIDO DPS LIS_tppLista Col;// = LIS_CriarLista(NULL); // ISSO PRECISA SER DESTRUIDO DPS Col = MTZ_achaCoor(Tab, x, y); p = (PLH_tppPilha) LIS_ObterValor(Col); PLH_Insere(p, valor); IrInicioLista(Col); IrInicioLista(Tab); return MTZ_CondRetOK ; }
LIS_tpCondRet LIS_InserirElementoOrdenado( LIS_tppLista pLista , void *pValor, int (*pComp) (void *a, void *b)) { LIS_tpElemLista *pElem ; #ifdef _DEBUG assert( pLista != NULL ) ; #endif /* Criar elemento a inserir antes */ pElem = CriarElemento( pLista , pValor ) ; if ( pElem == NULL ) { return LIS_CondRetFaltouMemoria ; } /* if */ /* Ir para o comeco da lista */ IrInicioLista (pLista); /* Percorrer a lista até encontrar um valor superior ou igual */ while (LIS_AvancarElementoCorrente(pLista, 1) != LIS_CondRetFimLista) { if (pComp(LIS_ObterValor(pLista), pValor) > 0) { break ; } /* if */ } /* while */ /* Encadear o elemento antes do elemento superior */ return LIS_InserirElementoAntes(pLista, pValor); } /* Fim funcao: LIS &Inserir elemento antes */
/* Essa funcao e' responsavel por criar e compor 2 links ligando * 2 nos. */ enum GRA_Ret linkTwoNodes(Node *node1, Node *node2) { Link *link1, *link2, *link; IrInicioLista(node1->links); CNT_CONTAR("linkTwoNodes - Inicializacao"); do{ /* Assertiva: Nao repetir links */ CNT_CONTAR("linkTwoNodes - Procurando link igual"); link = LIS_ObterValor(node1->links); if( link && link->n2 == node2 ){ CNT_CONTAR("linkTwoNodes - Links iguais"); return GRA_InvalidLink; } }while (LIS_CondRetOK == LIS_AvancarElementoCorrente (node1->links,1)); CNT_CONTAR("linkTwoNodes - Compondo Links"); link1 = (Link*) malloc (sizeof(Link)); if (!link1){ CNT_CONTAR("linkTwoNodes - Alocacao de link1 falhou"); return GRA_MemoryError; } link2 = (Link*) malloc (sizeof(Link)); if (!link2){ /* Muito improvavel de cair aqui num teste CNT_CNTAR("linkTwoNodes - Alocacao de link2 falhou"); */ free (link1); return GRA_MemoryError; } /* Compoe os dois links */ link1->n1 = node1; link1->n2 = node2; link1->brother = link2; link2->n1 = node2; link2->n2 = node1; link2->brother = link1; IrFinalLista(node1->links); IrFinalLista(node2->links); /* Tenta inserir cada link em uma lista, limpa se nao der certo */ CNT_CONTAR("linkTwoNodes - Inserindo link 1 na lista"); if( LIS_CondRetOK != LIS_InserirElementoApos(node1->links, link1) ){ CNT_CONTAR("linkTwoNodes - Nao inseriu link 1 na lista"); return GRA_MemoryError; } CNT_CONTAR("linkTwoNodes - Inserindo link 2 na lista"); if( LIS_CondRetOK != LIS_InserirElementoApos(node2->links, link2) ){ CNT_CONTAR("linkTwoNodes - Nao inseriu link 2 na lista"); LIS_ExcluirElemento(node1->links); return GRA_MemoryError; } #ifdef _DEBUG AssertLink(link1); AssertLink(link2); #endif /* _DEBUG */ CNT_CONTAR("linkTwoNodes - Finalizando"); return GRA_Ok; }
void AssertLink(Link *l) { Link *b = l->brother; CNT_CONTAR("AssertLink - Verificando assertivas"); assert( b->brother == l ); assert( l != b ); assert( l->n1 == b->n2 ); assert( l->n2 == b->n1 ); assert( l->n1->links == b->n2->links ); assert( l->n2->links == b->n1->links ); IrInicioLista( l->n1->links ); IrInicioLista( b->n1->links ); assert( LIS_CondRetOK == LIS_ProcurarValor( l->n1->links , l) ); assert( LIS_CondRetOK == LIS_ProcurarValor( b->n1->links , b) ); }
LIS_tppLista MTZ_criarTab(int x, int y) { int i,j; PLH_tppPilha Pilha; LIS_tppLista Elem; LIS_tppLista Col; LIS_tppLista Tab = LIS_CriarLista(0); /*Cria lista*/ if (Tab == NULL) return NULL; /*if NULL*/ for (i = 0; i < x; i++) { Col = LIS_CriarLista(0); LIS_InserirElementoApos(Tab, Col); for (j = 0; j < y; j++) { Pilha = PLH_CriarTopo(); LIS_InserirElementoApos(Col, Pilha); } } /* Cria toda a matriz de pilhas */ IrInicioLista(Tab); return Tab; }
LIS_tppLista FRC_CriarBaralho( ) { int np ; int rk ; CAR_tppCarta pAuxCar; LIS_tppLista pNovo = LIS_CriarLista( NULL ); if( pNovo == NULL) { return NULL ; } /* if */ for( np = CAR_NaipePaus ; np <= CAR_NaipeOuros ; np++ ) { for( rk = CAR_RankAs ; rk <= CAR_RankRei ; rk++ ) { pAuxCar = CAR_CriarCarta( ( CAR_tpNaipe ) ( np ) , ( CAR_tpRank ) ( rk ) ); LIS_InserirElementoApos( pNovo , ( void *) pAuxCar ) ; } /* for */ } /* if */ IrInicioLista( pNovo ) ; return pNovo; }
int firstFit(Memory *mem, LIS_tppLista lisProntos) { Processo *aux; int cnt = 0, fail = 0,i; IrInicioLista(lisProntos); aux = (Processo*)LIS_ObterValor(lisProntos); cnt = LIS_ObterTamanho(lisProntos); fail = 1; while(cnt > 0) { for(i = 0; i < 5; i++) { if((aux->tamanho <= mem->s[i].tam) && (mem->s[i].proc == NULL) && (aux->naMemoria == 0)) // se o tamanho do process é menor q o segmento da memoria { InsereProcessosMem(mem, aux, mem->s[i].tam); fail = 0; break; } } LIS_AvancarElementoCorrente(lisProntos, 1); aux = (Processo*)LIS_ObterValor(lisProntos); cnt--; } return fail; }
int WorstFit (Memory *mem, LIS_tppLista lisProntos){ Processo *aux; int cnt = 0, fail = 0, i; IrInicioLista(lisProntos); aux = (Processo*)LIS_ObterValor(lisProntos); cnt = LIS_ObterTamanho(lisProntos); fail = 1; while (cnt > 0) { int diferenca = 0; int BestInd = -1; for (i = 0; i < 5; i++) { if ((aux->tamanho <= mem->s[i].tam) && (mem->s[i].proc == NULL) && (aux->naMemoria == 0)) // se o tamanho do process é menor q o segmento da memoria { if (mem->s[i].tam - aux->tamanho >= diferenca){ BestInd = i; diferenca = mem->s[i].tam - aux->tamanho; } } } if (BestInd != -1){ InsereProcessosMem(mem, aux, mem->s[BestInd].tam); fail = 0; } LIS_AvancarElementoCorrente(lisProntos, 1); aux = (Processo*)LIS_ObterValor(lisProntos); cnt--; } return fail; }
//confere numero da quantidade com o numero de movimentos possiveis dentro da lista LIS_tpCondRet LIS_confereNumero(LIS_tppLista pLista){ int i, j; LIS_tppLista lis; IrInicioLista(pLista); for(lis = pLista, i = 0; lis->pElemCorr->pProx != NULL; lis->pElemCorr = lis->pElemCorr->pProx, i++); IrFinalLista(pLista); for(lis = pLista, j = 0; lis->pElemCorr->pAnt != NULL; lis->pElemCorr = lis->pElemCorr->pAnt, j++); return ((i > j ? i : j < pLista->numElem) ? LIS_CondRetOK : LIS_CondRetNaoAchou); }
void getNext(LIS_tppLista lisProc) { Processo *aux; IrInicioLista(lisProc); aux = (Processo*)LIS_ObterValor(lisProc); while(aux->naMemoria == 0) { LIS_AvancarElementoCorrente(lisProc, 1); aux = (Processo*)LIS_ObterValor(lisProc); } }
void popCoor(LIS_tppLista Tab, int x, int y) { LIS_tppLista Col; PtoTopo* p; Col = achaCoor(Tab, x, y); p = (PtoTopo*) LIS_ObterValor(Col); retira(p); IrInicioLista(Tab); }
void pushCoor(LIS_tppLista Tab, int x, int y, void * valor) { LIS_tppLista Col; PtoTopo* p; Col = achaCoor(Tab, x, y); p = (PtoTopo*) LIS_ObterValor(Col); insere(p, valor); IrInicioLista(Tab); }
void ImprimirLista(LIS_tppLista lisProntos) { int tam, i; Processo *aux; IrInicioLista(lisProntos); tam = LIS_ObterTamanho(lisProntos); for(i = 0; i < tam; i++) { aux = (Processo*)LIS_ObterValor(lisProntos); imprimeProcesso(aux); LIS_AvancarElementoCorrente(lisProntos, 1); } }
LIS_tpCondRet LIS_VerificaLixoPredecessor(LIS_tppLista pLista){ LIS_tppLista lis; IrInicioLista(pLista); for(lis = pLista; lis->pElemCorr->pProx != NULL && lis->pElemCorr->pProx->pAnt == lis->pElemCorr; lis->pElemCorr = lis->pElemCorr->pProx) if(lis->pElemCorr->pProx->pAnt != NULL && lis->pElemCorr->pProx->pAnt != lis->pElemCorr) return LIS_CondRetPredecessorLixo; if(lis->pElemCorr->pAnt != NULL && lis->pElemCorr->pAnt->pProx != lis->pElemCorr) return LIS_CondRetPredecessorLixo; return LIS_CondRetOK; }
LIS_tpCondRet LIS_VerificaPredecessorNULL(LIS_tppLista pLista){ LIS_tppLista lis; IrInicioLista(pLista); for(lis = pLista; lis->pElemCorr->pProx != NULL; lis->pElemCorr = lis->pElemCorr->pProx) if(lis->pElemCorr->pProx->pAnt == NULL) return LIS_CondRetPredecessor; if(lis->pElemCorr->pAnt == NULL) return LIS_CondRetPredecessor; return LIS_CondRetOK; }
void GRA_NodesStart (Graph *g) { #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ if (!g->currentNode){ CNT_CONTAR("GRA_NodesStart - Nao existe currentNode"); return; } CNT_CONTAR("GRA_NodesStart - Finalizando"); g->nodesOld = NULL; IrInicioLista ( g->nodes ); }
/*********************************************************************** * * $FC Função: PILHA Push Pilha * * $FV Valor retornado * PILHA_CondRetNaoExiste Caso a PILHA nao exista * PILHA_CondRetOk * $ED Descrição do tipo * bota um elemento no topo da pilha ***********************************************************************/ PILHA_tpCondRet PILHA_Push(PILHA_tppPilha p, CRT_tppCarta carta){ if (!p){ #ifdef _DEBUG CNT_CONTAR("PILHA_Push-pilhaInexistente"); #endif return PILHA_CondRetPilhaInexistente; } IrInicioLista(p->topo); LIS_InserirElementoAntes(p->topo, carta, "carta"); p->quantidade += 1; return PILHA_CondRetOK; } /* Fim função: PILHA Push Pilha */
void GRA_LinksStart (Graph *g) { #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ if (!g->currentNode){ CNT_CONTAR("GRA_LinksStart - Nao existe currentNode"); return; } CNT_CONTAR("GRA_LinksStart - Finalizando"); g->linksOld = NULL; IrInicioLista ( ((Node *)LIS_ObterValor (g->currentNode))->links ); }
/*************************************************************************** * * Função: PIL &Ir Inicio Pilha * ****/ PIL_tpCondRet PIL_IrInicioPilhaCartas( PIL_tpPilha pPilha ) { LIS_tpCondRet retorno; if (pPilha == NULL) return PIL_CondRetPilhaNaoExiste; retorno = IrInicioLista( pPilha->cabeca ) ; if ( retorno == PIL_CondRetPilhaNaoExiste) return PIL_CondRetPilhaNaoExiste; return PIL_CondRetOK; } /* Fim função: PIL &Ir Inicio Pilha Cartas */
void DestruirMatriz ( LIS_tppLista Tab ) { LIS_tppLista Col; PtoTopo* p; while ( Tab != NULL) { IrFinalLista( Tab ); Col = (LIS_tppLista) LIS_ObterValor( Tab ); while (Col != NULL) { IrFinalLista( Col ); p = (PtoTopo*) LIS_ObterValor( Col ); libera( p ); LIS_ExcluirElemento( Col ); IrInicioLista( Col ); } LIS_ExcluirElemento( Tab ); IrInicioLista( Tab ); } free ( Tab ); }
/*********************************************************************** * $FC Função: NPE &Contar Cartas ***********************************************************************/ NPE_tpCondRet NPE_ContarCartas(NPE_Coluna coluna, int *numCartas){ LIS_tpCondRet condRet; if(coluna == NULL) return NPE_CondRetColunaNaoExiste; IrInicioLista(coluna); while(condRet != LIS_CondRetFimLista){ condRet = LIS_AvancarElementoCorrente(coluna, 1); (*numCartas)++; } return NPE_CondRetOK; }
void AssertNode(Node *n, Graph *g) { CNT_CONTAR("AssertNode - Verificando assertivas"); IrInicioLista(g->nodes); do{ CNT_CONTAR("AssertNode - Procurando valor"); if (n == LIS_ObterValor(LIS_ObterValor( g->nodes ))){ CNT_CONTAR("AssertNode - Valor encontrado"); assert( n->delData == g->delData ); return; } } while(LIS_CondRetOK == LIS_AvancarElementoCorrente ( g->nodes , 1)); /* Nao deveria cruzar esse ponto: Significa que nao foi encontrado * n na lista g->nodes */ assert( 0 ); }
enum GRA_Ret GRA_RemLink (Graph *g, void *d) { Node *curr; Link *l; CNT_CONTAR("GRA_RemLink - Inicio"); if (!d){ CNT_CONTAR("GRA_RemLink - Nao existe d"); return GRA_NullData; } if (!g->currentNode){ CNT_CONTAR("GRA_RemLink - Nao existe g->currentNode"); return GRA_InvalidCurrentNode; } #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ curr = LIS_ObterValor (g->currentNode); #ifdef _DEBUG AssertNode(curr,g); #endif /* _DEBUG */ CNT_CONTAR("GRA_RemLink - Procurando elemento"); IrInicioLista (curr->links); do{ CNT_CONTAR("GRA_RemLink - Avanando na lista"); l = (Link*)LIS_ObterValor (curr->links); #ifdef _DEBUG AssertLink(l); #endif /* _DEBUG */ if (l->n2->data == d){ CNT_CONTAR("GRA_RemLink - Deletando elemento"); /* deleta de curr -> n2 */ LIS_ExcluirElemento(curr->links); /* isso deleta link e link->brother */ #ifdef _DEBUG g->nOfLinks -= 2 ; AssertGraph(g); #endif /* _DEBUG */ return GRA_Ok; } }while (LIS_AvancarElementoCorrente(curr->links, 1) == LIS_CondRetOK); CNT_CONTAR("GRA_RemLink - Finalizando"); return GRA_InvalidArgNode; }
void delLink (Link *l) { static int RecursionCnt = 0; RecursionCnt++; IrInicioLista( l->n2->links ); CNT_CONTAR("delLink - Inicio"); if ( RecursionCnt <= 1 && LIS_CondRetOK == LIS_ProcurarValor(l->n2->links,l->brother) ){ CNT_CONTAR("delLink - Achou: Deletando Lista de Links"); LIS_ExcluirElemento(l->n2->links); } CNT_CONTAR("delLink - Finalizando"); free (l); RecursionCnt--; }
enum GRA_Ret searchData(LIS_tppLista l, void *data) { Node* n; CNT_CONTAR("searchData - Inicializao"); IrInicioLista(l); do{ CNT_CONTAR("searchData - Procurando dado"); n = LIS_ObterValor(LIS_ObterValor(l)); if(data == n->data){ CNT_CONTAR("searchData - Achou o dado"); return GRA_Ok; } } while(LIS_CondRetOK == LIS_AvancarElementoCorrente (l, 1)); CNT_CONTAR("searchData - Finalizacao"); return GRA_InvalidArgNode; }
void AssertGraph(Graph *g) { int i; CNT_CONTAR("AssertGraph - Verificando assertivas"); assert( g->nOfNodes == 0 || g->currentNode !=NULL ); assert( g->nOfLinks % 2 == 0 ); IrInicioLista(g->nodes); for ( i=1; LIS_CondRetOK == LIS_AvancarElementoCorrente ( g->nodes , 1); i++ ){ CNT_CONTAR("AssertGraph - Avanando elemento corrente"); } CNT_CONTAR("AssertGraph - Verificando assertivas, finalizando"); assert( i == g->nOfNodes || g->nOfNodes == 0 ); }
void delNode (Graph *g, void *n_) { Node * n=n_; CNT_CONTAR("delNode - Destruindo lista de links"); LIS_DestruirLista( n->links ); if ( g->delData ){ CNT_CONTAR("delNode - Existe g->delData"); ( *g->delData )( n->data ); } #ifdef _DEBUG IrFinalLista( g->currentNode ); free(LIS_ObterValor( g->currentNode )); IrInicioLista( g->currentNode ); #endif /* _DEBUG */ CNT_CONTAR("delNode - Finalizando"); free (n); }