//开始上面一节复制漏了,这是写了重新补上的,运行过没有错
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct ZH)
struct ZH *Create() ;
void Shu(struct ZH *head);
void Fang(struct ZH **head);
void Pai(struct ZH *head);
struct ZH
{
int z;
struct ZH *next;
};
int main()
{
struct ZH *head,*p;
head=Create();
Pai(head);
Shu(head);
FILE *fp;
fp=fopen("a.bf","wb"); //要用rewind函数的话就把wb改成wb+
if(fp==NULL)
{
printf("打开失败!!\n!!");
exit(0);
}
p=head;
while(p!=NULL)
{
fwrite(&p->z ,sizeof(int),1,fp);
p=p->next ;
}
//rewind(fp); 可以把我下面用双斜杠框起来的这一段替换成这一行函数,表示把文件位置指针重新指向了文件的开头位置,记得把上面打开文件那里的wb改成wb+
fclose(fp);
fp=fopen("a.bf","rb");
if(fp==NULL)
{
printf("打开失败!!\n!!");
exit(0);
}
//
int c;
fread(&c,sizeof(int),1,fp);
while(!feof(fp))
{
printf("%3d",c);
fread(&c,sizeof(int),1,fp);
}
putchar('\n');
fclose(fp);
Fang(&head);
return 0;
}
//链表创建
struct ZH *Create()
{
struct ZH *head=NULL,*p,*p1;
p=(struct ZH*)malloc(LEN);
scanf("%d",&p->z );
p->next =NULL;
while(p->z !=-1)
{
if(head==NULL)
{
head=p;
p1=p;
}
else
{
p1->next =p;
p1=p;
}
p=(struct ZH*)malloc(LEN);
scanf("%d",&p->z );
p->next =NULL;
}
p1->next =NULL;
return head;
}
//输出链表
void Shu(struct ZH *head)
{
struct ZH *p;
p=head;
while(p!=NULL)
{
printf("%3d",p->z );
p=p->next ;
}
putchar('\n');
}
//释放链表
void Fang(struct ZH **head)
{
struct ZH *p;
while(*head!=NULL)
{
p=*head;
*head=(*head)->next ;
free(p);
}
*head=NULL;
}
//排序,这里我只是交换了结点的值,实在有点菜,本来想交换结点,有了想法,但是写出来一直没实现
void Pai(struct ZH *head)
{
struct ZH *p;
p=head;
int sum=0,i=1,j=0;
while(p!=NULL)
{
sum++;
p=p->next ;
}
p=head;
int temp;
while(i<sum)
{
p=head;
j=0;
while(j<sum-i)
{
if(p->z>p->next ->z )
{
temp=p->z ;
p->z =p->next ->z ;
p->next ->z =temp;
}
p=p->next ;
j++;
}
i++;
}
}
01-09