5830 根据读入的学生信息,利用尾插法创建链表,多组输入数据
#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
int sno;
char str[20];
double s1;
double s2;
double s3;
double avg;
double sum;
struct lnode *next;
}LNode,*LinkList;
void create(LinkList H,int n)
{
int i;
LNode *r;
r=H;
LNode *s;
for(i=0;i<n;i++)
{
s=(LNode *)malloc(sizeof(LNode));
scanf("%d",&s->sno);
scanf("%s",s->str);
scanf("%lf",&s->s1);
scanf("%lf",&s->s2);
scanf("%lf",&s->s3);
s->sum=(s->s1+s->s2+s->s3);
s->avg=s->sum/3;
s->next=r->next;
r->next=s;
r=s;
}
r->next=NULL;
}
void output(LinkList H)
{
LNode *p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->sno);
printf("%s ",p->str);
printf("%.2lf ",p->s1);
printf("%.2lf ",p->s2);
printf("%.2lf ",p->s3);
printf("%.2lf ",p->avg);
printf("%.2lf\n",p->sum);
p=p->next;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
LinkList H;
H=(LNode *)malloc(sizeof(LNode));
create(H,n);
output(H);
}
return 0;
}
5831 利用第一题创建的链表,写一个按学号查找函数,没有找到则输出0
#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
int sno;
char str[20];
double s1;
double s2;
double s3;
double avg;
double sum;
struct lnode *next;
}LNode,*LinkList;
void create(LinkList H,int n)
{
int i;
LNode *r;
r=H;
LNode *s;
for(i=0;i<n;i++)
{
s=(LNode *)malloc(sizeof(LNode));
scanf("%d",&s->sno);
scanf("%s",s->str);
scanf("%lf",&s->s1);
scanf("%lf",&s->s2);
scanf("%lf",&s->s3);
s->sum=(s->s1+s->s2+s->s3);
s->avg=s->sum/3;
s->next=r->next;
r->next=s;
r=s;
}
r->next=NULL;
}
void output(LinkList H)
{
LNode *p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->sno);
printf("%s ",p->str);
printf("%.2lf ",p->s1);
printf("%.2lf ",p->s2);
printf("%.2lf ",p->s3);
printf("%.2lf ",p->avg);
printf("%.2lf\n",p->sum);
p=p->next;
}
}
void search(LinkList H,int no)
{
int num=0;
LNode *p;
p=H->next;
while(p!=NULL)
{
if(p->sno==no)
{
num++;
printf("%d ",num);
printf("%d ",p->sno);
printf("%s ",p->str);
printf("%.2lf ",p->s1);
printf("%.2lf ",p->s2);
printf("%.2lf ",p->s3);
printf("%.2lf ",p->avg);
printf("%.2lf\n",p->sum);
break;
}
else
{
num++;
p=p->next;
}
}
if(p==NULL)
{
printf("0\n");
}
}
void output2(LinkList H,LNode *s)
{
LNode *p;
p=H->next;
if(s==NULL)
{
printf("0\n");
return;
}
while(p!=NULL&&s)
{
}
}
int main()
{
int n;
int index;
while(scanf("%d",&n)!=EOF)
{
LinkList H;
H=(LNode *)malloc(sizeof(LNode));
create(H,n);
scanf("%d",&index);
search(H,index);
//output(H);
}
return 0;
}
/*
5
20140101 LiMing 85 90 92
20140202 ZhaoLi 98 78 88
20140013 QianEn 92.5 85.6 78.5
20140404 TianYa 88.5 68.6 94
20140015 LuYao 89.4 86.5 88
*/
5832 尾插法建立链表,向创建完且学号有序的链表中插入一个新学生信息,插入后链表仍然按照学号有序排列
#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
int sno;
char str[20];
double s1;
double s2;
double s3;
double avg;
double sum;
struct lnode *next;
}LNode,*LinkList;
void yxinsert(LinkList H,int sa)
{
LNode *p,*pre;
p=H->next;pre=H;
while(p!=NULL&&p->sno<sa)
{
pre=p;
p=p->next;
}
LNode *s;
s=(LNode *)malloc(sizeof(LNode));
s->sno=sa;
scanf("%s",s->str);
scanf("%lf",&s->s1);
scanf("%lf",&s->s2);
scanf("%lf",&s->s3);
s->sum=(s->s1+s->s2+s->s3);
s->avg=s->sum/3;
s->next=pre->next;
pre->next=s;
}
void create(LinkList H,int n)
{
int i;
LNode *r;
r=H;
LNode *s;
for(i=0;i<n;i++)
{
s=(LNode *)malloc(sizeof(LNode));
scanf("%d",&s->sno);
scanf("%s",s->str);
scanf("%lf",&s->s1);
scanf("%lf",&s->s2);
scanf("%lf",&s->s3);
s->sum=(s->s1+s->s2+s->s3);
s->avg=s->sum/3;
s->next=r->next;
r->next=s;
r=s;
}
r->next=NULL;
}
void output(LinkList H)
{
LNode *p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->sno);
printf("%s ",p->str);
printf("%.2lf ",p->s1);
printf("%.2lf ",p->s2);
printf("%.2lf ",p->s3);
printf("%.2lf ",p->avg);
printf("%.2lf\n",p->sum);
p=p->next;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sno;
LinkList H;
H=(LNode *)malloc(sizeof(LNode));
create(H,n);
scanf("%d",&sno);
yxinsert(H,sno);
output(H);
}
return 0;
}
/*
3
1001 Wang 90 80 70
1003 Li 60 50 40
1005 Zhang 90 90 90
1004 QQQ 33 44 55
*/
5833 在第一题创建完的链表中按照学号删除一个学生信息(学号唯一)
#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
int sno;
char str[20];
double s1;
double s2;
double s3;
double avg;
double sum;
struct lnode *next;
}LNode,*LinkList;
void yxinsert(LinkList H,int sa)
{
LNode *p,*pre;
p=H->next;pre=H;
while(p!=NULL&&p->sno<sa)
{
pre=p;
p=p->next;
}
LNode *s;
s=(LNode *)malloc(sizeof(LNode));
s->sno=sa;
scanf("%s",s->str);
scanf("%lf",&s->s1);
scanf("%lf",&s->s2);
scanf("%lf",&s->s3);
s->sum=(s->s1+s->s2+s->s3);
s->avg=s->sum/3;
s->next=pre->next;
pre->next=s;
}
void create(LinkList H,int n)
{
int i;
LNode *r;
r=H;
LNode *s;
for(i=0;i<n;i++)
{
s=(LNode *)malloc(sizeof(LNode));
scanf("%d",&s->sno);
scanf("%s",s->str);
scanf("%lf",&s->s1);
scanf("%lf",&s->s2);
scanf("%lf",&s->s3);
s->sum=(s->s1+s->s2+s->s3);
s->avg=s->sum/3;
s->next=r->next;
r->next=s;
r=s;
}
r->next=NULL;
}
int delete(LinkList H,int sa)
{
LNode *p,*pre;
p=H->next;pre=H;
while(p!=NULL&&p->sno!=sa)
{
pre=p;
p=p->next;
}
if(p==NULL)
{
return 0;
}
else
{
pre->next=p->next;
free(p);
return 1;
}
}
void output(LinkList H)
{
LNode *p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->sno);
printf("%s ",p->str);
printf("%.2lf ",p->s1);
printf("%.2lf ",p->s2);
printf("%.2lf ",p->s3);
printf("%.2lf ",p->avg);
printf("%.2lf\n",p->sum);
p=p->next;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sno;
LinkList H;
H=(LNode *)malloc(sizeof(LNode));
create(H,n);
scanf("%d",&sno);
int l;
//yxinsert(H,sno);
l=delete(H,sno);
if(l)
{
output(H);
}
else
{
printf("Error\n");
}
}
return 0;
}
/*
3
1001 Wang 90 80 70
1003 Li 60 50 40
1005 Zhang 90 90 90
1004 QQQ 33 44 55
*/
5834 也是建立有序链表和实验9类似,只不过链表里的结点多了一些信息
#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
int sno;
char str[20];
double s1;
double s2;
double s3;
double avg;
double sum;
struct lnode *next;
}LNode,*LinkList;
void yxinsert(LinkList H,LNode *s)
{
LNode *p,*pre;
p=H->next;pre=H;
while(p!=NULL&&p->sum>s->sum)
{
pre=p;
p=p->next;
}
s->next=pre->next;
pre->next=s;
}
void create(LinkList H,int n)
{
int i;
int sno;char s[20];double s1,s2,s3,avg,sum;
for(i=0;i<n;i++)
{
LNode *s;
s=(LNode *)malloc(sizeof(LNode));
scanf("%d",&s->sno);
scanf("%s",s->str);
scanf("%lf",&s->s1);
scanf("%lf",&s->s2);
scanf("%lf",&s->s3);
s->sum=(s->s1+s->s2+s->s3);
s->avg=s->sum/3;
yxinsert(H,s);
}
}
int delete(LinkList H,int sa)
{
LNode *p,*pre;
p=H->next;pre=H;
while(p!=NULL&&p->sno!=sa)
{
pre=p;
p=p->next;
}
if(p==NULL)
{
return 0;
}
else
{
pre->next=p->next;
free(p);
return 1;
}
}
void output(LinkList H)
{
LNode *p;
p=H->next;
while(p!=NULL)
{
printf("%d ",p->sno);
printf("%s ",p->str);
printf("%.2lf ",p->s1);
printf("%.2lf ",p->s2);
printf("%.2lf ",p->s3);
printf("%.2lf ",p->avg);
printf("%.2lf\n",p->sum);
p=p->next;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sno;
LinkList H;
H=(LNode *)malloc(sizeof(LNode));
H->next=NULL;
create(H,n);
output(H);
}
return 0;
}
/*
3
1001 Wang 90 80 70
1003 Li 60 50 40
1005 Zhang 90 90 90
1004 QQQ 33 44 55
*/