Skip to content

Commit 59cbf05

Browse files
Merge pull request #8 from vedant1003k/patch-1
Create sortingInll.c
2 parents 871ef50 + 290d5c0 commit 59cbf05

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Linked List/sortingInll.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct node
4+
{
5+
int data;
6+
struct node *next;
7+
} * head, *temp;
8+
void creatLL(int);
9+
void display();
10+
void sortThell(int);
11+
int main()
12+
{
13+
int n, f, place;
14+
printf("enter the numbe rof node");
15+
scanf("%d", &n);
16+
creatLL(n);
17+
sortThell(n);
18+
display();
19+
}
20+
void creatLL(int n)
21+
{
22+
int i, num;
23+
struct node *newNode;
24+
if (n >= 1)
25+
{
26+
head = (struct node *)malloc(sizeof(struct node));
27+
if (head != NULL)
28+
{
29+
printf("enter the data");
30+
scanf("%d", &num);
31+
head->data = num;
32+
head->next = NULL;
33+
temp = head;
34+
35+
for (i = 2; i <= n; i++)
36+
{
37+
newNode = (struct node *)malloc(sizeof(struct node));
38+
printf("entet the data");
39+
scanf("%d", &num);
40+
newNode->data = num;
41+
temp->next = newNode;
42+
newNode->next = NULL;
43+
temp = newNode;
44+
}
45+
}
46+
}
47+
}
48+
void display()
49+
{
50+
struct node *tmp;
51+
tmp = head;
52+
printf("after sorting elements are :\n");
53+
while (tmp != NULL)
54+
{
55+
printf("%d\t", tmp->data);
56+
tmp = tmp->next;
57+
}
58+
}
59+
void sortThell(int n)
60+
{
61+
struct node *nextNode;
62+
struct node *prevNode;
63+
for (int i = 0; i < n; i++)
64+
{
65+
prevNode=head;
66+
nextNode=prevNode->next;
67+
for(int j=i+1;j<n;j++)
68+
{
69+
if(prevNode->data>nextNode->data)
70+
{
71+
int t=prevNode->data;
72+
prevNode->data=nextNode->data;
73+
nextNode->data=t;
74+
}
75+
prevNode=nextNode;
76+
nextNode=nextNode->next;
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)