Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // 044 - Exibir na tela
- /*** ESTRUTURAS ***/
- typedef struct
- {
- int matricula;
- char nome[30];
- float n1,n2,n3;
- }ALUNO;
- typedef struct elemento
- {
- ALUNO dados;
- struct elemento *prox; // estou criando uma escrutura que aponta para outro nó
- }NO;
- /*** PROTOTIPO E FUNÇAO ***/
- NO* cria_lista();
- int exibir (NO **li);
- int inserir_inicio(NO **li, ALUNO al);
- int inserir_final(NO **li, ALUNO al);
- int inserir_ordenado(NO **li, ALUNO al);
- int lista_vazia(NO **li);
- int remover_final(NO **li);
- int remover_inicio(NO **li);
- int remover_matricula(NO **li, int matricula);
- int pesquisar_matricula(NO **li, int matricula);
- void excluir(NO **li);
- void iniciar();
- /*** MAIN ***/////////////////////////////////////////////////////////////////////////////////////////////
- int main ()
- {
- iniciar();
- int check;
- NO *raiz = NULL;
- raiz = cria_lista();
- ALUNO al = {180, "00joao", 1.2,2.2,3.3};
- ALUNO al2 = {141, "01gabriel", 1.2,2.2,3.3};
- ALUNO al3 = {13, "02rafael", 1.2,2.2,3.3};
- ALUNO al4 = {912, "03marcos", 1.2,2.2,3.3};
- ALUNO al5 = {384, "04otavio", 1.2,2.2,3.3};
- ALUNO al6 = {9, "teste ordenado", 1.2,2.2,3.3};
- // INSERIR NO INICIO
- inserir_ordenado(&raiz, al); // estou passando o endereço do ponteiro raiz, e não o endereço aonde ela aponta
- inserir_ordenado(&raiz, al2);
- inserir_ordenado(&raiz, al3);
- inserir_ordenado(&raiz, al4);
- inserir_ordenado(&raiz, al5);
- inserir_ordenado(&raiz, al6);
- exibir(&raiz);
- pesquisar_matricula(&raiz,13);
- remover_matricula(&raiz,13);
- pesquisar_matricula(&raiz,13);
- exibir(&raiz);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
- void iniciar(){
- int opcao;
- int check;
- int mat;
- float n1, n2, n3;
- char nome[20];
- ALUNO reg;
- NO *raiz = NULL;
- do{
- system("cls");
- printf("Escolha uma opcao: \n\n");
- printf("1 - Criar lista\n");
- printf("2 - Inserir registro no comeco da lista\n");
- printf("3 - Inserir registro no final da lista\n");
- printf("4 - Inserir registro em ordem de matricula\n");
- printf("5 - Exibir todos os registros da lista\n");
- printf("6 - Menu de exclusao\n");
- printf("7 - Tamanho da lista\n");
- printf("0 - Sair\n");
- printf("Opcao: ");
- scanf(" %d",&opcao);
- // não deixa acessar as opções caso a lista nao exista
- if(opcao != 1 && raiz == NULL){
- printf("Por favor crie uma lista para acessar essa opcao\n");
- system("Pause");
- continue;
- }
- if(opcao == 2 || opcao == 3 || opcao == 4){
- printf("\nDigite o numero da matricula: ");
- scanf(" %d",®.matricula);
- printf("Digite o nome: ");
- scanf(" %s",®.nome);
- }
- switch (opcao){
- case 1:
- if(raiz == NULL){
- raiz = cria_lista();
- if (raiz != NULL) printf("\nLista Criada!\n\n");
- else printf("\nErro: nao foi possivel criar a lista");
- system("pause");
- }else{
- printf("\nLista ja foi criada\n");
- system("pause");
- }
- break;
- case 2:
- check = inserir_inicio(&raiz,reg);
- if (check == 1) printf("\nSucesso: Adicionado no inicio da lista\n\n");
- else printf("\n\nErro: lista cheia, ou nao existe\n\n");
- system("pause");
- break;
- case 3:
- check = inserir_final(&raiz,reg);
- if (check == 1) printf("\nSucesso! Adicionado no final da lista\n\n");
- else printf("\n\nErro: lista cheia, ou nao existe\n\n");
- system("pause");
- break;
- case 4:
- check = inserir_ordenado(&raiz,reg);
- if (check == 1) printf("\nSucesso! Adicionado ordenamente na lista\n\n");
- else printf("\n\nErro: lista cheia, ou nao existe\n\n");
- system("pause");
- break;
- case 5:
- check = exibir(&raiz);
- if(check == 0){
- printf("\nLista vazia, ou nao foi criada\n\n");
- }
- system("pause");
- break;
- case 6:
- excluir(&raiz);
- system("pause");
- break;
- case 7:
- /*check = tamanho_lista(&raiz);
- if (check < 0)
- printf("\nLista vazia\n\n");
- else
- printf("\nTamanho da lista %d\n\n", tamanho_lista(&raiz));
- system("pause");*/
- break;
- case 0:
- printf("\nSaindo...\n");
- break;
- default:
- system("cls");
- printf("Opcao invalida, tente novamente\n\n");
- }
- }while(opcao != 0 );
- }
- /*** Exibir na tela ***/
- int exibir(NO **li)
- {
- if (lista_vazia(li) == (-1)) {
- return 0;
- }else {
- NO *temp;
- temp = (*li)->prox;
- while (temp != NULL ){
- printf("Matricula: %d, Nome: %s \n",temp->dados.matricula,temp->dados.nome);
- temp = temp->prox;
- }
- return 1;
- }
- }
- /*** CRIA LISTA ***/
- NO* cria_lista()
- {
- NO *ptr;
- ptr = (NO*) malloc(sizeof(NO));
- ptr->prox = NULL;
- return ptr;
- }
- /*** LISTA VAZIA ***/
- int lista_vazia(NO **li){
- return ((*li)->prox == NULL);
- }
- /*** INSERIR NO INICIO ***/
- int inserir_inicio(NO **li, ALUNO al)
- {
- if(*li == NULL) {
- printf("Lista nao criada\n");
- return -1;
- }
- NO *novo = (NO*) malloc(sizeof(NO));
- if(novo != NULL)
- {
- novo->dados = al;
- novo->prox = (*li)->prox;
- (*li)->prox = novo;
- return 1;
- }
- }
- /*** REMOVER INICIO ***/
- int remover_inicio(NO **li){
- if (lista_vazia(li)) {
- printf("entrou");
- return 0;
- }
- NO *temp;
- temp = (*li)->prox;
- (*li)->prox = temp->prox;
- free(temp);
- return 1;
- }
- /*** INSERIR NO FINAL ***/
- int inserir_final(NO **li, ALUNO al)
- {
- if(*li == NULL) return -1;
- NO *temp;
- temp = (*li);
- while (temp->prox != NULL){
- temp = temp->prox;
- }
- if(temp->prox == NULL){
- NO *novo = (NO*) malloc(sizeof(NO));
- if (novo != NULL){
- novo->dados = al;
- temp->prox = novo;
- novo->prox = NULL;
- return 1;
- }
- }
- }
- /*** REMOVER FINAL ***/
- int remover_final(NO **li){
- if (lista_vazia(li) == (-1)) {
- return -1;
- }
- NO *temp;
- temp = (*li)->prox;
- while(temp->prox->prox != NULL){
- temp = temp->prox;
- }
- if(temp->prox->prox == NULL){
- free(temp->prox);
- temp->prox = NULL;
- return 1;
- }
- }
- /*** Pesquisar por matricula ***/
- int pesquisar_matricula(NO **li, int matricula){
- if (lista_vazia(li)){
- return 1;
- }
- NO *temp = (*li)->prox;
- while(temp->dados.matricula != matricula && temp->prox != NULL){
- temp = temp->prox;
- }
- if (matricula == temp->dados.matricula){
- printf("\nEncontrado!\n");
- printf("Nome da pessoa (%d): %s\n", temp->dados.matricula, temp->dados.nome);
- return 1;
- }else {
- printf("Matricula %d nao encontrada \n\n",matricula);
- return 0;
- }
- }
- /*** remover por matricula ***/
- int remover_matricula(NO **li, int matricula){
- if (lista_vazia(li)){
- printf("\nLista vazia\n");
- return 1;
- }
- NO *temp = (*li);
- NO *temp2;
- while(temp->prox->dados.matricula != matricula && temp != NULL){
- temp=temp->prox;
- }
- if (temp->prox->dados.matricula == matricula){
- printf("\nEncontrado! \nRemovendo matricula %d\n", temp->prox->dados.matricula);
- temp2 = temp->prox->prox;
- temp->prox = temp2;
- return 1;
- }
- else {
- printf("\nNao encontrado\n");
- }
- }
- /*** inserir ordenado ***/
- int inserir_ordenado(NO **li, ALUNO al){
- if(*li == NULL) return -1;
- // Chama a funçao inserir inicio se a lista estiver vazia
- if((*li)->prox == NULL){
- return inserir_inicio(li, al);
- }
- NO *novo = (NO*) malloc(sizeof(NO));
- NO *temp = (*li);
- printf("entrou %d, valor de temp: %d\n",al.matricula,temp->prox->dados.matricula);
- while (al.matricula >= temp->prox->dados.matricula){
- temp = temp->prox;
- if(temp->prox == NULL){
- novo->dados = al;
- novo->prox = NULL ;
- temp->prox = novo;
- return 1;
- }
- }
- if (al.matricula <= temp->prox->dados.matricula){
- novo->dados = al;
- novo->prox = temp->prox;
- temp->prox = novo;
- return 1;
- }
- }
- /*** EXCLUIR DA LISTA ***/
- void excluir(NO **li){
- int opcao, check, matricula;
- system("CLS");
- printf("1 - Remover inicio da lista \n2 - Remover final da lista \n3 - Remover por identificador \n0 - Voltar \nOpcao: ");
- scanf(" %d",&opcao);
- switch(opcao){
- case 1:
- check = remover_inicio(li);
- if (check == 1) printf("\n\nSucesso! Removido do inicio da lista\n\n");
- else printf("\n\nErro: lista vazia, ou nao foi criada\n\n");
- system("pause");
- break;
- case 2:
- check = remover_final(li);
- if (check == 1) printf("\n\nSucesso! Removido do final da lista\n\n");
- else printf("\n\nErro: lista vazia, ou nao foi criada\n\n");
- system("pause");
- break;
- case 3:
- printf("\n\nDigite o numero da matricula para remover: ");
- scanf(" %d", &matricula);
- check = remover_matricula(li, matricula);
- if (check == 1) printf("\nSucesso! Removido com sucesso\n\n");
- else printf("\n\nErro: Erro: lista vazia, ou nao foi criada\n\n");
- system("pause");
- break;
- case 0:
- break;
- default:
- printf("\n\nOpcao invalida, tente novamente...");
- system("pause");
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement