-
Notifications
You must be signed in to change notification settings - Fork 2
/
ADT_Linklist_sort_data.c
75 lines (61 loc) · 1.84 KB
/
ADT_Linklist_sort_data.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
// author: jaydattpatel
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node * next;
};
struct Node* top = NULL;
void Traversal(struct Node *tp)
{
int i=0;
while (tp != NULL)
{
printf("%d.Element: %d\n",i+1, tp->data);
tp = tp->next;
i++;
}
}
void sortlink(struct Node* top)
{
struct Node *next = (struct Node*) malloc(sizeof(struct Node));
struct Node *tp = (struct Node*) malloc(sizeof(struct Node));
int a,swap=0;
tp = top;
while (tp->next != NULL) // compare current->next nod is not null
{
swap=0;
next = tp->next; //copy pointer next of current
if((tp->data)>(next->data)) //compare data of current and next node
{
a = tp->data;
tp->data = next->data;
next->data=a;
swap=1; //swap executed
}
if(swap == 1) //if swap executed then again move top or start nod for next loop to check sort data entirly
tp = top;
else
tp = tp->next;
}
}
struct Node* push(struct Node* tp, int x)
{
struct Node* n = (struct Node*) malloc(sizeof(struct Node));
n->data = x;
n->next = tp;
tp = n;
return tp;
}
int main()
{
int a,i;
for(i=0;i<10;i++)
top = push(top, (rand()%1000)); //limit for integer max 999 values
Traversal(top);
sortlink(top);
printf("\n\nAfter sort the data:----------------\n");
Traversal(top);
return 0;
}