main

3
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("paus e") or input loop */ typedef struct No { int info; struct No * ante; struct No * prox; }no; typedef struct Lista { struct No * inicio; struct No * fim; int qtd; }lista; lista * inicialista(); void inserir (int v, lista*l); no * enderecono(); void imprimir(lista * l); int main(int argc, char *argv[]) { int a = 0; lista * teste; teste = inicialista(); printf("\n Digite Valor: "); while (scanf("%d", &a)) { printf("\n Digite Valor: "); inserir(a,teste); } imprimir(teste); return 0; } void inserir (int v, lista *l){ no * novo = enderecono(); no * atual; no * aux; if (l->inicio == NULL) { l->fim = novo; l->inicio = novo; novo -> ante = NULL; novo -> prox = NULL; } else { atual = l->inicio; aux = l->inicio; while ((atual != NULL) && (atual->info < v)) {

Upload: keiler

Post on 11-Jul-2016

212 views

Category:

Documents


0 download

DESCRIPTION

Lista Duplamente Encadeada

TRANSCRIPT

Page 1: Main

#include <stdio.h>#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

typedef struct No {

int info;struct No * ante;struct No * prox;

}no;

typedef struct Lista {struct No * inicio;struct No * fim;int qtd;

}lista;

lista * inicialista();void inserir (int v, lista*l);no * enderecono();void imprimir(lista * l);

int main(int argc, char *argv[]) {int a = 0;lista * teste;teste = inicialista();

printf("\n Digite Valor: ");while (scanf("%d", &a)) {

printf("\n Digite Valor: ");

inserir(a,teste);}

imprimir(teste);

return 0;}

void inserir (int v, lista *l){no * novo = enderecono();no * atual; no * aux;

if (l->inicio == NULL) {

l->fim = novo;l->inicio = novo;novo -> ante = NULL;novo -> prox = NULL;

}else{

atual = l->inicio;aux = l->inicio;

while ((atual != NULL) && (atual->info < v)) {

Page 2: Main

aux = atual;atual = atual -> prox;

}

if (atual == l->inicio){

novo->prox = l->inicio;atual->ante = novo;l->inicio = novo;novo->ante = NULL;

}else if (atual == NULL){

novo->ante = aux;novo->prox = NULL;l->fim->prox = novo;l->fim = novo;

}else {

// 0 1 2 4novo->ante = aux;novo->prox = atual;aux->prox = novo;atual->ante = novo;

}}

novo->info = v;l->qtd ++;

}

lista * inicialista(){

lista * l = (lista *)malloc(sizeof(lista));if (l == NULL) {

printf("\nMemoria cheia");exit(1);

}

l->inicio = NULL;l->fim = NULL;l->qtd = 0;

return l;}

no * enderecono() {no * novo;novo = (no *)malloc(sizeof(no));

if (novo == NULL) {printf("\nMemoria insuficiente");exit(1);

}

return novo;

Page 3: Main

}

void imprimir (lista *l){no * aux;aux = l->inicio;

//printf("%d", aux->info);

while(aux != NULL){

printf("%d - ", aux->info);aux = aux->prox;

};

fflush(stdin);getchar();

}