#include <stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student //学生信息存储结构
{
char num[20]; //学号
char name[20]; //姓名
int price; //成绩
struct student *next;
} student;
int Menu_select(); //菜单
struct student *Create(student *head); //输入
void Print(student *head); //输出
struct student *Del(student *head); //删除
struct student *Insert(student *head); //插入
void look1(student *head); //按姓名查找
void look2(student *head); //按位置查找
void sum(student *head); //查找人数
int main()
{
student *head=NULL;
while(1)
{
switch(Menu_select())
{
case 1:
head=Create(head);
break;
case 2:
Print(head);
break;
case 3:
look1(head);
break;
case 4:
look2(head);
break;
case 5:
head=Insert(head);
break;
case 6:
head=Del(head);
break;
case 7:
sum(head);
break;
case 8:
exit(0);
}
}
return 0;
}
int Menu_select()
{
int choice;
printf("***************************菜单***************\n");
printf(" 1 输入 \n");
printf(" 2 输出 \n");
printf(" 3 按姓名查找 \n");
printf(" 4 按位置查找 \n");
printf(" 5 插入 \n");
printf(" 6 删除 \n");
printf(" 7 输出人数 \n");
printf(" 8 退出 \n");
printf("Enter your choice(1-8):");
scanf("%d",&choice);
return choice;
}
struct student *Create(student *head)
{
student *p,*q;
int n;
head=(student *)malloc(sizeof(student));
head->next=NULL;
q=head;
printf("请输入总人数\n");
scanf("%d",&n);
while(n--)
{
p=(student*)malloc(sizeof(student));
printf("请输入学号:");
scanf("%s",p->num);
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入成绩:");
scanf("%d",&p->price);
q->next=p;
q=p;
}
q->next=NULL;
return head;
}
void Print(student *head)
{
student *p=head;
printf("学号 姓名 成绩\n");
while(p->next)
{
p=p->next;
printf("%s %s %d\n",p->num,p->name,p->price);
}
}
struct student *Del(student *head)
{
int i,j=0,k=0;
student *p=head,*q;
printf("输入要删除的学生位置:");
scanf("%d",&i);
while(p->next)
{if(i!=j)
{
q=p;
p=p->next;
j++;
}
else
{
q->next=p->next;
return head;
k=1;
printf("删除成功!\n");
break;
}
}
if(k==0&&i!=j)
printf("删除失败!\n");
else
{
q->next=NULL;
printf("删除成功!\n");
return head;
}
}
struct student *Insert(student *head)
{
int i,j=0,k=0;
student *p,*q=head,*t,*t1;
printf("请输入要插入的学生位置:");
scanf("%d",&i);
p=(student *)malloc(sizeof(student));
printf("请输入要增加的学生信息:\n");
printf("请输入学号:");
scanf("%s",p->num);
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入成绩:");
scanf("%d",&p->price);
while(q->next)
{if(i!=j)
{t1=q;
q=q->next;
t=q;
j++;
}
else
{
p->next=t->next;
t->next=p;
printf("添加成功!\n");
k=1;
return head;
break;
}
}
if(k==0&&i!=j&&i!=j+1)
printf("添加失败!\n");
else if(k==0&&i==j)
{
p->next=t;
t1->next=p;
printf("添加成功!\n");
return head;
}
else if(k==0&&(i=j+1))
{
q->next=p;
p->next=NULL;
return head;
printf("添加成功!\n");
}
}
void look1(student *head)
{ int k=0;
char no[20];
student *p=head->next;
printf("请输入要查询的学生姓名:");
scanf("%s",no);
while(p)
{if(strcmp(p->name,no)!=0)
p=p->next;
else
{
printf("学号 姓名 成绩\n");
printf("%s %s %d\n",p->num,p->name,p->price);
k=1;
break;
}
}
if(k==0)
printf("查询失败!\n");
}
void look2(student *head)
{
int i,j=1,k=0;
student *p=head->next;
printf("请输入要查找学生的位置: ");
scanf("%d",&i);
while(p)
{if(j==i)
{
printf("学号 姓名 成绩\n");
printf("%s %s %d\n",p->num,p->name,p->price);
k=1;
break;
}
else
{
j++;
p=p->next;
}}
if(k==0)
printf("查询失败!\n");
}
void sum(student *head)
{int i=1;
student *p=head->next;
while(p->next)
{i++;
p=p->next;
}
printf("共有人数:%d人\n",i);
}
#include<string.h>
#include<stdlib.h>
typedef struct student //学生信息存储结构
{
char num[20]; //学号
char name[20]; //姓名
int price; //成绩
struct student *next;
} student;
int Menu_select(); //菜单
struct student *Create(student *head); //输入
void Print(student *head); //输出
struct student *Del(student *head); //删除
struct student *Insert(student *head); //插入
void look1(student *head); //按姓名查找
void look2(student *head); //按位置查找
void sum(student *head); //查找人数
int main()
{
student *head=NULL;
while(1)
{
switch(Menu_select())
{
case 1:
head=Create(head);
break;
case 2:
Print(head);
break;
case 3:
look1(head);
break;
case 4:
look2(head);
break;
case 5:
head=Insert(head);
break;
case 6:
head=Del(head);
break;
case 7:
sum(head);
break;
case 8:
exit(0);
}
}
return 0;
}
int Menu_select()
{
int choice;
printf("***************************菜单***************\n");
printf(" 1 输入 \n");
printf(" 2 输出 \n");
printf(" 3 按姓名查找 \n");
printf(" 4 按位置查找 \n");
printf(" 5 插入 \n");
printf(" 6 删除 \n");
printf(" 7 输出人数 \n");
printf(" 8 退出 \n");
printf("Enter your choice(1-8):");
scanf("%d",&choice);
return choice;
}
struct student *Create(student *head)
{
student *p,*q;
int n;
head=(student *)malloc(sizeof(student));
head->next=NULL;
q=head;
printf("请输入总人数\n");
scanf("%d",&n);
while(n--)
{
p=(student*)malloc(sizeof(student));
printf("请输入学号:");
scanf("%s",p->num);
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入成绩:");
scanf("%d",&p->price);
q->next=p;
q=p;
}
q->next=NULL;
return head;
}
void Print(student *head)
{
student *p=head;
printf("学号 姓名 成绩\n");
while(p->next)
{
p=p->next;
printf("%s %s %d\n",p->num,p->name,p->price);
}
}
struct student *Del(student *head)
{
int i,j=0,k=0;
student *p=head,*q;
printf("输入要删除的学生位置:");
scanf("%d",&i);
while(p->next)
{if(i!=j)
{
q=p;
p=p->next;
j++;
}
else
{
q->next=p->next;
return head;
k=1;
printf("删除成功!\n");
break;
}
}
if(k==0&&i!=j)
printf("删除失败!\n");
else
{
q->next=NULL;
printf("删除成功!\n");
return head;
}
}
struct student *Insert(student *head)
{
int i,j=0,k=0;
student *p,*q=head,*t,*t1;
printf("请输入要插入的学生位置:");
scanf("%d",&i);
p=(student *)malloc(sizeof(student));
printf("请输入要增加的学生信息:\n");
printf("请输入学号:");
scanf("%s",p->num);
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入成绩:");
scanf("%d",&p->price);
while(q->next)
{if(i!=j)
{t1=q;
q=q->next;
t=q;
j++;
}
else
{
p->next=t->next;
t->next=p;
printf("添加成功!\n");
k=1;
return head;
break;
}
}
if(k==0&&i!=j&&i!=j+1)
printf("添加失败!\n");
else if(k==0&&i==j)
{
p->next=t;
t1->next=p;
printf("添加成功!\n");
return head;
}
else if(k==0&&(i=j+1))
{
q->next=p;
p->next=NULL;
return head;
printf("添加成功!\n");
}
}
void look1(student *head)
{ int k=0;
char no[20];
student *p=head->next;
printf("请输入要查询的学生姓名:");
scanf("%s",no);
while(p)
{if(strcmp(p->name,no)!=0)
p=p->next;
else
{
printf("学号 姓名 成绩\n");
printf("%s %s %d\n",p->num,p->name,p->price);
k=1;
break;
}
}
if(k==0)
printf("查询失败!\n");
}
void look2(student *head)
{
int i,j=1,k=0;
student *p=head->next;
printf("请输入要查找学生的位置: ");
scanf("%d",&i);
while(p)
{if(j==i)
{
printf("学号 姓名 成绩\n");
printf("%s %s %d\n",p->num,p->name,p->price);
k=1;
break;
}
else
{
j++;
p=p->next;
}}
if(k==0)
printf("查询失败!\n");
}
void sum(student *head)
{int i=1;
student *p=head->next;
while(p->next)
{i++;
p=p->next;
}
printf("共有人数:%d人\n",i);
}