include stdio include string include stdlib include ctype struct Eleme

 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct Element
{
struct Element *next;
char *u;
};
struct Element* InitSingleLinkedList()
{
struct Element *list;
list = (struct Element*)malloc(sizeof(struct Element));
list->next=NULL;
list->u=malloc(100*sizeof(char));;
memset(list->u,'\0' , 100*sizeof(char));
return list;
}
struct Element *bsort(struct Element *list)
{
int SwapsInIteration=1;
struct Element *last;
char*swap;
while(SwapsInIteration>0)
{
SwapsInIteration=0;
for(last=list->next;last->next!=NULL;last=last->next)
{
if(strlen(last->u)>(strlen(last->next->u)))
{
swap=last->u;
last->u=last->next->u;
last->next->u=swap;
SwapsInIteration+=1;
}
}
}
return list;
}
int main (int argc, char const *argv[])
{
unsigned long int i=0,len,inword=0,j=0;
char str[100];
struct Element *new;
struct Element *last;
struct Element *list;
list=InitSingleLinkedList();
gets(str);
len=strlen(str);
last=list;
if (len>0 && !isspace(str[0]))
{
inword=1;
}
for (i=0;i<len;i++)
{
if(inword==1)
{
new = InitSingleLinkedList();
while (i<len && (!isspace(str[i])))
{
new->u[j]=str[i];
i++;
j++;
}
inword=0;
last->next=new;
last=new;
}
if(inword==0 && (!isspace(str[i])))
{
inword=1;
j=0;
if(i>0) i--;
}
}
bsort(list);
for (last=list->next;last->next!=NULL;last=new)
{
new=last->next;
printf("%s ",last->u);
}
printf("%s\n",last->u);
for (last=list;last!=NULL;)
{
new=last->next;
free(last->u);
free(last);
last=new;
list=last;
}
return 0;
}