Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct no
- {
- int dado;
- struct no *ant;
- struct no *prox;
- } tno;
- typedef struct lista
- {
- tno *com;
- } tlista;
- void IniciaLista(tlista *l)
- {
- l->com = NULL;
- }
- tno* AlocaNo(int dado)
- {
- tno *p;
- p = (tno*)malloc(sizeof(tno));
- if (p)
- {
- p->dado = dado;
- p->ant = NULL;
- p->prox = NULL;
- }
- return p;
- }
- void InsereNo(tlista *l, int dado)
- {
- tno *aux, *p, *guardaAux;
- p = AlocaNo(dado);
- if(p)
- {
- aux = l->com;
- while((aux)&&(aux->dado < dado)){
- if(aux->prox == NULL){
- guardaAux = aux;
- }
- aux = aux->prox;
- }
- if(aux) {//insere elemento entre dois elementos
- p->ant = aux->ant;
- p->prox = aux;
- aux->ant = p;
- aux->ant->prox = p;
- }else{
- if(!l->com) //!aux, insere o primeiro elemento
- {
- l->com = p;
- }else{
- p->ant = guardaAux;
- guardaAux->prox = p;
- }
- }
- }
- }
- void ExcluiNo(tlista *l, int dado){
- tno *aux, *guardaAux;
- aux = l->com;
- guardaAux = NULL;
- if(!l->com) printf("\nLista Vazia!\n");
- else{
- while((aux->prox)&&(aux->dado != dado)){
- aux = aux->prox;
- }
- if(aux){
- if(!aux->ant)//o primeiro elemento
- {
- l->com = aux->prox;
- l->com->ant = NULL;
- free(aux);
- }
- else //nao eh o primeiro elemento
- {
- if(!aux->prox){//eh o ultimo
- aux->ant->prox = NULL;
- free(aux);
- }else{//nao eh o ultimo
- aux->ant->prox = aux->prox;
- aux->prox->ant = aux->ant;
- free(aux);
- }
- }
- }
- }
- }
- void mostraLista(tlista l)
- {
- tno *aux;
- aux = l.com;
- if(!aux) printf ("\nLista vazia\n");
- else
- {
- while(aux)
- {
- printf("\n %d",aux->dado);
- aux = aux->prox;
- }
- }
- }
- int main()
- {
- int i, n, dado, ExDado, status;
- tlista l;
- tno *p;
- IniciaLista(&l);
- printf("Digite a quantidade de elementos da lista:");
- scanf("%d", &n);
- for(i=0; i<n; i++)
- {
- printf("Digite o elemento:");
- scanf("%d", &dado);
- InsereNo(&l, dado);
- }
- printf("Digite o elemento que deseja excluir:");
- scanf("%d", &ExDado);
- ExcluiNo(&l, ExDado);
- mostraLista(l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement