This repository was archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathc_sorting_algorithm.c
More file actions
182 lines (177 loc) · 3.37 KB
/
c_sorting_algorithm.c
File metadata and controls
182 lines (177 loc) · 3.37 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *mid = NULL;
struct node *tail = NULL;
void list_print_karnewala();
void first_inser(int data);
struct node *first_delete()
{
struct node *tempLink = head;
head = head->next;
return tempLink;
}
int length();
struct node *searching_wala(int key)
{
struct node *mid = head;
if (head == NULL)
{
return NULL;
}
while (mid->data != key) // whole list traverse karne wala
{
if (mid->next == NULL)
{
return NULL;
}
else
{
mid = mid->next; // next line jane wala
}
}
return mid;
}
struct node *delete (int data)
{
struct node *mid = head;
struct node *previous = NULL;
if (head == NULL)
{
return NULL;
}
while (mid->data != data)
{
if (mid->next == NULL) // if last hua to
{
return NULL;
}
else
{
previous = mid;
mid = mid->next;
}
}
if (mid == head)
{
head = head->next;
}
else
{
previous->next = mid->next;
}
return mid;
}
void sorting();
void middleNode();
int main()
{
int i, n;
int key;
printf("\nenter the nodes u want to create \n");
scanf("%d", &n);
// srand(time(0));
for (i = 0; i < n; i++)
{
first_inser(rand() % 100);
}
printf("\nbefore sorting the linked list is \n");
list_print_karnewala();
printf("\n");
printf("\n");
sorting();
printf("\nafter sorting your linked list is \n");
list_print_karnewala();
printf("\n");
printf("\n");
middleNode();
printf("\n");
printf("\n");
printf("\nenter key which u want to search \n");
scanf("%d", &key);
struct node *temp = searching_wala(key);
if (temp != NULL)
{
printf("key is found at node number %d\n", temp->data);
}
else
{
printf("not found sorry \n");
}
return 0;
}
void list_print_karnewala()
{
struct node *ptr = head;
printf(" %d ", ptr->data);
while (ptr->next != NULL)
{
ptr = ptr->next;
printf(" %d ", ptr->data);
}
}
void first_inser(int data)
{
struct node *link = (struct node *)malloc(sizeof(struct node));
link->data = data;
link->next = head;
head = link;
}
int length()
{
int length = 0;
struct node *mid;
for (mid = head; mid != NULL; mid = mid->next)
{
length++;
}
return length;
}
void sorting()
{
int i, j, k;
int temp;
struct node *mid;
struct node *next;
int size = length();
k = size;
for (i = 0; i < size - 1; i++, k--)
{
mid = head;
next = head->next;
for (j = 1; j < k; j++)
{
if (mid->data > next->data)
{
temp = mid->data;
mid->data = next->data;
next->data = temp;
}
mid = mid->next;
next = next->next;
}
}
}
void middleNode()
{
struct node *ptr = head;
int count = 0;
while(ptr != NULL)
{
count++;
ptr = ptr->next;
}
ptr = head;
for (int i = 0; i <count / 2; i++)
{
ptr= ptr->next;
printc("Middle node is : %d ptr->data");
}
}