#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int no;
int age;
int score;
struct node *next;
}node;
node *creatINFO(node *&head)
{
node *p,*tail;
int temp=1;
tail=head;
printf("----创建信息表----\n");
while(temp != 2)
{
p = (node*)malloc(sizeof node);
printf ("输入 No:");
scanf ("%d",&(p->no));
printf ("输入 age:");
scanf ("%d",&(p->age) );
printf ("输入 score:");
scanf ("%d",&(p->score) );
p->next = NULL;
tail -> next = p;
tail = p;
printf ("继续建立新的学生信息(1.确定 2.退出):");
scanf("%d",&temp);
}
return head;
}
void showINFO(node* studentINFO)
{
int temp1;
node *tail,*p,*q;
printf("----打印----\n");
tail=studentINFO;
p=tail->next;
q=p->next;
printf("是否按照score的递增打印?(1.确定 2.取消)");
scanf("%d",&temp1);
if(temp1 == 2)
{
studentINFO=studentINFO->next;
while(studentINFO!=NULL)
{
printf("no:%d\t",studentINFO->no);
printf("age:%d\t",studentINFO->age);
printf("score:%d\n",studentINFO->score);
studentINFO=studentINFO->next;
}
}
if(temp1 == 1)
{
for(studentINFO=studentINFO->next;studentINFO->next!=NULL;studentINFO=studentINFO->next)
{
if(p->score >= q->score)
{
tail->next=q;
p->next=q->next;
q->next=p;
}
}
studentINFO=studentINFO->next;
while(studentINFO!=NULL)
{
printf("no:%d\t",studentINFO->no);
printf("age:%d\t",studentINFO->age);
printf("score:%d\n",studentINFO->score);
studentINFO=studentINFO->next;
}
}
}
void findINFO(node* studentINFO)
{
int find;
printf("----查找----\n");
printf("请输入要查找学生的NO:");
scanf("%d",&find);
studentINFO=studentINFO->next;
while(studentINFO!=NULL)
{
if(find==studentINFO->no)
{ printf("no:%d\t",studentINFO->no);
printf("age:%d\t",studentINFO->age);
printf("score:%d\n",studentINFO->score);
break;
}
else
studentINFO=studentINFO->next;
}
}
node *delINFO(node *studentINFO)
{
int no;
node *fre;
printf("----删除----\n");
printf("请输入要删除的学生的no:");
scanf("%d",&no);
studentINFO=studentINFO->next;
while(studentINFO->next != NULL);
{
if(no==studentINFO->next->no)
{
fre=studentINFO->next;
studentINFO->next=studentINFO->next->next;
free(fre);
}
else
studentINFO=studentINFO->next;
}
return studentINFO;
}
node *inst(node *studentINFO)
{
node* temp;
int ins;
printf("----插入----\n");
temp=(node*)malloc(sizeof node);
temp->next=NULL;
printf ("输入 No:");
scanf ("%d",&(temp->no));
printf ("输入 age:");
scanf ("%d",&(temp->age) );
printf ("输入 score:");
scanf ("%d",&(temp->score) );
printf("你想在插在no多少前:");
scanf("%d",&ins);
studentINFO=studentINFO->next;
while(studentINFO->next != NULL);
{
if(ins==studentINFO->no)
{
temp->next=studentINFO->next->next;
studentINFO->next=temp;
}
}
return studentINFO;
}
void main()
{
node *studentINFO;
int i;
studentINFO=(node*)malloc(sizeof node);
studentINFO->next=NULL;
while(1)
{
printf("**********************请选择需要的操作**********************\n");
printf("0 退出\n");
printf("1 创建新的学生信息表\n");
printf("2 打印学生信息表\n");
printf("3 查找并打印单个学生信息\n");
printf("4 删除单个学生信息\n");
printf("5 创建单个学生信息并插入到学生信息表\n");
scanf("%d",&i);
switch(i)
{
case 0:
return;
case 1:
creatINFO(studentINFO); break;
case 2:
showINFO(studentINFO); break;
case 3:
findINFO(studentINFO); break;
case 4:
delINFO(studentINFO); break;
case 5:
inst(studentINFO);break;
}
}
}
| |
[此贴子已经被作者于2006-5-17 20:50:56编辑过]