Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct celula {
- int valor;
- struct celula *prox;
- };
- void InsereNaLista (struct celula * ini) {
- // inserir elementos em ordem!
- struct celula *anterior = NULL;
- struct celula *novo;
- novo = malloc (sizeof(struct celula));
- printf("Digite o valor: ");
- scanf("%d", &novo->valor);
- // a lista está vazia
- if (ini->prox == NULL) {
- ini->prox = novo;
- novo->prox = NULL;
- }
- // a lista nao esta vazia
- else {
- while (ini->prox != NULL && ini->valor < novo->valor) {
- anterior = ini;
- ini = ini->prox;
- }
- novo->prox = ini;
- if (anterior == NULL) ini = novo;
- else anterior->prox = novo;
- }
- }
- void ImprimeListaCompleta(struct celula *inicio) {
- struct celula *p;
- printf("Teste lista completa: ");
- for (p = inicio->prox; p != NULL; p = p->prox) printf("%d ", p->valor);
- printf("\n\n");
- }
- int main() {
- struct celula *inicio;
- inicio = malloc (sizeof(struct celula));
- inicio->prox = NULL;
- inicio->valor = -666;
- // quero acrescentar quantos elementos eu desejar a minha lista ligada
- // quero inseri-los em ordem!
- while (1) {
- InsereNaLista(inicio);
- ImprimeListaCompleta(inicio);
- }
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement