麻烦各位帮我看看这个程序,该怎么改好呢?
#include <stdio.h> #include<malloc.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct WORD)
struct WORD
{
char word[15];
struct WORD *next;
};
struct WORD *sort(struct WORD *head)
{
struct WORD *p, *q, *temp;
p = head-> next;
if(!p)
return head;
while(p-> next)
{
q = head;
while (strcmp(q-> next-> word, p-> next-> word) < 0)
q = q-> next;
if(q != p)
{
temp = q-> next;
q-> next = p-> next;
p-> next = p-> next-> next;
q-> next-> next = temp;
}
else
p = p-> next;
}
return head;
}
struct WORD *create()
{
FILE *fp;
char str[15];
struct WORD *head=(struct WORD*)malloc(LEN);
struct WORD *p = head;
int n;
head-> next=NULL;
fp = fopen("e:\\test.txt", "r+");
if (fp == NULL)
{
printf("can't open the file\n");
exit(1);
}
n = fscanf(fp, "%s", str);
while(n != EOF)
{
struct WORD *node=(struct WORD*)malloc(LEN);
node-> next=NULL;
strcpy(node-> word, str);
p-> next = node;
p = node;
n = fscanf(fp, "%s", str);
}
return(head);
}
void print(struct WORD *head)
{
FILE *fp;
struct WORD *p, *q;
p = head-> next;
fp = fopen("e:\\test.txt", "r+");
if (fp == NULL)
{
printf("can't open the file\n");
exit(1);
}
while(p != NULL)
{
q = p;
fprintf(fp, "%s ", p-> word);
p = p-> next;
free(q);
}
free(head);
}
int main()
{
struct WORD *head1;
head1 = create();
head1 = sort(head1);
print(head1);
system("pause");
return 0;
}
在LINUX下运行提示是:在函数“sort”中:
15:错误:赋值时类型不兼容
23:错误:“p”未声明(在此函数内第一次使用)
23:错误:(即使在一个 函数内多次出现,每个未声明的标识符在其所在的函数内也只报告一次)
该怎么改啊,谢谢高手帮忙了