Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct lista {
- int n;
- struct lista *prox;
- };
- void inserirInicio(struct lista **celula, int numero) {
- struct lista *novoElemento;
- novoElemento = malloc(sizeof(struct lista));
- novoElemento->n = numero;
- if (*celula == NULL) {
- *celula = novoElemento;
- novoElemento->prox = NULL;
- } else {
- novoElemento->prox = *celula;
- *celula = novoElemento;
- }
- }
- void inserirFinal(struct lista **celula, int numero) {
- struct lista *novoElemento;
- novoElemento = malloc(sizeof(struct lista));
- novoElemento->n = numero;
- struct lista *elementoAtual;
- elementoAtual = *celula;
- while (elementoAtual->prox != NULL) {
- elementoAtual = elementoAtual->prox;
- }
- elementoAtual->prox = novoElemento;
- novoElemento->prox = NULL;
- }
- void printar(struct lista **celula) {
- struct lista *elementoAtual;
- elementoAtual = *celula;
- while (elementoAtual != NULL)
- {
- printf("%d ", elementoAtual->n);
- elementoAtual = elementoAtual->prox;
- }
- printf("\n");
- }
- int main (void) {
- struct lista *head = NULL;
- inserirInicio(&head, 5);
- inserirInicio(&head, 10);
- inserirInicio(&head, 25);
- inserirInicio(&head, 30);
- inserirFinal(&head, 3);
- printar(&head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement