Buscar

Tad Lista Dinâmica Circular em C -(Insere (Inicio,Fim),Busca,Exclui,Ordena(Alfabética)

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

TADCircL.h
/* 
 * File: TADCircL.h
 * Author: joao
 *
 * Created on 21 de Abril de 2016, 18:53
 */
#ifndef TADCIRCL_H
#define	TADCIRCL_H
#include<string.h>
#include<stdio.h>
#ifdef	__cplusplus
extern "C" {
#endif
 //ED----------------------------------------------------------------------------
 /*No ou celula da lista*/
 struct List {
 char nome[40];
 struct List *ant, *prox;
 };
 typedef struct List cList;
 /*Descritor*/
 struct desc {
 cList *inicio;
 cList *fim;
 int tl;
 };
 //Prototipos--------------------------------------------------------
 void init(desc *desc);
 void insereInicio(desc *desc, char nome[]);
 void insereFim(desc *desc, char nome[]);
 cList* busca(desc desc, char chave[]);
 void exclui(desc *desc, char chave[]);
 char isEmpty(desc *desc);
 void selecaoDireta(desc *d);
 int getTl(desc d);
 //--------------------------------------------------------------------------
 void init(desc *desc) {
 (*desc).inicio = (*desc).fim = NULL;
 (*desc).tl = 0;
 }
 //--------------------------------------------------------------------------
 char isEmpty(desc *desc) {
 return ((*desc).inicio == NULL);
 }
 //--------------------------------------------------------------------------
 int getTl(desc d){
 return d.tl;
 }
 //--------------------------------------------------------------------------
 void insereInicio(desc *desc, char nome[]) {
 cList *nova = (cList*) malloc(sizeof (cList));
 strcpy(nova->nome, nome);
 nova->ant = nova->prox = NULL;
 if (isEmpty(desc)) {
 nova->prox = nova;
 nova->ant = nova;
 (*desc).inicio = (*desc).fim = nova;
 } else {
 nova->prox = (*desc).inicio;
 nova->ant = (*desc).fim;
 (*desc).fim->prox = nova;
 (*desc).inicio->ant = nova;
 (*desc).inicio = nova;
 }
 (*desc).tl++;
 }
 //--------------------------------------------------------------------------
 void insereFim(desc *desc, char nome[]) {
 cList *nova = (cList*) malloc(sizeof (cList));
 strcpy(nova->nome, nome);
 nova->ant = nova->prox = NULL;
 if (isEmpty(desc)) {
 nova->prox = nova;
 nova->ant = nova;
 (*desc).inicio = (*desc).fim = nova;
 } else {
 nova->ant = (*desc).fim;
 nova->prox = (*desc).inicio;
 (*desc).fim->prox = nova;
 (*desc).inicio->ant = nova;
 (*desc).fim = nova;
 }
 (*desc).tl++;
 }
 //--------------------------------------------------------------------------
 cList* busca(desc desc, char chave[]) {
 cList *aux = desc.inicio;
 while (aux != NULL && strcmp(aux->nome, chave) != 0)
 aux = aux->prox;
 return aux;
 }
 //--------------------------------------------------------------------------
 void exclui(desc *desc, char chave[]) {
 cList *aux = busca(*desc, chave);
 if (aux != NULL) {
 if ((*desc).inicio == (*desc).fim) {//Apenas um elemento na lista
 (*desc).inicio = (*desc).fim = NULL;
 free(aux);
 } else if (aux->ant == (*desc).fim) {//primeiro elemento da lista
 (*desc).inicio = aux->prox;
 (*desc).inicio->ant = (*desc).fim;
 (*desc).fim->prox = (*desc).inicio;
 free(aux);
 } else if (aux->prox == (*desc).fim) {//ultimo elemento da lista
 (*desc).fim = aux->ant;
 (*desc).fim->prox = (*desc).inicio;
 free(aux);
 } else {
 aux->ant->prox = aux->prox;
 aux->prox->ant = aux->ant;
 free(aux);
 }
 (*desc).tl--;
 }
 }
 //------------------------------------------------------------------------------
 void exibe(desc desc) {
 cList *aux = desc.inicio;
 while (desc.tl-- != 0) {
 printf("\n[%s]", aux->nome);
 aux = aux->prox;
 }
 }
 //------------------------------------------------------------------------------
 void selecaoDireta(desc *d) {
 char menor[40];
 cList *i = (*d).inicio, *j = NULL;
 while (i->prox != (*d).inicio) {
 strcpy(menor, i->nome);
 j = i->prox;
 while (j != (*d).inicio) {
 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	/* TADCIRCL_H */
main.cpp
/* 
 * File: main.cpp
 * Author: joao
 *
 * Created on 16 de Abril de 2016, 13:34
 */
#include <cstdlib>
#include"TADCircL.h"
using namespace std;
/*
 * 
 */
int main(int argc, char** argv) {
 desc desc;
 init(&desc);
 
 insereFim(&desc, "Joao");
 insereFim(&desc, "Amanda");
 insereFim(&desc, "Andre");
 insereFim(&desc, "Osmar");
 insereFim(&desc, "Luciana");
 insereInicio(&desc,"Maria");
 exibe(desc);
 //exclui(&desc,"Andre");
 printf("\n<[%s]>",desc.inicio->ant->nome);
 printf("\n<[%s]>",desc.fim->prox->nome);
 selecaoDireta(&desc);
 exibe(desc);
 
 
 /*
 
 
*/
 return 0;
}

Teste o Premium para desbloquear

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

Outros materiais

Materiais relacionados

Perguntas relacionadas

Perguntas Recentes