-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ordenar
88 lines (81 loc) · 1.27 KB
/
Ordenar
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
#include <stdio.h>
#include <stdlib.h>
//############################
//NODE OF THE LSIT
typedef struct node{
int element;
struct node *next;
}node;
//############################
void printList(node *head)
{
node *aux = head;
while(aux!=NULL)
{
printf("%d ",aux->element );
aux = aux->next;
}
}
void swap(node *a, node *b)
{
int temp = a->element;
a->element = b->element;
b->element = temp;
}
// THIS FUNCTION SORTED A LIST
void sorted(node *head)
{
int swapped, i;
node *ptr1;
node *lptr = NULL;
if (head == NULL)
{
return;
}
do
{
swapped = 0;
ptr1 = head;
while(ptr1->next != lptr)
{
//printf("antes\n"); DEBUG
// PROBLEM SEGMENT FALT IS HERE:
if (ptr1->element > ptr1->next->element)
{
//printf("antes\n"); DEBUG
swap(ptr1 , ptr1->next);
swapped =1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
void *readList(node **head, int element)
{
node *newNode = malloc(sizeof(node));
newNode->next = *head;
newNode->element = element;
*head = newNode;
return *head;
}
void *read(node *head)
{
int i;
if (scanf("%d", &i)==EOF)
{
return head;
}
else
{
head = readList(&head,i);
}
read(head);
}
int main(void)
{
node *head = NULL;
head = read(head);
sorted(head);
printList(head);
}