第1个回答 推荐于2016-07-23
这个是我以前编的,有些小错误,你参考一下:
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#include"string.h"
typedef struct stud
{
long int number;
char name[20];
int grade;
char other[30];
char sex[10];
struct stud *next;
}T;
T *creat()
{
T *p,*q,*head;
long int number;
int grade;
char name[20],other[30],sex[10];
head=(T *)malloc(sizeof(T));
q=head;
printf("Please input the data:(num,name,sex,grade,other,0:end)\n");
scanf("%ld",&number);fflush(stdin);
if(number)
{
gets(name);
gets(sex);
scanf("%d",&grade);fflush(stdin);
gets(other);
}
while(number)
{
p=(T *)malloc(sizeof(T));
q->number=number;
strcpy(q->name,name);
strcpy(q->sex,sex);
q->grade=grade;
strcpy(q->other,other);
q->next=p;
q=p;
scanf("%ld",&number);fflush(stdin);
if(number)
{
gets(name);
gets(sex);
scanf("%d",&grade);fflush(stdin);
gets(other);
}
}
q->next=NULL;
fflush(stdin);
return head;
}
void print(T *head)
{
T *p;
p=head;
if(p)
printf("Number name \tsex grade other\n");
while(p->next)
{
printf("%08ld %-15s%-6s %-8d %s\n",p->number,p->name,p->sex,p->grade,p->other);
p=p->next;
}
printf("\nEND!\n");
}
void fprint(T *head,FILE *fp)
{
T *p;
p=head;
if(p)
fprintf(fp,"Number name \tsex grade other\n");
while(p->next)
{
fprintf(fp,"%08ld %-15s%-6s %-8d %s\n",p->number,p->name,p->sex,p->grade,p->other);
p=p->next;
}
fprintf(fp,"\nEND!\n");
}
T *range(T *head)
{
T *p,*q,*s;
int key;
printf("Please input the key you want to range:\n1.number,2.name,3.sex,4.grade,5.other.\nYour choice:[ ]\b\b");
scanf("%d",&key);fflush(stdin);
s=q=head;
p=q->next;
switch(key)
{
case 1:
while(p)
{
if(s->number>p->number) s=p;
p=p->next;
}
if(s!=head)
{
s->next=head;
head=s;
}
for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;
if(s!=q)
{
s->next=q;
q=s;
}
}
break;
case 2:
while(p)
{
if(strcmp(s->name,p->name)>0) s=p;
p=p->next;
}
if(s!=head)
{
s->next=head;
head=s;
}
for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;
if(s!=q)
{
s->next=q;
q=s;
}
}
break;
case 3:
while(p)
{
if(strcmp(s->sex,p->sex)>0) s=p;
p=p->next;
}
if(s!=head)
{
s->next=head;
head=s;
}
for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;
if(s!=q)
{
s->next=q;
q=s;
}
}
break;
case 4:
while(p)
{
if(s->grade>p->grade) s=p;
p=p->next;
}
if(s!=head)
{
s->next=head;
head=s;
}
for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;
if(s!=q)
{
s->next=q;
q=s;
}
}
break;
case 5:
while(p)
{
if(strcmp(s->other,p->other)>0) s=p;
p=p->next;
}
if(s!=head)
{
s->next=head;
head=s;
}
for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;
if(s!=q)
{
s->next=q;
q=s;
}
}
break;
}
return head;
}
T *range_1(T *head)
{
T *p,*q,*s;
s=q=head;
p=q->next;
while(p)
{
if(s->number>p->number) s=p;
p=p->next;
}
if(s!=head)
{
s->next=head;
head=s;
}
for(s=q=head->next;s;s=q=s->next)
{
for(p=q->next;p;p=p->next)
if(s->grade>p->grade) s=p;
if(s!=q)
{
s->next=q;
q=s;
}
}
return head;
}
T *insert(T *head)
{
T *p,*q,*s;
s=(T *)malloc(sizeof(T));
printf("Please input the insert data:(num,name,sex,grade,other)\n");
scanf("%ld",&s->number);fflush(stdin);
if(s->number==0)
{
printf("ERROR!\nPlease input number again.\n");
scanf("%ld",&s->number);fflush(stdin);
}
gets(s->name);
gets(s->sex);
scanf("%d",&s->grade);fflush(stdin);
gets(s->other);
q=head;
p=q->next;
if(q->number>s->number)
{
s->next=head;
head=s;
}
while(q->number<s->number&&p)
{
q=p;
p=q->next;
}
/*if(!q)
{
q->next=s;
s->next=NULL;
}
else
{
s->next=p;
q->next=s;
}*/
s->next=p;
q->next=s;
return head;
}
T *del(T *head)
{
T *p,*q;
long int x;
int key=2;
print(head);
printf("Please input the data you want to delete:(num)\n");
scanf("%ld",&x);fflush(stdin);
for(;key==2;)
{
printf("Are you sure?\n1.OK.\n2.Input new.\n3.Exit delete.\nYour choice:[ ]\b\b");
scanf("%d",&key);fflush(stdin);
if(key==3) return head;
if(key==2) scanf("%ld",&x);
}
q=head;
p=q->next;
if(q->number==x)
{head=p;return(head);}
while(q&&p->number!=x)
{
q=p;
p=q->next;
}
if(q)
{
q->next=p->next;
free(p);
}
else printf("NOT Found!!!\n");
return head;
}
void prt()
{
printf(" welcome to use!!! \t\t========================\n");
printf(" \t\t* Linklist Student *\n");
printf(" \t\t* by czj *\n");
printf(" \t\t* 2008 10 4 *\n");
printf(" \t\t========================\n");
}
void fprt(FILE *p)
{
fprintf(p," welcome to use!!! \t\t========================\n");
fprintf(p," \t\t* Linklist Student *\n");
fprintf(p," \t\t* by czj *\n");
fprintf(p," \t\t* 2008 10 4 *\n");
fprintf(p," \t\t========================\n");
}
void main()
{
T *head=NULL;
int key=1;
FILE *p;
prt();
head=creat();
print(head);
printf("1.INSERT.\n2.DLETE.\n3.CORRECT.\n4.RANGE.\n0.EXIT.\nYour choice[ ]\b\b");
scanf("%d",&key);fflush(stdin);
for(;key;)
{
system("cls");
prt();
if(key==1) insert(head);
if(key==2||key==3) del(head);
if(key==3) insert(head);
if(key==4) range(head);
print(head);
printf("1.INSERT.\n2.DELETE.\n3.CORRECT.\n4.RANGE.\n0.EXIT.\nYour choice[ ]\b\b");
scanf("%d",&key);fflush(stdin);
}
if((p=fopen("d:\\linklist1.dat","w"))==NULL)
{
printf("Can't open the file.\n");
exit(0);
}
fprt(p);
fprint(head,p);
fclose(p);
}
/*
最后一个节点不用。
fflush(stdin);清除缓存。
insert中输入有问题,range。
*/本回答被提问者采纳