链表操作表格
来源:互联网 发布:java websocketcli 编辑:程序博客网 时间:2024/05/21 09:33
//我们把grade.c中的内容名字字符串,按大小排列,后写入另一个文件//中
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
//根据表格,我们写义一个结构体来存储,这个文件中每个人的信息
Struct person{
int number;
char name[20];
int age;
int grade;
struct person *next;
};
void show(struct person *head);//存入链表后,显示所每个成员的内容
struct person *store_list(struct person *head,char *buf);//将文件中的内容读出来,存储在链表中
struct person *list_sort(struct person *head);//在链表的按名字排序
void write_file(struct person *head);//将排列好的链表,存在另一个文件中
int main()
{
FILE *fp;
struct person *head=NULL;
char buf[100];
char *line;
size_t len;
ssize_t read;
int num=8;
fp = fopen("grade.c","r");
while(num)
{
read = getline(&line,&len,fp);
printf("line:--%s\n",line);
head = store_list(head,line);
num--;
}
show(head);
printf("++++++++++++++++++++++++++\n");
head = list_sort(head);
show(head);
printf("++++++++++++++++++++++++++\n");
write_file(head);
return 0;
}
void write_file(struct person *head)
{
FILE *fp;
fp = fopen("1th","w");
while(head)
{
fprintf(fp,"%d\t%s\t%d\t%d\n",head->number,head->name,head->age,head->grade);
head = head->next;
}
}
void show(struct person *head)
{
while(head)
{
printf("%d\t%s\t%d\t%d\n",head->number,head->name,head->age,head->grade);
head = head->next;
}
}
struct person *store_list(struct person *head,char *buf)
{
char *aa;
char *bb;
struct person *tmp=NULL;
tmp = (struct person *)malloc(sizeof(struct person ));
aa = strstr(buf,"\t");
bb = strtok(buf,"\t");
printf("bb:%s\n",bb);
tmp->number = atoi(bb);
buf = aa +1;
aa = strstr(buf,"\t");
bb = strtok(buf,"\t");
strcpy(tmp->name,bb);
printf("name:%s\n",tmp->name);
buf = aa + 1;
aa = strstr(buf,"\t");
bb = strtok(buf,"\t");
tmp->age = atoi(bb);
printf("%d\n",tmp->age);
tmp->grade = atoi(aa+1);
printf("%d\n",tmp->grade);
if(head == NULL){
tmp->next =NULL;
head = tmp;
}
else{
tmp->next = head;
head = tmp;
}
return head;
}
struct person *list_sort(struct person *head)
{
int ret;
struct person *tmp;
struct person *min;
struct person *pre_min;
struct person *newhead = NULL;
while(head)
{
min = head;
tmp = head;
while(tmp->next != NULL)
{
if((ret = strcmp(tmp->next->name,min->name))<0)
{
pre_min = tmp;
min = pre_min->next;
}
tmp = tmp->next;
}
if(min == head)
{
head= head->next;
}
else{
pre_min->next = min->next;
}
if(newhead == NULL)
{
min->next = NULL;
newhead = min;
}
else{
min->next = newhead;
newhead = min;
}
}
return newhead;
}
Grade.c文件:
2013003 bob 21 90
2013005 lucy 20 78
2013006 alis 21 59
2013011 jams 19 61
2013025 jim 23 80
2013039 kobe 19 81
2013041 tim 20 53
2013042 lily 20 71
- 链表操作表格
- 表格操作
- 表格操作
- 操作表格
- 操作表格
- 表格操作
- 表格操作二例
- c#操作word表格
- javascript 操作表格
- javascript操作表格排序
- js 表格操作
- javascript操作表格
- c#操作word表格
- c#操作word表格
- c#操作word表格
- java操作EXCEL表格
- c#操作word表格
- c#操作word表格
- 设备模型-中断
- 最大子段和 各种算法讨论
- 编程之美3.9 重建二叉树
- 过程化到基于集合的思想
- hdu4712 Hamming Distance
- 链表操作表格
- Java学习笔记7 —— 语句和流控制
- 局域网即时通信软件
- ExtJs Grid的增加、删除、修改、查询操作
- OpenStack impl_kombu
- 【Tesseract-OCR】在VS2012环境下调用API方法---注意避免名字冲突
- Android自动开关机实现
- C#数据类型
- “一步千里”之数组找数