#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LENGTH sizeof(struct student)
struct student
{
int number;
char sex;/*M(male) or F(famal)*/
int age;
double score;
struct student *next;
};
void main()
{
struct student *del(struct student *del,int key);/*删除链表中key这个结点值*/
void output(struct student *P);/*输出一个链表的函数*/
struct student *insert(struct student *head,struct student *in);/*插入一个结点放在此链表的最后面*/
struct student *creat(int m);/*创建一个新的链表*/
void start();
struct student *new_list=NULL;
struct student plug;/*插入的结点*/
char select;/*输入界面的先择*/
int b=0;/*删除链表中的结点的number*/
int s=0; /*创建链表时的结点数*/
printf("**学生结构体包括number,sex(M or F),age,score,*p几个成员**\n");
printf("***输入时请注意:number age 请输入整数***\n");
printf("***score输入整数或小数\t sex输入M或F***\n");
start();/*进入时的界面*/
select=getchar();
switch(select)
{
case 'C':
{
printf("请输入您想要创建的链表的结点数目:");
scanf("%d",&s);
new_list=creat(s);
printf("您要创建的列表是:\n");
output(new_list);
break;
}
case 'D':
{
printf("请输入您想要删除的链表中结点的序号:");
scanf("%d",&b);
new_list=del(new_list,b);
break;
}
case 'I':
{
printf("请输入您想插入链表中结点的学生的序号:");
scanf("%d",&plug.number);
printf("请输入您想插入链表中结点的学生的性别:");
plug.sex=getchar();
plug.sex=getchar();
printf("请输入您想插入链表中结点的学生的年龄:");
scanf("%d",&plug.age);
printf("请输入您想插入链表中结点的学生的分数:");
scanf("%lf",&plug.score);
new_list=insert(new_list,&plug);
printf("插入新结点后的列表为:\n");
output(new_list);
break;
}
default:
{
printf("您的输入有误!!\n");
}
}
}
struct student *creat(int m)/*建一个m个结点的链表函数*/
{
struct student *head;
struct student *temp1,*temp2;
head=NULL;
int n=0;
temp1=temp2=(struct student*) malloc(LENGTH);
printf("You will be creat a list.\n");
printf("\tYou are creating the %d node.\n",n+1);
printf("Please input the number:");
scanf("%d",&temp1->number);
printf("please input the sex:");
/*scanf("%c",temp1->sex);*/
temp1->sex=getchar();
/*得同时使用两个getchar(),若使用一个将一跳而过,原因可能是
因为输入前一个age时的回车读进来了*/
temp1->sex=getchar();
printf("Please input the age:");
scanf("%d",&temp1->age);
printf("Please input the score:");
scanf("%lf",&temp1->score);
while(n!=m+1)
{
n=n+1;
if(n==1)/*判断是否为第一个结点,是则将head的地址指向它*/
{
head=temp1;
}
else/*不是第1个结点,则将前一个节点next指向刚新建的节点,并将temp2现次指向尾结点*/
{
temp2->next=temp1;
temp2=temp1;
temp1=(struct student*)malloc(LENGTH);
printf("\tYou are creating the %d node.\n",n);
printf("Please input the number:");
scanf("%d",&temp1->number);
printf("please input the sex:");
temp1->sex=getchar();
temp1->sex=getchar();
printf("Please input the age:");
scanf("%d",&temp1->age);
printf("Please input the score:");
scanf("%lf",&temp1->score);
}
}
temp2->next=NULL;/*已建立起m个结点,将temp2指向尾节点*/
printf("you have created a list with %d node.\n",m);
return(head);
}
