主题:链表内嵌指针如何排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct text
{
char *data;
struct text *next;
};
int countl;
int word;
void InSert(char *line, struct text *txt)
{
struct text *TempCell;
TempCell=(struct text *)malloc(sizeof(struct text));
TempCell->data=(char *)malloc(sizeof(char) * (word+1));
TempCell->data=line;
TempCell->next =txt->next;
txt->next=TempCell;
free(TempCell);
free(TempCell->data);
TempCell=NULL;
}
void BubbleSort(struct text *txt,int n)
{
int i,j,s;
char *temp;
struct text *p;
p=(struct text *)malloc(sizeof(struct text));
p->data=(char *)malloc(sizeof(char) * (word+1));
temp=(char *)malloc(sizeof(char)*(word+1));
if (p!=NULL) {
for(i=0;i <countl;i++)
for(j=0, p=txt->next;j <countl-i-1; j++, p=p->next)
{
s= strcmp(p->data, p->next->data);
if(s>0)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
}
}
free(p);
free(temp);
free(p->data);
p=NULL;
temp=NULL;
}
int main(int argc, char *argv[]) {
FILE *fp1 = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "a");
int count=0;
countl=0;
word=0;
int sum=0;
char w;
while ((w=getc(fp1))!=EOF)
{
count++;
if (word<count)
word=count;
if (w=='\n') {
sum+=count;
count=0;
countl++;
continue;
}
}
struct text *txt;
txt=(struct text *)malloc(sizeof(struct text));
txt->next=NULL;
char *c;
while (fgets(c, word+1, fp1)) {
InSert(c, txt);
BubbleSort(txt,countl+1);
}
while(txt!= NULL)
{
fwrite(txt, sizeof(struct text), 1, fp2);
txt = txt->next;
}
fprintf(stderr,argv[1],argv[2],word,sum,sum);
fclose(fp1);
fclose(fp2);
free(txt);
txt=NULL;
return 0;
}
要求是从文本逐行读存入指针中 再把指针组成单链 我的程序总有段错误(本人很菜) 请各位高手帮忙看看谢谢了
#include <stdlib.h>
#include <string.h>
struct text
{
char *data;
struct text *next;
};
int countl;
int word;
void InSert(char *line, struct text *txt)
{
struct text *TempCell;
TempCell=(struct text *)malloc(sizeof(struct text));
TempCell->data=(char *)malloc(sizeof(char) * (word+1));
TempCell->data=line;
TempCell->next =txt->next;
txt->next=TempCell;
free(TempCell);
free(TempCell->data);
TempCell=NULL;
}
void BubbleSort(struct text *txt,int n)
{
int i,j,s;
char *temp;
struct text *p;
p=(struct text *)malloc(sizeof(struct text));
p->data=(char *)malloc(sizeof(char) * (word+1));
temp=(char *)malloc(sizeof(char)*(word+1));
if (p!=NULL) {
for(i=0;i <countl;i++)
for(j=0, p=txt->next;j <countl-i-1; j++, p=p->next)
{
s= strcmp(p->data, p->next->data);
if(s>0)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
}
}
free(p);
free(temp);
free(p->data);
p=NULL;
temp=NULL;
}
int main(int argc, char *argv[]) {
FILE *fp1 = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "a");
int count=0;
countl=0;
word=0;
int sum=0;
char w;
while ((w=getc(fp1))!=EOF)
{
count++;
if (word<count)
word=count;
if (w=='\n') {
sum+=count;
count=0;
countl++;
continue;
}
}
struct text *txt;
txt=(struct text *)malloc(sizeof(struct text));
txt->next=NULL;
char *c;
while (fgets(c, word+1, fp1)) {
InSert(c, txt);
BubbleSort(txt,countl+1);
}
while(txt!= NULL)
{
fwrite(txt, sizeof(struct text), 1, fp2);
txt = txt->next;
}
fprintf(stderr,argv[1],argv[2],word,sum,sum);
fclose(fp1);
fclose(fp2);
free(txt);
txt=NULL;
return 0;
}
要求是从文本逐行读存入指针中 再把指针组成单链 我的程序总有段错误(本人很菜) 请各位高手帮忙看看谢谢了