@Arbalest-Laevatain
2018-07-04T04:05:59.000000Z
字数 5815
阅读 940
C语言
/******
struct date{int year; int month; int day;}; //定义日期结构体类型
struct student
{ char name[20];
struct date birth; //出生日期
};
结构体数组s存储了n个人的名字和出生日期。写一函数,求这n个人中年龄最大
(即出生日期最小)者的姓名。
char *oldest(student s[], int n)
{
struct student *s0,*od;
for (od=s,s0=s+1;s0<s+n;s0++)
{
if (od->birth.year>(s0)->birth.year)
{
*od=*(s0);
}
else if (od->birth.year == (s0)->birth.year)
{
if (od->birth.month>(s0)->birth.month)
*od=*(s0);
else if (od->birth.month == (s0)->birth.month)
{
if (od->birth.day>(s0)->birth.day)
*od=*(s0);
}
}
}
return (od->name);
}
struct date{int year; int month; int day;}; //日期结构体类型
struct studentNode //链表结点的结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next;
};
结构体链表L存储了n个人的名字和出生日期。写一函数,求这n个人中
年龄最大(即出生日期最小)者的名字。
char *oldest(struct studentNode *L)
/* 若L是空表,则返回空指针null
否则返回表中年龄最大者的名字
*/
{
studentNode *p,*od;
p=L;od=L;
while (p!=NULL)
{
if (od->birth.year>p->birth.year)
{
*od=*p;
}
else if (od->birth.year == p->birth.year)
{
if (od->birth.month>p->birth.month)
*od=*p;
else if (od->birth.month == p->birth.month)
{
if (od->birth.day>p->birth.day)
*od=*p;
}
}
p=p->next;
}
return (od->name);
}
#include <stdio.h>
#include <string.h>
struct date
{
int year;
int month;
int day;
}; //定义日期结构体类型
struct studentNode //链表结点的结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next;
};
char *oldest(struct studentNode *L)
/* 若L是空表,则返回空指针null
否则返回表中年龄最大者的名字
*/
{
studentNode *p,*od;
p=L+1;od=L;
while (p->next!=NULL)
{
if (od->birth.year>p->birth.year)
{
*od=*p;
}
else if (od->birth.year == p->birth.year)
{
if (od->birth.month>p->birth.month)
*od=*p;
else if (od->birth.month == p->birth.month)
{
if (od->birth.day>p->birth.day)
*od=*p;
}
}
p=p->next;
}
return (od->name);
}
//#define n 10
int main()
{
int i;
char o[20];
char *old=o;
struct studentNode a[3];
for (i=0;i<3;i++)
{
printf("请输入同学%d的姓名\n",i+1);
scanf("%s",&a[i].name);
printf("请输入同学%d的生日\n",i+1);
scanf("%d%d%d",&a[i].birth.year,&a[i].birth.month,&a[i].birth.day);
a[i]
}
old=oldest(a,3);
printf("%s\n",old);
return 0;
}
/******
struct course
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
};
结构体数组c存储了n门课程的信息。写一函数,求学期s的总学分。
******/
float creditSum(struct course c[], int n, int s)
{
int i=0;
float sum=0;
for (i=0;i<n;i++)
{
if (c[i].semester==s)
sum+=c[i].credit;
}
return sum;
}
/******
struct courseNode //课程链表结点的结构体类型
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
struct courseNode *next;
};
结构体链表Lc存储了各学期多门课程的信息。写一函数,求学
期s的总学分。
******/
float creditSum(struct courseNode *Lc, int s)
/* 若Lc是空表,则返回0;
否则返回学期s的总学分
*/
{
struct courseNode *p;
float sum=0.0;
if (NULL==Lc)
{
return 0.0; //注意这里如果是0可能会报错(类型不匹配)
}
else
{
p=Lc;
while (p!=NULL)
{
if (p->semester==s)
sum+=p->credit;
p=p->next;
}
}
return sum;
}
/******
struct date{int year; int month; int day;}; //日期结构体类型
struct student //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
};
结构体数组s存储了n个人的名字和出生日期。写一函数,由数组s中n个人
的信息及其顺序构造相应的链表。链表的结点的结构体类型定义如下:
struct studentNode //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next
};
******/
struct studentNode *CreateLinkList(struct student s[], int n)
{
struct studentNode* L,*p0;
L = (studentNode*) malloc (sizeof(studentNode));
L->next=NULL;
struct student *x;
if (NULL==s || n==0) return NULL;
x=s;
L->birth.year=x->birth.year;
L->birth.month=x->birth.month;
L->birth.day=x->birth.day;
strcpy(L->name,x->name);
x++;
p0=L;
for (;x<s+n;x++)
{
struct studentNode* p;
p = (studentNode*) malloc (sizeof(studentNode));
p->birth.year=x->birth.year;
p->birth.month=x->birth.month;
p->birth.day=x->birth.day;
strcpy(p->name,x->name);
p->next=p0->next;
p0->next=p;
p0=p0->next;
}
return L;
}
/******
struct courseNode //课程链表结点的结构体类型
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
struct courseNode *next;
};
结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程的学分修改为t。
******/
struct courseNode *creditChange(struct courseNode *Lc, int c, float t)
/* 若课程c不存在,则修改不成功,返回null;
否则修改该课程的学分为t,返回指向该课程结点的指针。
*/
{
struct courseNode*p;
p=Lc;
while (p!=NULL)
{
if (p->cID==c)
{
p->credit=t;
return p;
}
p=p->next;
}
return NULL;
}
/******
struct courseNode //课程链表结点的结构体类型
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
struct courseNode *next;
};
结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程结点删除。
******/
struct courseNode *deleteCourse(struct courseNode **Lc, int c)
/* 若在链表Lc中课程c不存在,则删除不成功,返回null;
否则从链表Lc中删除该课程结点,并返回指向该课程结点的指针。
*/
{
struct courseNode*p;
p=*Lc;
while (p!=NULL)
{
if (p->next->cID==c)
{
struct courseNode*t=p->next;
p->next=p->next->next;
return t;
}
if (p->next==NULL)
{
if (p->cID==c)
{
Lc[0]=NULL;
return p;
}
}
p=p->next;
}
return NULL;
}
/******
struct node{
char ch;
struct node *next;
};
编写函数,对单向链表L实现就地逆置,即将所有结点
的指针反向,原链头当作链尾,原链尾当作链头,并返
回逆置后链表的头指针。
******/
struct node *inverse(struct node *L)
{
struct node *t,*p;
p=L->next;
L->next=NULL;
while (p!=NULL)
{
t=p->next;
p->next=L;
L=p;
p=t;
}
return L;
}
/******
struct node{
char ch;
struct node *next;
};
编写函数,对单向链表L实现排序,即按结点的ch值,
从小到大重构链表L,并返回排序后的链表的头指针。
******/
struct node *sorting(struct node *L)
/* 对单向链表L实现从小到大排序,
并返回重构后的链表的头指针。
*/
{
struct node *p=L,*tq;
int n=0,i=0;
if (NULL==L) return NULL;
char t;
while (p!=NULL)
{
p=Lp->next;
n++;
}
p=L;
for (t=L;t!=NULL;t;i<n-1;i++)
{
forp=L;
while (p=L->next;p!=NULL;p++)
{
q=p->next;
if (t(p->ch) > p(q->ch)
)
{
char tmp=t t=p->ch;
t p->ch=pq->ch;
p q->ch=tmp;
}
p=p->next;
t=p
p=p->next;
}
}
return L;
}
答案1(ERROR 1):
struct node *p=L,*q;
int n=0,i=0;
char t;
while (p!=NULL)
{
p=p->next;
n++;
}
p=L;
for (;i<n-1;i++)
{
while (p!=NULL)
{
q=p-<next;
if ((p->ch) > (q->ch))
{
t=p->ch;
p->ch=q->ch;
q->ch=t;
}
if (q->next!=NULL)
p=p->next;
else p=NULL;
}
}
return L;
答案2(ERROR 2):
char t;
struct node*p=L,*q=L;
while (L)
{
p=L-next;
while (p)
{
if (L-ch > p->ch)
{
t=L->ch;
L->ch=p->ch;
p->ch=t;
}
}
}
其他同学的答案:
struct node *p=L,*q;
int n=0,i=0;
char t;
while(p!=NULL)
{
p=p->next;
++n;
}
p=L;
for(;i<n-1;i++)
{
while(p!=NULL)
{
q=p->next;
if((p->ch)>(q->ch))
{
t=p->ch,p->ch=q->ch,q->ch=t;
}
if(q->next!=NULL)
p=p->next;
else p=NULL;
}
}
p=L;
return L;
https://leetcode.com/problems/insertion-sort-list/description/
https://www.cnblogs.com/torresliang/p/4798099.html
可能值得关注的rss订阅
http://feed.cnblogs.com/blog/u/147990/rss