Buscar

TAD Lista Dinâmica Duplamente Encadeada - (Insere, busca, exclui,ordena(alfabética))

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

ListaDE.h
/* 
 * File: ListaDE.h
 * Author: joao
 *
 * Created on 19 de Abril de 2016, 22:03
 */
#ifndef LISTADE_H
#define	LISTADE_H
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#ifdef	__cplusplus
extern "C" {
#endif
 //--------------------------------------------------------------------------
 struct DoubleLL {
 char nome[40];
 struct DoubleLL *prox, *ant;
 };
 typedef struct DoubleLL list;
 //--------------------------------------------------------------------------
 void insere(list **l, char info[]);
 void init(list **l);
 void exibe(list *l);
 list* busca(list *l, char info[]);
 void exclui(list **l, char nome[]);
 //--------------------------------------------------------------------------
 void init(list **l) {
 *l = NULL;
 }
 //--------------------------------------------------------------------------
 void insere(list **l, char info[]) {
 list *aux, *nova = (list*) malloc(sizeof (list));
 strcpy(nova->nome, info);
 nova->ant = nova->prox = NULL;
 if (*l == NULL)
 *l = nova;
 else {
 aux = *l;
 while (aux->prox != NULL) {
 aux = aux->prox;
 }
 nova->ant = aux;
 aux->prox = nova;
 }
 }
 //--------------------------------------------------------------------------
 void exibe(list *l) {
 while (l != NULL) {
 printf("[%s]\n", l->nome);
 l = l->prox;
 }
 }
 //--------------------------------------------------------------------------
 list* busca(list *l, char info[]) {
 while (l != NULL && strcmp(info, l->nome) != 0) {
 l = l->prox;
 }
 return l;
 }
 //--------------------------------------------------------------------------
 list* getFim(list *l) {
 while (l->prox != NULL)
 l = l->prox;
 return l;
 }
 //--------------------------------------------------------------------------
 void exclui(list **l, char nome[]) {
 list *aux = busca(*l, nome);
 if ((*l)->prox == NULL) {
 *l = NULL;
 free(aux);
 } else if (aux->ant == NULL) {
 *l = (*l)->prox;
 free(aux);
 } else if (aux->prox == NULL) {
 aux->ant->prox = NULL;
 aux->ant = NULL;
 free(aux);
 } else {
 aux->ant->prox = aux->prox;
 aux->prox->ant = aux->ant;
 free(aux);
 }
 }
 //--------------------------------------------------------------------------
 void OrdenaShakeSort(list **l) {
 list *tl2 = getFim(*l), *inicio = *l, *i = inicio;
 char aux[40];
 while (tl2 != inicio) {
 i = inicio;
 while (i != tl2) {
 if (strcmp(i->nome, i->prox->nome) > 0) {
 strcpy(aux, i->nome);
 strcpy(i->nome, i->prox->nome);
 strcpy(i->prox->nome, aux);
 }
 i = i->prox;
 }
 tl2 = tl2->ant;
 i = tl2;
 while (i != inicio) {
 if (strcmp(i->nome, i->ant->nome) < 0) {
 strcpy(aux, i->nome);
 strcpy(i->nome, i->ant->nome);
 strcpy(i->ant->nome, aux);
 }
 i = i->ant;
 }
 inicio = inicio->prox;
 }
 }
 //--------------------------------------------------------------------------
 void selecaoDireta(list **l) {
 char menor[40];
 list *i = *l, *j = NULL;
 while (i->prox != NULL) {
 strcpy(menor, i->nome);
 j = i->prox;
 while (j != NULL) {
 if (strcmp(j->nome, menor) < 0) {
 strcpy(menor, j->nome);
 strcpy(j->nome, i->nome);
 strcpy(i->nome, menor);
 }
 j = j->prox;
 }
 i = i->prox;
 }
 }
 //--------------------------------------------------------------------------
#ifdef	__cplusplus
}
#endif
#endif	/* LISTADE_H */
main.cpp
/* 
 * File: main.cpp
 * Author: joao
 *
 * Created on 19 de Abril de 2016, 22:03
 */
#include <cstdlib>
#include"ListaDE.h"
using namespace std;
/*
 * 
 */
int main(int argc, char** argv) {
 list *l;
 init(&l);
 insere(&l,"joao");
 insere(&l,"amanda");
 insere(&l,"maria");
 insere(&l,"osmar");
 insere(&l,"camila");
 insere(&l,"luciana");
 selecaoDireta(&l);
 exibe(l);
 return 0;
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais