-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSearch_And_Sort_In_SLL.c
188 lines (140 loc) · 5.1 KB
/
Search_And_Sort_In_SLL.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
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
183
184
185
186
187
188
//To implement linear search and selection sort in a singly linked list
//12.03.19
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <conio.h>
//Macro for easy use of malloc for memory allocation
#define Malloc(a) (a *)(malloc(sizeof(a)))
//Self referential structure for node creation in linked list
typedef struct nodeStructure{
int data; //data storage
struct nodeStructure *next; //points to next node in list
};
//Declaration of first and last node pointers
struct nodeStructure *firstNode;
struct nodeStructure *lastNode;
//Function declaration and prototypes for operation
int getNodeData();
void makeNewList();
//To display the nodes on the console
void getNodes();
//Operations on linked list
void selectionSort();
void linearSearch();
//main()opens
void main(){
makeNewList(); //Gets new list from the user
system("cls");
printf("Original list is :\n");
getNodes();
//Implements selection sort on the list and displays it
printf("Sorted list is : \n");
selectionSort();
getNodes();
getch();
//Implements linear search on the linked list
linearSearch();
getch();
} //main closes
//Function definitions for operations
int getNodeData(){
int value;
printf("Enter value in the node : ");
scanf("%d", &value);
return value;
}
void makeNewList(){
//Declare new node in local scope
struct nodeStructure *newNode;
char wantMoreNodes = 'y';
while(wantMoreNodes == 'y'){
//Allocate memory for the new node and get it's value
newNode = Malloc(struct nodeStructure);
newNode->next = NULL;
newNode->data = getNodeData();
//If the list exists attach the new node to the end of the list
if (firstNode){
lastNode->next = newNode;
lastNode = newNode;
}
//If the list doesn't exist initialize the new node as the first and last nodes
else{
firstNode = newNode;
lastNode = firstNode;
}
//Check if user wants more nodes
printf("Want more nodes y/n\n");
scanf("%s", &wantMoreNodes);
}
}
void getNodes(){
//Declare new node in local scope to traverse the list
struct nodeStructure *scanNode;
//Initialize scanNode as the first node for traversal
scanNode = firstNode;
//Prints the node values along with denoting the start and end of list
printf("HEAD -> ");
while(scanNode != NULL){
printf("%d -> ", scanNode->data);
scanNode = scanNode->next;
}
printf("NULL\n");
}
void selectionSort(){
int minimumValue, swapNumber;
//Declare nodes in local scope to traverse the list
struct nodeStructure *currentNode;
struct nodeStructure *scanNode;
struct nodeStructure *leastValueNode;
currentNode = firstNode;
while(currentNode->next != NULL){
minimumValue = currentNode->data;
scanNode = currentNode->next;
//Always initializes the node with the least value in the list as NULL
leastValueNode = NULL;
while(scanNode){
if(scanNode->data < minimumValue){
minimumValue = scanNode->data;
leastValueNode = scanNode;
}
scanNode = scanNode->next;
}
//Swaps the data value of the nodes if a node with lesser value is found
if(leastValueNode){
swapNumber = currentNode->data;
currentNode->data = leastValueNode->data;
leastValueNode->data = swapNumber;
}
currentNode = currentNode->next;
}
}
void linearSearch(){
int valueIsFound = 0;
int searchValue, searchValuePosition;
int nodeCounter = 0;
//Get the value to search from the user
printf("Enter value to search : ");
scanf("%d",&searchValue);
//Declare new node in local scope to traverse the list
struct nodeStructure *scanNode;
//Initailize scanNode as the first node for traversal
scanNode = firstNode;
while(scanNode != NULL){
nodeCounter++; //Keeps the index of each node
if(searchValue == scanNode->data){
valueIsFound = 1;
searchValuePosition = nodeCounter; //Stores the index of the node at which search value is found
}
scanNode = scanNode->next;
}
//Check if searched value is found or not
if(valueIsFound){
printf("The given value is found at %d node.\n", searchValuePosition);
}
else{
printf("Node not found. Search unsuccessful\n");
}
}
//Written in ANSI C for the GNU-GCC compiler
//Elit Altum