单链表的创建插入删除以及读取记录.docx
《单链表的创建插入删除以及读取记录.docx》由会员分享,可在线阅读,更多相关《单链表的创建插入删除以及读取记录.docx(16页珍藏版)》请在冰点文库上搜索。
单链表的创建插入删除以及读取记录
#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;idif(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;idread(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;idwrite(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;idif(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;idif(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;
}