[C] Listas Duplamente Encadeadas
Publicado por Enzo de Brito Ferber 09/04/2006
[ Hits: 9.536 ]
Homepage: http://www.maximasonorizacao.com.br
Codigo de fácil entendimento com funçoes de inserçao em ordem crescente, retirada, e outras :).... Muito bom
/*
* Programa: Listas Duplamente Encadeadas
* Arquivo : lde.c
* Autor : Enzo Ferber 'Slackware_10'
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MALLOC(a) (a*)malloc(sizeof(a))
struct node{
int info;
struct node *next;
struct node *prior;
};
struct node *head;
struct node *last;
void display(void);
void menu(void);
void push(int);
void pop(int);
void ins(void);
void del(void);
void clear(void);
void flush(void);
int length(void);
void push(int valor){
struct node *novo;
struct node *current;
current = head;
novo = MALLOC(struct node);
if(!head){
novo->info = valor;
novo->next = NULL;
novo->prior = NULL;
head = last = novo;
}
if(valor > last->info){
novo->info = valor;
novo->next = NULL;
novo->prior = last;
last->next = novo;
last = novo;
return;
}
if(valor < head->info){
novo->info = valor;
novo->next = head;
novo->prior = NULL;
head->prior = novo;
head = novo;
return;
}
while(current){
if(current->info > valor){
novo->info = valor;
novo->next = current;
novo->prior = current->prior;
current->prior->next = novo;
current->prior = novo;
}
current = current->next;
}
}
void pop(int valor){
struct node *current;
current = head;
if(valor == head->info){
head = head->next;
free(head->prior);
return;
}
if(valor == last->info){
last = last->prior;
free(last->next);
return;
}
while(current){
if(current->info == valor){
current->prior->next = current->next;
current->next->prior = current->prior;
free(current);
return;
}
current = current->next;
}
}
void display(void){
struct node *aux;
aux = head;
if(!head){
clear();
printf("Pilha vazia.");
getchar();
getchar();
return;
}
clear();
while(aux){
printf("%d\n", aux->info);
aux = aux->next;
}
getchar();
getchar();
}
void ins(void){
int valor;
clear();
printf("Valor: ");
flush();
scanf("%d", &valor);
push(valor);
}
void del(void){
int valor;
clear();
printf("Valor: ");
flush();
scanf("%d", &valor);
pop(valor);
}
void flush(void){
fflush(stdin);
}
void clear(void){
system("cls");
}
void menu(void){
int op;
while(1){
clear(); //limpa a tela
printf("\n\tLitas duplamente encadeadas\n\n");
printf("\t1. Inserir\n");
printf("\t2. Retirar\n");
printf("\t3. Mostar\n");
printf("\t4. Sair\n\n");
printf("\tSua opcao: ");
flush(); //limpa o buffer de entrada do teclado
scanf("%d", &op); //le de 'stdin' um valor inteiro e armazena em 'op'
switch(op){ //faz um 'switch' com o valor de op
case 1:
ins();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
free(head);
free(last);
exit(0);
default:
clear();
printf("Opcao invalida");
getchar();
}
}
}
int main(void){
head = last = NULL;
menu();
return 0;
}
Biblioteca estática para manipulação de pilhas
Jogo Simon (Genius) - com gráficos
Função "Partição de Inteiros" Recursiva SEM Tabela Estática em C
Agenda feita em C usando árvore binária
XFCE - quase um Gnome ou Plasma mas muito mais leve
LXQT - funcional para máquinas pererecas e usuários menos exigentes
Instalação do K3s Single-Node com Rancher no Ubuntu 24.04
Usei o NotebookLM para Auditar Logs do Linux
Cinnamon seria a aposta acertada frente às outras interfaces gráficas mais populares?
Como Configurar DNS Reverso (PTR) em Ambientes Linux e Microsoft
Preparando o Ambiente para Administrar o Samba 4 a partir do Windows com RSAT
WiFi Seguro: EAP-TLS com FreeRADIUS e Active Directory (LDAP)
Midia de instalação LM-21.3 não inicializa (5)
O que você está ouvindo agora? [2] (227)









