C语言双链表简单操作.docx

上传人:b****2 文档编号:2234857 上传时间:2022-10-28 格式:DOCX 页数:11 大小:16.44KB
下载 相关 举报
C语言双链表简单操作.docx_第1页
第1页 / 共11页
C语言双链表简单操作.docx_第2页
第2页 / 共11页
C语言双链表简单操作.docx_第3页
第3页 / 共11页
C语言双链表简单操作.docx_第4页
第4页 / 共11页
C语言双链表简单操作.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

C语言双链表简单操作.docx

《C语言双链表简单操作.docx》由会员分享,可在线阅读,更多相关《C语言双链表简单操作.docx(11页珍藏版)》请在冰豆网上搜索。

C语言双链表简单操作.docx

C语言双链表简单操作

#include

#include

#include

typedefenum{false=0,true=1}bool;

voidinit_stu();

voidinsert_stu();

voiddelete_stu();

boolselect_stu();

voidmodifly_stu();

voidarchive_stu();

voidreadInfo_stu();

char*getNameById(int);

structStudents

{

intnum;

charname[20];

structStudents*pre;

structStudents*next;

};

structStudents*ptr,*tail,*current,*head;

intsel_id;//存放学生的学号

//初始化链表,头结点num存放现有的学生个数,name没有具体含义

voidinit_stu()

{

ptr=(structStudents*)malloc(sizeof(structStudents));

ptr->num=0;//学生数,初始值0

strcpy(ptr->name,"HeadPointer!

");

ptr->next=ptr;

ptr->pre=ptr;

head=tail=ptr;

}

//按学号顺序从小到大排序加入学生

//学号为主码,不允许相同

voidinsert_stu()

{

charflag;

do

{

ptr=(structStudents*)malloc(sizeof(structStudents));

printf("\n请输入学生的学号:

\n");

scanf("%d",&ptr->num);

//判断学号是否已经存在,如存在则不允许继续输入

if(isExistId(ptr->num))

{

printf("学号已存在,请重新输入!

\n");

continue;

}

printf("请输入学生的姓名:

\n");

scanf("%s",ptr->name);

tail=head;

current=head->next;

//查找合适位置

while(current!

=head&¤t->numnum)

{

tail=current;

current=current->next;

}

//插入合适位置

ptr->next=current;

ptr->pre=tail;

tail->next=ptr;

current->pre=ptr;

//学生数加1

head->num++;

printf("添加成功,是否继续添加(y/n)\n");

flag=getch();

}while('Y'==flag||'y'==flag);

}

//按学号删除学生记录

voiddelete_stu()

{

intdel_id;

char*del_name;

charflag;

printf("\n请输入待删除学生的学号:

\n");

scanf("%d",&del_id);

//查找是否有该学号

if(!

isExistId(del_id))

{

printf("该学号不存在!

\n");

return;

}

//如果有该学号,返回该学生的信息判断是否应该删除

del_name=getNameById(del_id);

printf("待删除的学生信息为:

\n");

printf("学号\t\t姓名\n");

printf("%d\t\t%s\n",del_id,del_name);

printf("确认删除(y/n)?

\n");

flag=getch();

if(!

('y'==flag||'Y'==flag))

{

return;

}

//开始删除

tail=head;

current=head->next;

while(current->num!

=del_id)

{

tail=current;

current=current->next;

}

tail->next=current->next;

current->next->pre=tail;

free(current);

//学生数减1

head->num--;

printf("\n删除成功!

\n");

}

//按学号查询学生信息,returnvalue:

true(学号存在)false(学号不存在)

boolselect_stu()

{

char*sel_name=NULL;

printf("\n请输入学生的学号:

\n");

scanf("%d",&sel_id);

if(!

isExistId(sel_id))

{

printf("\n学号不存在!

\n");

returnfalse;

}

sel_name=getNameById(sel_id);

printf("学生的信息为:

\n");

printf("\n学号\t\t姓名\n");

printf("%d\t\t%s\n",sel_id,sel_name);

returntrue;

}

//通过学号修改学生的信息(只做了姓名修改)

voidmodifly_stu()

{

charnew_name[20];

charflag;

if(!

select_stu())

{

return;

}

printf("请输入新的名字:

\n");

scanf("%s",new_name);

printf("确认修改?

(y/n)\n");

flag=getch();

if('y'==flag||'Y'==flag)

{

//开始修改

current=head->next;

while(current->num!

=sel_id)

{

current=current->next;

}

strcpy(current->name,new_name);

printf("修改成功!

\n");

}

}

//保存学生信息到本地

voidarchive_stu()

{

FILE*output;

if(0==head->num)

{

printf("\n无学生数据!

\n\n");

return;

}

if((output=fopen("StudentsInfo.txt","w"))==NULL)

{

printf("无法打开文件!

\n");

return;

}

//写入磁盘

current=head;

do

{

fprintf(output,"%d%s\n",current->num,current->name);

current=current->next;

}while(current!

=head);

fclose(output);

printf("\n保存成功!

\n");

}

//从本地读取学生信息

voidreadInfo_stu()

{

intstu_id;

charstu_name[20];

boolisHead=true;

FILE*input;

if(0!

=head->num)

{

printf("\n已有记录输入,不允许再录入!

\n");

return;

}

if((input=fopen("StudentsInfo.txt","r"))==NULL)

{

printf("无法打开文件!

\n");

return;

}

tail=head;

current=head->next;

//录入学生信息

while(fscanf(input,"%d%s",&stu_id,stu_name)==2)

{

//录入头结点信息

if(isHead)

{

tail->num=stu_id;

strcpy(tail->name,stu_name);

isHead=false;

continue;

}

ptr=(structStudents*)malloc(sizeof(structStudents));

ptr->num=stu_id;

strcpy(ptr->name,stu_name);

tail->next=ptr;

ptr->pre=tail;

ptr->next=current;

current->pre=ptr;

tail=current;

current=current->next;

}

printf("\n录入完毕!

\n");

fclose(input);

}

//显示记录

voiddisplay()

{

if(0==head->num)

{

printf("\n无学生数据!

\n\n");

return;

}

current=head->next;

printf("\n学号\t\t姓名\n");

while(current!

=head)

{

printf("%d\t\t%s\n",current->num,current->name);

current=current->next;

}

printf("共有%d条记录!

",head->num);

}

//查找是否存在指定的学号true代表存在,false表示不存在该学号

boolisExistId(intid)

{

current=head->next;

while(current!

=head)

{

if(current->num==id)

{

returntrue;

}

current=current->next;

}

returnfalse;

}

//通过学号,返回学生的姓名

char*getNameById(intdel_id)

{

current=head->next;

while(current->num!

=del_id)

{

current=current->next;

}

returncurrent->name;

}

intmain(void)

{

charnumber;

init_stu();

while(true)

{

printf("\n-------

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1