Last active
August 3, 2023 18:57
-
-
Save wsricardo/aaefbb6924c8119811c35db3a2cb8673 to your computer and use it in GitHub Desktop.
Estruturas de Dados - Lista encadeada
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
/* | |
* Regras | |
* Lista inserções no inicio, fim ou meio da lista. | |
* | |
*/ | |
typedef int Chave; | |
struct node { | |
Chave chave; // dado armazenado. | |
struct node *prox; //ponteiro pro próximo no. | |
}; | |
typedef struct node *Lista; | |
void insere(Lista *l, Chave v); | |
int remover(Lista *l); | |
void exibi(Lista l); | |
int main(){ | |
Lista l; | |
insere(&l, 2); | |
insere(&l, 64); | |
insere(&l, 5); | |
//printf("\nAddress list %p\n", l); | |
//printf("\nAddress apontado pelo próximo da lista %p\n", l->prox ); | |
exibi(l); | |
remover(&l); | |
exibi(l); | |
return 0; | |
} | |
// Insere no fim da lista. | |
void insere( Lista *l, Chave v ) { | |
Lista *p; | |
Lista novo; | |
novo = malloc( sizeof( Lista ) ); | |
novo->chave = v; | |
novo->prox = NULL; | |
p = l; | |
while ((*p) != NULL ) { | |
p = &(*p)->prox; | |
} | |
*p = novo; | |
} | |
void exibi(Lista l) { | |
Lista p; | |
p = l; | |
//printf("\nExibir\n"); | |
while ( p != NULL ) { | |
printf("\n[ %p ] chave %d\n", p, p->chave ); | |
p = p->prox; | |
} | |
} | |
// Remove elemento no fim da lista. | |
int remover( Lista *l) { | |
Lista *p; | |
p = l; | |
if ( (*p) == NULL ) return -1; | |
while ( (*p)->prox != NULL ){ | |
p = &(*p)->prox; | |
} | |
free((*p)); | |
*p = NULL; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment