-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordenadolista.c
92 lines (87 loc) · 2.03 KB
/
ordenadolista.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct lista
{
int number;
char nome[40];
struct lista *next;
}lista;
lista *inserir(lista *head)
{
int elem;
char nome[40];
printf("Insira o número: ");
scanf("%d", &elem);
getchar();
printf("Insira o nome: ");
scanf("%s", nome);
lista *new;
new = (lista*)malloc(sizeof(lista));
if (new == NULL) return head;
if (head == NULL) /*inserre 1 elemento*/
{
new->number = elem;
strcpy(new->nome, nome);
new->next = NULL;
head = new;
return head;
}
else
{
lista *aux = head;
while (aux->next != NULL && elem > aux->number)
{
aux = aux->next;
}
if (aux->next == NULL) /*O numero a inserir é maior que todos */
{
if (elem > aux->number)
{
new->next = NULL;
new->number = elem;
strcpy(new->nome, nome);
aux->next = new;
}
else
{
new->number = aux->number;
strcpy(new->nome, aux->nome);
new->next = aux->next;
aux->number = elem;
strcpy(aux->nome, nome);
aux->next = new;
}
}else{
new->number = aux->number;
strcpy(new->nome, aux->nome);
new->next = aux->next;
aux->number = elem;
strcpy(aux->nome, nome);
aux->next = new;
}
return head;
}
}
lista *display(lista *head)
{
if (head == NULL)
{
printf("\n########### NULL ###########\n");
}
else{
printf("\nNumber: %d\n", head->number);
printf("Nome: %s", head->nome);
head = head->next;
return display(head);
}
}
int main()
{
lista *head = NULL;
head = inserir(head);
head = inserir(head);
head = inserir(head);
display(head);
return 0;
}