Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct no
- {
- char dispositivos[30];
- char estado [30];
- struct no *prox;
- };
- typedef struct no no;
- int tam;
- void inicia(no *LISTA);
- int menu(void);
- void opcao(no *LISTA, int op);
- no *criaNo();
- void insereFim(no *LISTA);
- void insereInicio(no *LISTA);
- void exibe(no *LISTA);
- void libera(no *LISTA);
- void insere (no *LISTA);
- no *retiraInicio(no *LISTA);
- no *retiraFim(no *LISTA);
- no *retira(no *LISTA);
- void lista_ligado(no *LISTA);
- void muda_estado(no *LISTA);
- int main()
- {
- no *LISTA = (no*) malloc(sizeof(no));
- if(!LISTA)
- {
- printf("Sem memoria disponivel!\n");
- exit(1);
- }
- else
- {
- inicia(LISTA);
- int opt;
- do
- {
- opt = menu();
- opcao(LISTA, opt);
- }
- while(opt);
- free(LISTA);
- }
- return 0;
- }
- void inicia(no *LISTA)
- {
- LISTA -> prox = NULL;
- tam = 0;
- }
- int menu(void)
- {
- int opt;
- printf("0. Sair\n");
- printf("1. Zerar lista\n");
- printf("2. Exibir lista\n");
- printf("3. Adicionar no inicio\n");
- printf("4. Adicionar no final\n");
- printf("5. Escolher onde inserir\n");
- printf("6. Retirar do inicio\n");
- printf("7. Retirar do fim\n");
- printf("8. Escolher de onde tirar\n");
- printf("9. Listar Objetos Ligados\n");
- printf("10. Muda estado");
- scanf("%d", &opt);
- return opt;
- }
- void opcao(no*LISTA, int op)
- {
- no *tmp;
- switch(op)
- {
- case 0:
- libera(LISTA);
- break;
- case 1:
- libera(LISTA);
- inicia(LISTA);
- break;
- case 2:
- exibe(LISTA);
- break;
- case 3:
- insereInicio(LISTA);
- break;
- case 4:
- insereFim(LISTA);
- break;
- case 5:
- insere(LISTA);
- break;
- case 6:
- tmp = retiraInicio(LISTA);
- printf("Retirado: %s\n\n", tmp->dispositivos);
- break;
- case 7:
- tmp = retiraFim(LISTA);
- printf("Retirado: %s\n\n", tmp->dispositivos);
- break;
- case 8:
- tmp = retira(LISTA);
- printf("Retirado: %s\n\n", tmp->dispositivos);
- break;
- case 9:
- lista_ligado(LISTA);
- break;
- case 10:
- muda_estado(LISTA);
- break;
- default:
- printf("Comando invalido\n\n");
- }
- }
- int vazia(no *LISTA)
- {
- if(LISTA->prox == NULL)
- return 1;
- else
- return 0;
- }
- no *aloca()
- {
- no *novo = (no *) malloc(sizeof(no));
- if(!novo)
- {
- printf("Sem memoria disponivel!\n");
- exit(1);
- }
- else
- {
- printf("Novo elemento: \n");
- scanf("%s", &novo->dispositivos);
- printf("Estado LIGADO/DESLIGADO\n");
- scanf("%s", &novo->estado);
- return novo;
- }
- }
- void insereFim(no *LISTA)
- {
- no *novo = aloca();
- novo->prox = NULL;
- if(vazia(LISTA))
- LISTA->prox = novo;
- else
- {
- no*tmp = LISTA->prox;
- while(tmp->prox != NULL)
- tmp = tmp->prox;
- tmp->prox = novo;
- }
- tam++;
- }
- void insereInicio(no *LISTA)
- {
- no *novo = aloca();
- no *oldHead = LISTA->prox;
- LISTA->prox = novo;
- novo->prox = oldHead;
- tam++;
- }
- void exibe(no *LISTA)
- {
- if(vazia(LISTA))
- {
- printf("Lista vazia!\n\n");
- return ;
- }
- no *tmp;
- tmp = LISTA->prox;
- while( tmp != NULL)
- {
- printf("%s", tmp->dispositivos);
- printf("%s", tmp->estado);
- tmp = tmp->prox;
- }
- printf("\n\n");
- }
- void libera(no *LISTA)
- {
- if(!vazia(LISTA))
- {
- no *proxNo,
- *atual;
- atual = LISTA->prox;
- while(atual != NULL)
- {
- proxNo = atual->prox;
- free(atual);
- atual = proxNo;
- }
- }
- }
- void insere(no *LISTA)
- {
- int pos,
- count;
- printf("Em que posicao, [de 1 ate %d] voce deseja inserir: ", tam);
- scanf("%d", &pos);
- if(pos > 0 && pos <= tam)
- {
- if(pos == 1)
- insereInicio(LISTA);
- else
- {
- no *atual = LISTA->prox,
- *anterior = LISTA;
- no *novo = aloca();
- for(count = 1 ; count < pos ; count++)
- {
- anterior = atual;
- atual = atual->prox;
- }
- anterior->prox = novo;
- novo->prox = atual;
- tam++;
- }
- }
- else
- printf("Elemento invalido\n\n");
- }
- no *retiraInicio(no *LISTA)
- {
- if(LISTA->prox == NULL)
- {
- printf("Lista ja esta vazia\n");
- return NULL;
- }
- else
- {
- no *tmp = LISTA->prox;
- LISTA->prox = tmp->prox;
- tam--;
- return tmp;
- }
- }
- no *retiraFim(no *LISTA)
- {
- if(LISTA->prox == NULL)
- {
- printf("Lista ja vazia\n\n");
- return NULL;
- }
- else
- {
- no *ultimo = LISTA->prox,
- *penultimo = LISTA;
- while(ultimo->prox != NULL)
- {
- penultimo = ultimo;
- ultimo = ultimo->prox;
- }
- penultimo->prox = NULL;
- tam--;
- return ultimo;
- }
- }
- no *retira(no *LISTA)
- {
- int opt,
- count;
- printf("Que posicao, [de 1 ate %d] voce deseja retirar: ", tam);
- scanf("%d", &opt);
- if(opt > 0 && opt <= tam)
- {
- if(opt == 1)
- return retiraInicio(LISTA);
- else
- {
- no *atual = LISTA->prox,
- *anterior = LISTA;
- for(count = 1 ; count < opt ; count++)
- {
- anterior = atual;
- atual = atual->prox;
- }
- anterior->prox = atual->prox;
- tam--;
- return atual;
- }
- }
- else
- {
- printf("Elemento invalido\n\n");
- return NULL;
- }
- }
- void lista_ligado(no *LISTA)
- {
- if(vazia(LISTA))
- {
- printf("Lista vazia!\n\n");
- return ;
- }
- no *tmp;
- tmp = LISTA->prox;
- printf("Lista:\n\n");
- while( tmp != NULL)
- {
- if(strcmp("ligado", tmp->estado) == 0)
- {
- printf("\n Dispositivo %s\n", tmp->dispositivos);
- printf("\n Estado %s\n", tmp->estado);
- tmp = tmp->prox;
- }
- else
- {
- tmp = tmp->prox;
- }
- }
- printf("\n\n");
- }
- void muda_estado(no *LISTA)
- {
- if(vazia(LISTA))
- {
- printf("Lista vazia!\n\n");
- return ;
- }
- char op[30];
- printf("Digite o dispotivo para mudar estado");
- scanf("%s",&op);
- no *tmp;
- tmp = LISTA->prox;
- while( tmp != NULL)
- {
- if(strcmp(op, tmp->dispositivos) == 0)
- {
- tmp->estado = "desligado";
- tmp = tmp->prox;
- }
- else
- {
- tmp = tmp->prox;
- }
- }
- printf("\n\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement