用链表实现两个字符串的比较,相同则提示the same 并把他们打印出来(只用打印一个就行了);不同提示not the same,然后把两个不同的字符串分别打印出来。
源代码如下: VC6.0编译通过
#include <stdio.h>
#include <stdlib.h>
struct string{
char ch;
struct string *next;
};
int string_cmp(struct string *string1, struct string *string2){
struct string *temp1, *temp2;
temp1 = string1;
temp2 = string2;
while(temp1 != NULL){
if(temp1->ch == temp2->ch){
temp1 = temp1->next;
temp2 = temp2->next;
}
else
return 0;
}
return 1;
}
struct string *string_write(){
char ch;
struct string *str, *head, *tail;
str = head = tail = NULL;
printf("please input the chars end with enter/n");
scanf("%c",&ch);
while(ch != '/n'){
str = (struct string*)malloc(sizeof(struct string));
str->ch =ch;
if(head == NULL){
head = tail = str;
head->next = tail->next = NULL;
}
tail->next = str;
tail = str;
tail->next = NULL;
scanf("%c",&ch);
}
return head;
}
void string_print(struct string *str){
struct string *temp;
temp = str;
while(temp->next != NULL){
printf("%c ",temp->ch);
temp = temp->next;
}
printf("%c/n",temp->ch);
}
int main(){
struct string *str1, *str2;
str1 = string_write();
str2 = string_write();
if(string_cmp(str1, str2)){
printf("these two strings are the same/n");
string_print(str1);
}
else {
printf("these two strings are not the same/n");
string_print(str1);
string_print(str2);
}
return 0;
}