用气泡法对字符串进行排序啊!
#include
#include
#include
#define N 20 // the length of English words
struct node {
char en[N];
struct node *next;
};
void print(struct node *p);
void sort(struct node *p);
void del(struct node *p);
void swap(struct node *s1, struct node *s2);
void main() {
struct node *h=NULL, *p, *p1;
char a[N];
int z = 0;
while(z<5) {
printf("Input the word:");
gets(a);
p=(struct node *) malloc(sizeof(struct node));
strcpy(p->en,a);
if(h==0) {
h = p;
p1 = p;
}
else {
p1->next = p;
p1 = p;
}
z = z + 1;
}
p->next = NULL;
// print(h);
sort(h);
// print(h);
del(h);
print(h);
}
void sort(struct node *p) {
// bubble method
struct node *p1, *p2;
p1 = p;
while(p1) {
p2 = p1->next;
while(p2) {
if(strcmp((p2->en),(p1->en))>0) {
swap(p1,p2);
}
p2 = p2->next;
}
p1 = p1->next;
}
}
void swap(struct node *s1, struct node *s2) {
struct node temp1,temp2;
temp1 = *s1;
temp1.next = s2->next;
temp2 = *s2;
temp2.next = s1->next;
*s1 = temp2;
*s2 = temp1;
}
void del(struct node *p) {
struct node *p1,*p2;
p1 = p->next;
while(p1) {
p2 = p1->next;
while(p2) {
if(strcmp((p2->en),(p1->en))==0) {
p1->next = p1->next->next;
free(p2);
}
else {
break;
}
p2 = p1->next;
}
p1 = p1->next;
}
}
void print(struct node *p) {
while(p) {
puts(p->en);
p = p->next;
}
}
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=951862