Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct
- {
- int item;
- struct Tlista *prox;
- }Tlista;
- typedef struct
- {
- Tlista *fim;
- Tlista *inicio;
- }lista;
- void create_lista(lista *lista)
- {
- lista->fim = NULL;
- lista->inicio = NULL;
- }
- int ver_lista_vazia(lista *lista)
- {
- if(lista->inicio == NULL)
- return 1;
- else
- return 0;
- }
- void listar_fim(lista *lista)
- {
- int valor;
- //CRIAR UMA NOVA CÉLULA PARA SER ADICIONADA A LISTA
- Tlista *novo;
- novo = (Tlista*)malloc(sizeof(Tlista));
- novo->prox = NULL;
- printf("\nINFORME UM VALOR A SER ADICIONADO NA LISTA: ");
- fflush(stdin);
- scanf("%d", &valor);
- novo->item = valor;
- int result;
- result = ver_lista_vazia(lista);
- if(result)
- lista->inicio = novo;
- else
- lista->fim->prox = novo;
- lista->fim = novo;
- printf("\n%d FOI LISTADO COM SUCCESS...\n", lista->fim->item);
- }
- void listar_inicio(lista *lista)
- {
- int valor;
- //CRIAR UMA NOVA CÉLULA PARA SER ADICIONADA A LISTA
- Tlista *novo;
- novo = (Tlista*)malloc(sizeof(Tlista));
- novo->prox = NULL;
- printf("\nINFORME UM VALOR A SER ADICIONADO NA LISTA: ");
- fflush(stdin);
- scanf("%d", &valor);
- novo->item = valor;
- int result;
- result = ver_lista_vazia(lista);
- if(result)
- {
- lista->inicio = novo;
- lista->fim = novo;
- }
- else
- {
- novo->prox = lista->inicio;
- lista->inicio = novo;
- }
- printf("\n%d FOI LISTADO COM SUCCESS...\n", lista->inicio->item);
- }
- void listar_meio(lista *lista)
- {
- int valor;
- //CRIAR UMA NOVA CÉLULA PARA SER ADICIONADA A LISTA
- Tlista *novo;
- novo = (Tlista*)malloc(sizeof(Tlista));
- novo->prox = NULL;
- printf("\nINFORME UM VALOR A SER ADICIONADO NA LISTA: ");
- fflush(stdin);
- scanf("%d", &valor);
- novo->item = valor;
- int result;
- result = ver_lista_vazia(lista);
- if(result)
- {
- lista->inicio = novo;
- lista->fim = novo;
- }
- else
- {
- Tlista *aux;
- aux = (Tlista*)malloc(sizeof(Tlista));
- int antec = 0;
- printf("\nDIGITE O ANTECESSOR: ");
- fflush(stdin);
- scanf("%d", &antec);
- aux = lista->inicio;
- while(aux->item != antec)
- {
- aux = aux->prox;
- }
- novo->prox = aux->prox;
- aux->prox = novo;
- }
- printf("\n%d FOI LISTADO COM SUCCESS...\n", novo->item);
- }
- void retira_inicio_lista(lista *lista)
- {
- int result;
- result = ver_lista_vazia(lista);
- int retirado;
- Tlista *aux;
- aux = (Tlista*)malloc(sizeof(Tlista));
- if(result)
- printf("\nLISTA VAZIA! NADA PARA DESLISTAR...\n");
- else
- {
- aux = lista->inicio;
- lista->inicio = aux->prox;
- retirado = aux->item;
- aux->prox = NULL;
- free(aux);
- printf("\n%d FOI RETIRADO COM SUCCESS", retirado);
- }
- }
- void retira_meio_lista(lista *lista)
- {
- int result;
- result = ver_lista_vazia(lista);
- int retirado;
- Tlista *aux;
- aux = (Tlista*)malloc(sizeof(Tlista));
- if(result)
- printf("\nLISTA VAZIA! NADA PARA DESLISTAR...\n");
- else
- {
- Tlista *aux = (Tlista*)malloc(sizeof(Tlista));
- Tlista *ant = (Tlista*)malloc(sizeof(Tlista));
- //O ANTERIOR RECEBER O INÍCIO DA LISTA
- ant = lista->inicio;
- //E O AUXILIAR RECEBE A PRÓXIMA POSIÇÃO DO INÍCIO
- aux = ant->prox;
- int retirar;
- printf("\nINFORME O NUMERO QUE DESEJA RETIRAR: ");
- fflush(stdin);
- scanf("%d", &retirar);
- while(aux->item != retirar)
- {
- //MODIFICANDO AS "POSIÇÕES" DO ANTERIOR E DO AUXILIAR
- ant = aux;
- aux = aux->prox;
- }
- //RECONECTANDO A LISTA
- ant->prox = aux->prox;
- //DESCONECTANDO O AUXILIAR -> ELEMENTO A SER RETIRADO
- aux->prox = NULL;
- free(aux);
- printf("\n%d FOI RETIRADO COM SUCCESS", retirar);
- }
- }
- void retira_fim_lista(lista *lista)
- {
- int result;
- result = ver_lista_vazia(lista);
- int retirado;
- Tlista *aux;
- aux = (Tlista*)malloc(sizeof(Tlista));
- if(result)
- printf("\nLISTA VAZIA! NADA PARA DESLISTAR...\n");
- else
- {
- aux = lista->inicio;
- if(lista->inicio == lista->fim)
- {
- lista->inicio == NULL;
- lista->fim == NULL;
- }
- else
- {
- while(aux->prox != lista->fim)
- {
- aux = aux->prox;
- }
- retirado = lista->fim->item;
- lista->fim = aux;
- aux->prox = NULL;
- printf("\n%d FOI RETIRADO COM SUCCESS", retirado);
- }
- }
- }
- void mostrar(lista *lista)
- {
- int result;
- result = ver_lista_vazia(lista);
- if(result)
- printf("\LISTA VAZIA! NADA PARA MOSTRAR...\n");
- else
- {
- Tlista *aux;
- aux = (Tlista*)malloc(sizeof(Tlista));
- aux = lista->inicio;
- while(aux != NULL)
- {
- printf("\n%d\n", aux->item);
- aux = aux->prox;
- }
- }
- }
- void menu(lista *lista)
- {
- int opc;
- //LAÇO PRINCIPAL
- do
- {
- system("cls");
- printf("\n========MENU DE OPCOES========\n");
- printf("\n1 - ADD AO INICIO DA LISTA");
- printf("\n2 - ADD AO MEIO DA LISTA");
- printf("\n3 - ADD AO FIM DA LISTA");
- printf("\n4 - RETIRAR DO INICIO DA LISTA");
- printf("\n5 - RETIRAR DO MEIO DA LISTA");
- printf("\n6 - RETIRAR DO FIM DA LISTA");
- printf("\n7 - MOSTRAR A LISTA");
- printf("\n8 - SAIR");
- printf("\n\nINFORME A OPCAO DESEJADA: ");
- fflush(stdin);
- scanf("%d", &opc);
- //VALIDAR CORRETIVAMENTE A OPÇÃO
- while(opc < 1 || opc > 8)
- {
- printf("\n\nOPCAO INVALIDA!\nINFORME A OPCAO DESEJADA: ");
- fflush(stdin);
- scanf("%d", &opc);
- }
- system("cls");
- switch(opc)
- {
- case 1:
- listar_inicio(lista);
- break;
- case 2:
- listar_meio(lista);
- break;
- case 3:
- listar_fim(lista);
- break;
- case 4:
- retira_inicio_lista(lista);
- break;
- case 5:
- retira_meio_lista(lista);
- break;
- case 6:
- retira_fim_lista(lista);
- break;
- case 7:
- mostrar(lista);
- break;
- }
- if(opc != 8)
- getch();
- }while(opc != 8);
- }
- int main()
- {
- lista *l;
- l = (lista*)malloc(sizeof(lista));
- create_lista(l);
- menu(l);
- printf("\n\nFIM DO PROGRAMA...\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement