-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
84 lines (76 loc) · 1.21 KB
/
Copy pathsort.c
File metadata and controls
84 lines (76 loc) · 1.21 KB
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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void printlist(struct Node *n)
{
if(n==NULL)
{
printf("list is empty\n");
return;
}
while(n!=NULL)
{
printf("%d->",n->data);
n=n->next;
}
printf("\n");
}
void push(struct Node **head_ref,int new_data)
{
struct Node *new_node=(struct Node *)malloc(sizeof(struct Node));
new_node->data=new_data;
new_node->next=NULL;
struct Node *temp=*head_ref;
if(temp==NULL)
{
*head_ref=new_node;
return;
}
while(temp->next!=NULL)
temp=temp->next;
temp->next=new_node;
return;
}
void sort(struct Node **head_ref)
{
int temp;
struct Node *cur=(*head_ref);
struct Node *next=NULL;
while(cur!=NULL)
{
next=cur->next;
while(next!=NULL)
{
if(cur->data>next->data)
{
temp=cur->data;
cur->data=next->data;
next->data=temp;
}
next=next->next;
}
cur=cur->next;
}
}
int main()
{
struct Node *head=NULL;
struct Node *first=head;
struct Node *last=head;
printf("Created list is:");
printlist(head);
push(&head,30);
push(&head,40);
push(&head,50);
push(&head,20);
printf("Created list is:");
printlist(head);
sort(&head);
printf("Created list is:");
printlist(head);
return 0;
}