Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //Declaracao da Estrutura
- struct tipoElemento{ //criar funcao
- int valor;
- struct tipoElemento *proximo;
- };
- struct tipoLista{
- struct tipoElemento *inicio;
- struct tipoElemento *fim;
- };
- //---------------------------------------------------------------
- // ASSINATURAS DAS FUNΓΓES
- //---------------------------------------------------------------
- void inicializar(struct tipoLista *lista);
- void inserir_inicio(struct tipoLista *lista, struct tipoElemento *elem);
- void inserir_fim(struct tipoLista *lista, struct tipoElemento *elem);
- void remover_inicio(struct tipoLista *lista);
- void remover_fim(struct tipoLista *lista);
- void listar(struct tipoLista *lista);
- void pesquisar(struct tipoLista *lista, struct tipoElemento *elem);
- int lista_vazia(struct tipoLista *lista);
- int menu();
- struct tipoElemento *lerElemento();
- //------------------------------------------
- //PROGRAMA PRINCIPAL
- //------------------------------------------
- int main(){
- struct tipoLista lista;
- struct tipoElemento *elem;
- int opcao;
- inicializar(&lista);
- do{
- system("cls");
- opcao = menu();
- system("cls");
- switch(opcao)
- {
- case 1:
- printf("INSERIR INICIO \n\n");
- elem = lerElemento();
- inserir_inicio(&lista, elem);
- break;
- case 2:
- printf("INSERIR FIM \n\n");
- elem = lerElemento();
- inserir_fim(&lista, elem); //nao esquecer desta merda
- break;
- case 3:
- printf("REMOVER INICIO \n\n");
- remover_inicio(&lista);
- break;
- case 4:
- printf("REMOVER FIM \n\n");
- remover_fim(&lista);
- break;
- case 5:
- printf("PESQUISAR \n\n");
- break;
- case 6:
- printf("LISTAR \n\n");
- listar(&lista);
- break;
- }
- system("pause");
- }while(opcao !=0);
- return 0;
- }
- void inicializar(struct tipoLista *lista){
- lista->inicio = NULL;
- lista->fim = NULL;
- }
- void inserir_inicio(struct tipoLista *lista, struct tipoElemento *elem){
- if(lista_vazia(lista) == 1){
- lista->inicio = elem;
- lista->fim = elem;
- }else{
- elem->proximo = lista->inicio; //proximo aponta para o inicio
- lista->inicio = elem;
- }
- }
- void inserir_fim(struct tipoLista *lista, struct tipoElemento *elem){
- if(lista_vazia(lista) == 1){
- lista->inicio = elem;
- lista->fim = elem;
- } else{
- lista->fim->proximo = elem;
- lista->fim = elem;
- }
- }
- void remover_inicio(struct tipoLista *lista) {
- if (lista->inicio == lista->fim) {
- lista->inicio = NULL;
- lista->fim = NULL;
- } else {
- lista->inicio = lista->inicio->proximo;
- }
- }
- void remover_fim(struct tipoLista *lista) {
- if (lista->inicio == lista->fim) {
- lista->inicio = NULL;
- lista->fim = NULL;
- } else {
- struct tipoElemento *aux = lista->inicio;
- while(aux->proximo != lista->fim)
- aux = aux->proximo;
- lista->fim = aux;
- lista->fim->proximo = NULL;
- }
- }
- void listar(struct tipoLista *lista){
- if(lista_vazia(lista) == 1){
- printf("\n\nA lista esta VAZIA!\n\n");
- } else{
- struct tipoElemento *aux = lista->inicio;
- while(aux!= NULL){
- printf("\nValor: %d", aux->valor);
- aux = aux->proximo;
- }
- printf("\nFIM\n\n");
- }
- }
- int lista_vazia(struct tipoLista *lista){
- if(lista->inicio == NULL && lista -> fim == NULL){
- return 1; //SIM, a lista esta vazia
- }else{
- return 0;
- } //NAO, a lista nao esta vazia
- }
- //------------------------------------------
- //-----------MENU------------
- int menu()
- {
- int opcao = 0;
- printf("\n===================================");
- printf("\n LISTA DINAMICA SIMPLES ");
- printf("\n===================================");
- printf("\n [1] Inserir Inicio ");
- printf("\n [2] Inserir Fim ");
- printf("\n [3] Remover Inicio ");
- printf("\n [4] Remover Fim ");
- printf("\n [5] Pesquisar ");
- printf("\n [6] Listar ");
- printf("\n [0] Sair ");
- printf("\n\n Opcao: ");
- scanf("%d",&opcao);
- return opcao;
- }
- //-------------------------------
- struct tipoElemento *lerElemento(){
- struct tipoElemento *novo;
- //Alocacao de memoria
- /*A funcao malloc eh utilizada para a alocacao de memoria. O nome
- eh uma abreviatura de memory allocation.
- A alocacao de memoria reserva um espaco na memoria principal (RAM)
- para manipulacao de dados. */
- //malloc(1000) -> aloca 1000 bytes
- novo = (struct tipoElemento *)malloc(sizeof(struct tipoElemento));
- //Leitura de valor. Tem que ter a funcao malloc. Ver no gerenciador
- printf("\nValor: ");
- scanf("%d", &novo->valor);
- novo->proximo = NULL;
- return novo;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement