-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssign1Aa.c
126 lines (104 loc) · 2.53 KB
/
Assign1Aa.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
//To create a linked list where each node contain a part of student's name which distinguishes students,and then sort the linked list and then print full names.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max(a,b) ((a)>(b)?a:b) //max function made using macros.
#define min(a,b) ((a)<(b)?a:b) //min function made using macros.
struct node{ //node is basic unit of linked list.
char c2[1000];
char c3[1000];
struct node *next;
};
struct node *head=NULL,*prev;
int n;
void sort() //sort function made to sort the list.
{
struct node *r,*s,*prev2,*prev1,*q,*p;
s=head;
r=s;
int m=1;
while(m!=n) //while loop for carrying out insertion sort.
{
q=r;
p=s->next;
prev2=r=prev1=s;
while(p!=NULL) //while loop for carrying out the comparisons of the data in the list.
{
if(strcmp(r->c2,p->c2)>0)
{
r=p;
prev1=prev2;
}
prev2=p;
p=p->next;
}
prev1->next=r->next;
if(r!=s)r->next=s;
else
s=s->next;
if(s==head)
head=r;
else
q->next=r;
m++;
}
}
void print() //print function made to print the full names present in the list.
{
struct node *p;
p=head;
while(p!=NULL)
{
printf("%s\n",p->c3);
p=p->next;
}
}
int main()
{
struct node *p;
printf("Enter no. of students->");
scanf("%d",&n); //n is no. of students.
char c[n][1000]; //c array made to take the names of the students.
char c1[n][1000]; //c1 array made to take the distinguishing criteria of students.
int i=0,m=0,j,k;
while(i<n) //while loop made to take the names of students.
{
scanf("%s",c[i]);
i++;
}
for(i=0;i<n;i++) //for loop made to make the comparisons and then store the required criteria of students in c1 array.
{
m=0;
for(j=0;j<n;j++)
{
if(i==j)continue;
for(k=0;k<max(strlen(c[i]),strlen(c[j]));k++) //for loop made to iterate over the length of the comparators.
{
if(c[i][k]!=c[j][k])
{
m=max(m,k);
break;
}
}
if(c[i][k]=='\0'&&c[j][k]=='\0')
m=k-1;
}
strcpy(c1[i],c[i]); //required data copied.
c1[i][m+1]='\0';
}
i=0;
for(i=0;i<n;i++) //for loop made to enter the data in the list.
{
p=(struct node*)malloc(sizeof(struct node));
strcpy(p->c2,c1[i]);
strcpy(p->c3,c[i]);
p->next==NULL;
if(head==NULL)head=p;
else
prev->next=p;
prev=p;
}
sort(); //sort function called.
print(); //print function called.
return 0;
}