#include "stdio.h"
#include "stdlib.h"
typedef struct student
{
int sno;
char sname[20];
float score;
}
datatype;/*学生数据类型定义*/
typedef struct node
{
datatype data;
struct node *next;
}
linklist; /*链表数据类型定义*/
linklist *head, *p; /*全局变量*/
int search()
{ int i;
printf("请输入学号:");
scanf("%d",&i);
int j;
linklist *p;
p=head;j=0;
while((p->next!=NULL)&&(j<i))
{
p=p->next;
j++;
}
if(i==j)
printf("姓名:%s\n",&p->data.sname);
printf("成绩:%f\n",&p->data.score);
}
int add()
{ datatype x;
printf("请输入学号:");
scanf("%d",&x.sno);
printf("输入姓名:");
scanf("%S",&x.sname);
printf("输入成绩:");
scanf("%f",&x.score);
{
linklist *s;
s=(linklist*)malloc(sizeof(linklist));
s->data=x;
s->next=p->next;
p->next=s;
return 1;
}
}
int stdelete(linklist *head,int sno)
{
linklist *pre,*p;
pre=head;
p=pre->next;
while (p)
{
if (sno==p->data.sno)
{pre->next=p->next;
free(p);
return 1;}
else
{pre=pre->next; p=p->next;}
}
return 0;
}
linklist *CREATLISTR1()
{ char ch;/*ch代表输入结束标志,y,n*/
linklist *head,*s,*r;
head=(linklist *)malloc(sizeof(linklist));
r=head;
ch='y';
while(ch!='n')
{ s=(linklist *)malloc(sizeof(linklist));
printf("输入学号:");
scanf("%d",&s->data.sno);
printf("输入姓名:");
scanf("%s",s->data.sname);
printf("输入成绩:");
scanf("%f",&s->data.score);
printf("是否继续输入下一个(n代表结束,其它数值继续输入):");
getchar();
scanf("%c",&ch);
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
void display()
{
linklist *p;
int i;
printf("\nThe Values of linklist is:\n");
p=head->next;
i=1;
while (p)
{
printf("第%d个学生",i);
printf("--学号:%d",p->data.sno);
printf("--姓名:%s",p->data.sname);
printf("--成绩:%f",p->data.score);
i++;
p=p->next;
printf("\n");
}
printf("\n");
}
void printmenu()
{
printf("**************************************************************\n");
printf("*1.创建单链表 2.遍历单链表 *\n");
printf("*3.查找学生 4.增加学生 *\n");
printf("*5.删除学生 0.退出 *\n");
printf("**************************************************************\n");
printf("请选择:");
}
int main()
{
int op,sno;
float avg;
printmenu();
scanf("%d",&op);
printf("\n");
while (op!=0)
{
switch(op)
{
case 1:
head=CREATLISTR1();
printmenu();
break;
case 2:
display();
printmenu();
break;
case 3:
search();
printmenu();
break;
case 4:
add();
printf("添加成功");
printmenu();
break;
case 5:
printf("请输入要删除的学号:");
scanf("%d",&sno);
printf("\n");
if (stdelete(head,sno))
printf("删除成功!!\n");
else printf("删除失败!!\n");
printmenu();
break;
default: printf("Wrong Operation Selected!\n"); break;
}
scanf("%d",&op);
}
}
#include "stdlib.h"
typedef struct student
{
int sno;
char sname[20];
float score;
}
datatype;/*学生数据类型定义*/
typedef struct node
{
datatype data;
struct node *next;
}
linklist; /*链表数据类型定义*/
linklist *head, *p; /*全局变量*/
int search()
{ int i;
printf("请输入学号:");
scanf("%d",&i);
int j;
linklist *p;
p=head;j=0;
while((p->next!=NULL)&&(j<i))
{
p=p->next;
j++;
}
if(i==j)
printf("姓名:%s\n",&p->data.sname);
printf("成绩:%f\n",&p->data.score);
}
int add()
{ datatype x;
printf("请输入学号:");
scanf("%d",&x.sno);
printf("输入姓名:");
scanf("%S",&x.sname);
printf("输入成绩:");
scanf("%f",&x.score);
{
linklist *s;
s=(linklist*)malloc(sizeof(linklist));
s->data=x;
s->next=p->next;
p->next=s;
return 1;
}
}
int stdelete(linklist *head,int sno)
{
linklist *pre,*p;
pre=head;
p=pre->next;
while (p)
{
if (sno==p->data.sno)
{pre->next=p->next;
free(p);
return 1;}
else
{pre=pre->next; p=p->next;}
}
return 0;
}
linklist *CREATLISTR1()
{ char ch;/*ch代表输入结束标志,y,n*/
linklist *head,*s,*r;
head=(linklist *)malloc(sizeof(linklist));
r=head;
ch='y';
while(ch!='n')
{ s=(linklist *)malloc(sizeof(linklist));
printf("输入学号:");
scanf("%d",&s->data.sno);
printf("输入姓名:");
scanf("%s",s->data.sname);
printf("输入成绩:");
scanf("%f",&s->data.score);
printf("是否继续输入下一个(n代表结束,其它数值继续输入):");
getchar();
scanf("%c",&ch);
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
void display()
{
linklist *p;
int i;
printf("\nThe Values of linklist is:\n");
p=head->next;
i=1;
while (p)
{
printf("第%d个学生",i);
printf("--学号:%d",p->data.sno);
printf("--姓名:%s",p->data.sname);
printf("--成绩:%f",p->data.score);
i++;
p=p->next;
printf("\n");
}
printf("\n");
}
void printmenu()
{
printf("**************************************************************\n");
printf("*1.创建单链表 2.遍历单链表 *\n");
printf("*3.查找学生 4.增加学生 *\n");
printf("*5.删除学生 0.退出 *\n");
printf("**************************************************************\n");
printf("请选择:");
}
int main()
{
int op,sno;
float avg;
printmenu();
scanf("%d",&op);
printf("\n");
while (op!=0)
{
switch(op)
{
case 1:
head=CREATLISTR1();
printmenu();
break;
case 2:
display();
printmenu();
break;
case 3:
search();
printmenu();
break;
case 4:
add();
printf("添加成功");
printmenu();
break;
case 5:
printf("请输入要删除的学号:");
scanf("%d",&sno);
printf("\n");
if (stdelete(head,sno))
printf("删除成功!!\n");
else printf("删除失败!!\n");
printmenu();
break;
default: printf("Wrong Operation Selected!\n"); break;
}
scanf("%d",&op);
}
}