单链表的创建插入删除以及读取记录.docx

上传人:b****8 文档编号:9778333 上传时间:2023-02-06 格式:DOCX 页数:16 大小:15.88KB
下载 相关 举报
单链表的创建插入删除以及读取记录.docx_第1页
第1页 / 共16页
单链表的创建插入删除以及读取记录.docx_第2页
第2页 / 共16页
单链表的创建插入删除以及读取记录.docx_第3页
第3页 / 共16页
单链表的创建插入删除以及读取记录.docx_第4页
第4页 / 共16页
单链表的创建插入删除以及读取记录.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

单链表的创建插入删除以及读取记录.docx

《单链表的创建插入删除以及读取记录.docx》由会员分享,可在线阅读,更多相关《单链表的创建插入删除以及读取记录.docx(16页珍藏版)》请在冰豆网上搜索。

单链表的创建插入删除以及读取记录.docx

单链表的创建插入删除以及读取记录

#include

#include

#include

#include

intn,id;

//intfd;

FILE*fp;

structstudent

{

longnum;

intage;

floatscore;

structstudent*next;

};

structstudent*head;

//structstudent*f,*sp;

structstudent*create()

{

structstudent*p,*q;

n=0,head=NULL;

p=(structstudent*)malloc(sizeof(structstudent));

q=(structstudent*)malloc(sizeof(structstudent));

printf("pleaseinputtherecords:

\n");

scanf("%ld%d%f",&q->num,&q->age,&q->score);

if(q->num!

=0){

if((fp=fopen("/home/linux/shiyan/listfile.c","ab+"))==NULL){

printf("cannotopenthefile.\n");

return;

}

if(fwrite(q,sizeof(structstudent),1,fp)!

=1)

{

printf("cannotwritefile!

\n");

}

printf("ok!

\n");

fclose(fp);

}

else{

printf("overwriterecordsintofile.\n");

}

while(q->num!

=0){

n+=1;

if(n==1){

head=q;

}

else

{

p->next=q;

}

p=q;

q=(structstudent*)malloc(sizeof(structstudent));

printf("pleaseinputmorerecords:

\n");

scanf("%ld%d%f",&q->num,&q->age,&q->score);

if(q->num!

=0){

if((fp=fopen("/home/linux/shiyan/listfile.c","ab+"))==NULL){

printf("cannotopenthefile.\n");

return;

}

if(fwrite(q,sizeof(structstudent),1,fp)!

=1)

{

printf("cannotwritefile!

\n");

}

printf("ok!

\n");

fclose(fp);

}

else{

printf("overwriterecordsintofile.\n");

}

}

p->next=NULL;

returnhead;

}

voidprint()

{

structstudent*l;

printf("thenumbersoflistare:

%d\n",n);

l=head;

if(head!

=NULL){

do

{

printf("num:

%ldage:

%dscore:

%.1f\n",l->num,l->age,l->score);

l=l->next;

}while(l!

=NULL);

}

else

{

printf("thelistisnull.\n");

}

}

structstudent*insert()

{

structstudent*e,*t,*x,*into;

t=head;

printf("pleaseinputtheinsertrecord:

\n");

into=(structstudent*)malloc(sizeof(structstudent));

scanf("%ld%d%f",&into->num,&into->age,&into->score);

if(into->num!

=0){

if((fp=fopen("/home/linux/shiyan/listfile.c","ab+"))==NULL){

printf("cannotopenthefile.\n");

return;

}

if(fwrite(into,sizeof(structstudent),1,fp)!

=1)

{

printf("cannotwritefile!

\n");

}

printf("ok!

\n");

fclose(fp);

}

else{

printf("overwriterecordsintofile.\n");

}

while(into->num!

=0)

{

e=into;

if(head==NULL){

head=e;

e->next=NULL;

}

else

{

while((e->num>t->num)&&t->next!

=NULL)

{

x=t;

t=t->next;

}

if(e->num<=t->num){

if(t==head){

head=e;

e->next=t;

}

else

{

x->next=e;

e->next=t;

}

}

else

{

t->next=e;

e->next=NULL;

}

}

n+=1;

printf("pleaseinputtheinsertrecord:

\n");

into=(structstudent*)malloc(sizeof(structstudent));

scanf("%ld%d%f",&into->num,&into->age,&into->score);

if(into->num!

=0){

if((fp=fopen("/home/linux/shiyan/listfile.c","ab+"))==NULL){

printf("cannotopenthefile.\n");

return;

}

if(fwrite(into,sizeof(structstudent),1,fp)!

=1)

{

printf("cannotwritefile!

\n");

}

printf("ok!

\n");

fclose(fp);

}

else{

printf("overwriterecordsintofile.\n");

}

}

returnhead;

}

structstudent*delete()

{

structstudent*s,*c;

longnum;

printf("pleaseinputthedeleterecord:

\n");

scanf("%ld",&num);

while(num!

=0)

{

if(head==NULL)

{

printf("thelistisnull.\n");

returnhead;

}

s=head;

while((num!

=s->num)&&(s->next!

=NULL))

{

c=s;

s=s->next;

}

if(num==s->num)

{

if(s==head){

head=s->next;

free(s);

}

else

{

c->next=s->next;

s=s->next;

free(s);

}

printf("delete%ldissuccess.\n",num);

n-=1;

}

else

{

printf("cannotfound%ld\n",num);

}

printf("pleaseinputthedeleterecord:

\n");

scanf("%ld",&num);

}

returnhead;

}

structstudent*refer()

{

structstudent*r,*m;

longrnum;

printf("pleaseinputtorefernum:

\n");

scanf("%ld",&rnum);

r=head;

if(rnum!

=0){

while(m->next!

=NULL&&r->num!

=rnum){

m=r;

r=m->next;

}

if(m->next==NULL){

printf("therecordisnotexist.\n");

returnhead;

}

if(rnum==r->num)

{

printf("therecordfind:

\n");

printf("num:

%ldage:

%dscore:

%.1f\n",r->num,r->age,r->score);

}

}

else{

printf("theinputeriserror.\n");

}

returnhead;

}

/*structstudent*save()

{

FILE*fp;

structstudent*l1;

if((fp=fopen("/home/linux/shiyan/listx","ab+"))==NULL){

printf("cannotopenthefile.\n");

return;

}

l1=head;

for(id=0;id

if(fwrite(l1,sizeof(structstudent),1,fp)!

=1)

{

printf("cannotwritefile!

\n");

}

l1+=1;

printf("ok!

\n");

}

fclose(fp);

returnhead;

}*/

structstudent*load()

{

FILE*fp;

structstudent*f1;

f1=(structstudent*)malloc(sizeof(structstudent));

if((fp=fopen("/home/linux/shiyan/listfile.c","rb"))==NULL)

{

printf("cannotopenthefile!

\n");

return;

}

while(!

feof(fp)){

if(fread(f1,sizeof(structstudent),1,fp)!

=NULL){

printf("num:

%ldage:

%dscore:

%.1f\n",f1->num,f1->age,f1->score);

//sp=(structstudent*)malloc(sizeof(structstudent));

//f1=f1->next;

n+=1;

}

}

fclose(fp);

returnf1;

}

/*voidload()

{

fd=open("/home/linux/shiyan/list.txt",O_RDONLY);

for(id=0;id

read(fd,buffer,sizeof(buffer));

printf("num:

%ldage:

%dscore:

%.1f\n",f->num,f->age,f->score);

f=f->next;

buffer=f;

}

close(fd);

}*/

/*voidsave()

{

fd=open("/home/linux/shiyan/list.txt",O_WRONLY|O_CREAT);

for(id=0;id

write(fd,f,sizeof(structstudent));

f=f->next;

}

close(fd);

}*/

intchoose()

{

intchoice;

scanf("%d",&choice);

returnchoice;

}

voidselecte(intnumber)

{

//head=create();

//structstudent*head;

switch(number)

{

case1:

insert();

break;

case2:

delete();

break;

case3:

print();

break;

case4:

load();

break;

case5:

refer();

break;

//case6:

//save();

//break;

case6:

exit(0);

default:

printf("thechoiceisnotlawful,pleaseinputagain\n");

break;

}

}

voidmenu()

{

printf("==========THEMAINMENU==========\n");

printf("1:

insertarecordinlist.\n");

printf("2:

deletearecordfromlist.\n");

printf("3:

printfpresentrecordsonscreen.\n");

printf("4:

readtherecordsfromlistfile.c.\n");

printf("5:

selecttherecordfromlist.\n");

printf("6:

getawaythemainmenu.\n");

printf("=================================\n");

printf("pleaseinputachoice:

1--2--3--4--5--6\n");

selecte(choose());

}

/*voidsave()

{

FILE*fp;

if((fp=fopen("list.txt","w"))==NULL){

printf("cannotopenfile!

\n");

return;

}

for(id=0;id

if(fwrite(f,sizeof(structstudent),1,fp)!

=1){

printf("cannotwritefile!

\n");

}

f=f->next;

}

fclose(fp);

}*/

/*voidload()

{

FILE*fp;

if((fp=fopen("list.txt","r"==NULL))){

printf("cannotopenfile!

\n");

return;

}

for(id=0;id

if(fread(head,sizeof(structstudent),1,fp)!

=1){

if(feof(fp)){

fclose(fp);

return;

}

printf("cannotreadfile!

\n");

}

printf("num:

%ldage:

%dscore:

%.1f\n",head->num,head->age,head->score);

head=head->next;

}

fclose(fp);

}*/

intmain()

{

//printf("pleaseinputtherecords:

\n");

//h=create();

//print(h);

/*printf("pleaseinputtheinsertrecord:

\n");

into=(structstudent*)malloc(sizeof(structstudent));

scanf("%ld%d%f",&into->num,&into->age,&into->score);

while(into->num!

=0)

{

h=insert(h,into);

print(h);

printf("pleaseinputtheinsertrecord:

\n");

into=(structstudent*)malloc(sizeof(structstudent));

scanf("%ld%d%f",&into->num,&into->age,&into->score);

}

printf("pleaseinputthedeleterecord:

\n");

scanf("%ld",&dnum);

while(dnum!

=0)

{

h=delete(h,dnum);

print(h);

printf("pleaseinputthedeleterecord:

\n");

scanf("%ld",&dnum);

}*/

head=create();

while

(1){

menu();

}

returnEXIT_SUCCESS;

}

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

当前位置:首页 > 求职职场 > 简历

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

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