[code]#include<stdio.h>
#include<string.h>
#include<malloc.h>
#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;
}
}
[/code]
#include<string.h>
#include<malloc.h>
#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;
}
}
[/code]
本文介绍了一个使用C语言实现的程序,该程序能够接收用户输入的英文单词,并通过冒泡排序法对这些单词进行排序,同时去除重复的单词。程序首先构建一个链表来存储单词,然后对链表中的元素进行排序并去重,最后输出排序后的单词列表。
852

被折叠的 条评论
为什么被折叠?



