(筆記?。?/p>
我們提供的服務(wù)有:網(wǎng)站設(shè)計制作、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、蘭西ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的蘭西網(wǎng)站制作公司功能:#include#include#includestruct STU
{char name[20];
int num;
int age;
char addr[20];
};
struct Jd
{struct STU student;
struct Jd *next;
};
void createFile(char *FName); //創(chuàng)建文件,存放結(jié)構(gòu)體數(shù)據(jù)
void readFile(char *FName); //從文件讀出數(shù)據(jù)到結(jié)構(gòu)體
struct Jd *createchain(char *FName); //利用文件創(chuàng)建鏈表
void printchain(struct Jd *head); //輸出鏈表所有結(jié)點數(shù)據(jù)
void sortchain(struct Jd *head); //根據(jù)地址對鏈表進行排序
void writeToFile(struct Jd *head, char *FName); //把鏈表結(jié)點數(shù)據(jù)寫往文件
int main(void)
{struct Jd *head = NULL;
char FileName[128] = {'\0'};
strcpy(FileName,"stu.dat");
createFile(FileName);
readFile(FileName);
head = createchain(FileName);
printf("* * * * * 未排序的鏈表數(shù)據(jù) * * * * * \n");
printchain(head);
sortchain(head);
printf("* * * * * 根據(jù)地址排序后的鏈表數(shù)據(jù) * * * * * \n");
printchain(head);
strcpy(FileName, "paixu.dat");
writeToFile(head,FileName);
readFile(FileName);
return 0;
}
void createFile(char *FName)
{int i = 1;
struct STU stud;
FILE *fp;
if ( (fp = fopen(FName, "wb")) == NULL ) //利用FName的字符串作文件名
{printf("cannot open file\n");
exit(0);
}
while ( i!=0 )
{printf("enter data of student:\n");
printf("姓名:");
scanf("%s", stud.name);
printf("學(xué)號:");
scanf("%d", &stud.num);
printf("年齡:");
scanf("%d", &stud.age);
printf("地址:");
scanf("%s", stud.addr);
if ( fwrite(&stud,sizeof(struct STU),1,fp) != 1 ) //把一個結(jié)構(gòu)體數(shù)據(jù)寫到文件中,并判斷是否真實寫入
{ printf("file write error\n");
}
printf("還需要輸入新的學(xué)生數(shù)據(jù)(1=繼續(xù),0=退出)");
scanf("%d", &i);
}
fclose(fp);
}
void readFile(char *FName)
{struct STU stud;
FILE *fp;
if ( (fp = fopen(FName, "rb")) == NULL )
exit(0);
printf("* * * * * %s 文件中的記錄數(shù)據(jù) * * * * * \n", FName);
//從文件里讀出一個結(jié)構(gòu)體數(shù)據(jù),知道讀不出一個完整的結(jié)構(gòu)體數(shù)據(jù)
while ( fread(&stud,sizeof(struct STU),1,fp) == 1 )
{printf("%-20s %4d %4d %-20s\n", stud.name, stud.num, stud.age, stud.addr);
}
fclose(fp);
}
struct Jd *createchain(char *FName)
{FILE *fp;
struct Jd *pt, *pEnd, *head = NULL;
struct STU stud;
if ( (fp=fopen(FName,"rb")) == NULL )
{exit(0);
}
while ( fread(&stud, sizeof(struct STU), 1, fp) == 1 ) //從文件里讀出一個結(jié)構(gòu)體數(shù)據(jù)
{pt = (struct Jd *)malloc(sizeof(struct Jd)); //創(chuàng)建鏈表結(jié)點空間
pt->student = stud; //把前面讀出的結(jié)構(gòu)體數(shù)據(jù)賦值給結(jié)點的student成員
if ( head == NULL )
{ head = pEnd = pt;
pt->next = NULL;
}
else //在尾部插入結(jié)點
{ pEnd->next = pt; //將新結(jié)點鏈入鏈表
pt->next = NULL; //將新結(jié)點的next指針置空,標識成尾結(jié)點
pEnd = pt; //讓pEnd指向尾結(jié)點,為下一次插入做準備
}
}
fclose(fp);
return head;
}
void printchain(struct Jd *head)
{struct STU stud;
while ( head != NULL ) //從頭結(jié)點開始輸出,直到鏈表最后的尾結(jié)點
{stud = head->student;
printf("%-20s %4d %4d %-20s\n", stud.name, stud.num, stud.age, stud.addr);
head = head->next;
}
}
void sortchain(struct Jd *head) //根據(jù)addr的值從小到大的排序
{struct Jd *pt1, *pt2, *pt;
struct STU temp;
for ( pt1 = head; pt1->next != NULL; pt1 = pt1->next )
{pt = pt1;
for ( pt2 = pt1->next; pt2 != NULL; pt2 = pt2->next )
{ if ( strcmp(pt->student.addr,pt2->student.addr) >0 )
pt = pt2;
}
if ( pt != pt1 )
{ temp = pt1->student;
pt1->student = pt->student;
pt->student = temp;
}
}
}
void writeToFile(struct Jd *head, char *FName)
{FILE *fp;
struct Jd *pt;
if ( (fp = fopen(FName,"wb")) == NULL)
{exit(0);
}
pt = head;
//依次把鏈表結(jié)點的學(xué)生信息寫往文件中,直到輸出鏈表最后一個結(jié)點
while ( pt != NULL )
{fwrite(&(pt->student), sizeof(struct STU), 1, fp); //只將鏈表中學(xué)生信息寫入
pt = pt->next;
}
fclose(fp);
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧