/*
字符串换成整形
void Tonum(char* str)
{
int i=0;
for(i=0;(str+i)!=’\0’;i++)
{
if((str+i)>=‘0’&&(str+i)<=9)
res=res10+(*(str+i)-‘0’)
}
}
/
/
静态链表:需要数组来实现 物理上是连续的 也有可能不连续 每个元素由data和cur两部分组成,其中cur相当于链表的next指针,这种用数组描述的链表叫做静态链表
动态链表:执行过程中从无到有 一一建立 需要free
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#define BUF while(getchar()!=’\n’)
typedef struct Data
{
int id;
char name[20];
}Data;
typedef struct Student
{
Data data;
struct Student* next;
struct Student* prv;
}stu_t;
/*
函数名: creatLink
功能 : 创建列表的头节点
返回值: stu_t*
/
stu_t creatstu()
{
stu_t* head=(stu_t*)malloc(sizeof(stu_t));
assert(head);
memset(head,0,sizeof(stu_t));
head->next=head;
head->prv=head;
return head;
}
/*
函数名: insertLink
功能: 在列表中添加新的节点
参数: 头节点
返回值: void
/
void intsertstu(stu_t head)
{
while(1)
{
stu_t* newnode=(stu_t*)malloc(sizeof(stu_t));
memset(newnode,0,sizeof(stu_t));
Data user1;
printf(“请输入学生的ID:”);
scanf("%d",&user1.id);
BUF;
printf(“请输入学生姓名:”);
scanf("%s",user1.name);
BUF;
stu_t* p=head;
while(p->next!=head)
{
p=p->next;
}
newnode->data=user1;
p->next=newnode; //尾插入
newnode->next=head;
newnode->prv=p;
head->prv=newnode;
char ch;
printf(“是否继续输入:y/n”);
scanf("%c",&ch);
if(ch==‘n’) break;
}
/* 头插法
head->next->prv=newnode;
newnode->prv=head;
newnode->next=head->next;
head->next=newnode;
*/
}
/*
函数名:printAllstu(stu_t* head)
函数功能:打印所用学生
返回: void
/
void printAllstu(stu_t head)
{
stu_t* p=head->next;
while(p!=head)
{
printf(“name:%s id:%d\n”,p->data.name,p->data.id);
p=p->next;
}
}
/*
函数名: findstu
函数功能: 查找学生
返回值:返回节点
*/
stu_t* findstu(stu_t* head)
{
stu_t* p=head->next;
int id=0;
printf(“请输入要查找的学号:”);
scanf("%d",&id);
BUF;
while(p!=head)
{
if(p->data.id==id)
{
//printf(“学生信息:%s”,p->data.name);
return p;
}
p=p->next;
}
return head;
}
/*
函数名: UpdataStu
函数功能: 修改学生信息
函数返回值: void
*/
void updatastu(stu_t* head)
{
stu_t* p=findstu(head);
if(p==head)
{
printf(“查无此生!”);
return ;
}
printf(“请输入学生信息:”);
scanf("%s",p->data.name);
while(getchar()!=’\n’);
}
/*
函数名: sortstu
功能:根据学生ID进行排序
函数:参数头节点
返回:void
*/
void sortstu(stu_t* head)
{
stu_t* p;
Data temp={0};
int flag=0;
while(1)
{ p=head->next;
flag=0;
while(p->next!=head)
{
if(p->data.id>p->next->data.id)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
flag=1;
}
p=p->next;
}
if(flag==0) { break;}
}
}
/*
函数名称: removestu
函数功能:删除学生信息
函数参数: 头节点
*/
void removestu(stu_t* head)
{
stu_t* p=head->next;
int id=0;
printf("请输入要删除的学生id:");
scanf("%d",&id);
BUF
if(p==head) printf("无数据");
if(p->next==head)
{
if(p->data.id==id)
{
free(p);
printf("删除成功!");
}
else
{
printf("无此数据!");
}
}
while(p!=head)
{
printf("Mark\n");
if(p->data.id==id)
{
p->prv->next=p->next;
p->next->prv=p->prv;
free(p);
break;
}
p=p->next;
}
}
/*
void removestu(stu_t* head)
{
stu_t* p=findstu(head);
if(p==NULL) return ;
if(p->next!=NULL)
{
p->next->prv=p->prv;
p->prv->next=p->next;
}
free§;
}
/
/
函数: freestu
功能: 回收链表的堆空间
返回值: void
/
void freestu(stu_t head)
{
stu_t* p=head->next;
stu_t* temp=NULL;
while(p!=head)
{
temp=p;
p=p->next;
free(temp);
}
free(head);
}
/*
void freestu(stu_t* head)
{
stu_t* p=head;
while(p->next!=head)
{
p=p->next;
free(p->prv);
//printf(“释放成功!!\n”);
}
free§;
}
/
/
函数名: savestu
功能: 将链表的数据保存到文件中
/
void savestu(stu_t head)
{
FILE* fp=fopen("./DB.bin",“w”);
stu_t* p=head->next;
while(p!=head)
{
fwrite(&p->data,sizeof(Data),1,fp);
//fprintf(fp,"id:%d--name:%s\n",p->data.id,p->data.name);
p=p->next;
}
fclose(fp);
}
/*
函数名: loadstu
功能: 将文件中的所有数据加载到链表
返回:void
*/
void loadstu(stu_t* head)
{
FILE* fp=fopen("./DB.bin","r");
if(fp==NULL) {return;}
//Data temp;
while(1)
{
stu_t* newstu=(stu_t*)malloc(sizeof(stu_t));
memset(newstu,0,sizeof(stu_t));
if(fread(&newstu->data,sizeof(Data),1,fp)==0){break;}
//res=fscanf(fp,"id:%d--name:%s\n",&newstu->data.id,newstu->data.name);
//if(res!=2){break;}
//newstu->data.id=id;
//strncpy(newstu->data.name,name,sizeof(name));
stu_t* p=head;
while(p->next!=head) //尾插法
{
p=p->next;
}
newstu->next=p->next;
p->next=newstu;
p->next->prv=newstu;
//head->prv=newstu;
newstu->prv=p;
/*
p->next=newNode;
newNode->next=head;
newNode->prev=p;
head->prev=newNode;
*/
//printf("******************");
}
fclose(fp);
}
int main(int argc, char** argv)
{
stu_t* head=creatstu();
loadstu(head);
int ch=0;
while(1)
{
printf(“1 添加学生\n”);
printf(“2 打印学生\n”);
printf(“3 删除学生\n”);
printf(“4 排序\n”);
printf(“0 退出\n”);
printf(“请选择:”);
scanf("%d",&ch);
while(getchar()!=’\n’);
switch(ch)
{
case 1: intsertstu(head);break;
case 2: printAllstu(head);break;
case 3: removestu(head);break;
case 4: sortstu(head); break;
case 0: savestu(head);freestu(head);return 0;
default: break;
}
}
}