[关闭]
@Arbalest-Laevatain 2018-07-04T04:05:59.000000Z 字数 5815 阅读 940

Anyview 第九章 自定义类型与结构体

C语言


/******

【习题9.023】结构体类型定义如下:

struct date{int year; int month; int day;}; //定义日期结构体类型
struct student
{  char name[20]; 
   struct date birth; //出生日期
};

结构体数组s存储了n个人的名字和出生日期。写一函数,求这n个人中年龄最大
(即出生日期最小)者的姓名。

  1. char *oldest(student s[], int n)
  2. {
  3. struct student *s0,*od;
  4. for (od=s,s0=s+1;s0<s+n;s0++)
  5. {
  6. if (od->birth.year>(s0)->birth.year)
  7. {
  8. *od=*(s0);
  9. }
  10. else if (od->birth.year == (s0)->birth.year)
  11. {
  12. if (od->birth.month>(s0)->birth.month)
  13. *od=*(s0);
  14. else if (od->birth.month == (s0)->birth.month)
  15. {
  16. if (od->birth.day>(s0)->birth.day)
  17. *od=*(s0);
  18. }
  19. }
  20. }
  21. return (od->name);
  22. }

【习题9.033】日期和链表结点的结构体类型定义如下:

  1. struct date{int year; int month; int day;}; //日期结构体类型
  2. struct studentNode //链表结点的结构体类型
  3. { char name[10]; //人名
  4. struct date birth; //出生日期
  5. struct studentNode *next;
  6. };

结构体链表L存储了n个人的名字和出生日期。写一函数,求这n个人中
年龄最大(即出生日期最小)者的名字。

  1. char *oldest(struct studentNode *L)
  2. /* 若L是空表,则返回空指针null
  3. 否则返回表中年龄最大者的名字
  4. */
  5. {
  6. studentNode *p,*od;
  7. p=L;od=L;
  8. while (p!=NULL)
  9. {
  10. if (od->birth.year>p->birth.year)
  11. {
  12. *od=*p;
  13. }
  14. else if (od->birth.year == p->birth.year)
  15. {
  16. if (od->birth.month>p->birth.month)
  17. *od=*p;
  18. else if (od->birth.month == p->birth.month)
  19. {
  20. if (od->birth.day>p->birth.day)
  21. *od=*p;
  22. }
  23. }
  24. p=p->next;
  25. }
  26. return (od->name);
  27. }
  1. #include <stdio.h>
  2. #include <string.h>
  3. struct date
  4. {
  5. int year;
  6. int month;
  7. int day;
  8. }; //定义日期结构体类型
  9. struct studentNode //链表结点的结构体类型
  10. { char name[10]; //人名
  11. struct date birth; //出生日期
  12. struct studentNode *next;
  13. };
  14. char *oldest(struct studentNode *L)
  15. /* 若L是空表,则返回空指针null
  16. 否则返回表中年龄最大者的名字
  17. */
  18. {
  19. studentNode *p,*od;
  20. p=L+1;od=L;
  21. while (p->next!=NULL)
  22. {
  23. if (od->birth.year>p->birth.year)
  24. {
  25. *od=*p;
  26. }
  27. else if (od->birth.year == p->birth.year)
  28. {
  29. if (od->birth.month>p->birth.month)
  30. *od=*p;
  31. else if (od->birth.month == p->birth.month)
  32. {
  33. if (od->birth.day>p->birth.day)
  34. *od=*p;
  35. }
  36. }
  37. p=p->next;
  38. }
  39. return (od->name);
  40. }
  41. //#define n 10
  42. int main()
  43. {
  44. int i;
  45. char o[20];
  46. char *old=o;
  47. struct studentNode a[3];
  48. for (i=0;i<3;i++)
  49. {
  50. printf("请输入同学%d的姓名\n",i+1);
  51. scanf("%s",&a[i].name);
  52. printf("请输入同学%d的生日\n",i+1);
  53. scanf("%d%d%d",&a[i].birth.year,&a[i].birth.month,&a[i].birth.day);
  54. a[i]
  55. }
  56. old=oldest(a,3);
  57. printf("%s\n",old);
  58. return 0;
  59. }

/******

【习题9.063】结构体类型定义如下:

struct course
{  int   cID;       //课程号,取值0~99
   char  name[10];  //课程名
   float credit;    //学分,取值0~5
   int   semester;  //学期,取值1~8
};

结构体数组c存储了n门课程的信息。写一函数,求学期s的总学分。
******/

  1. float creditSum(struct course c[], int n, int s)
  2. {
  3. int i=0;
  4. float sum=0;
  5. for (i=0;i<n;i++)
  6. {
  7. if (c[i].semester==s)
  8. sum+=c[i].credit;
  9. }
  10. return sum;
  11. }

/******

【习题9.073】课程链表结点的结构体类型定义如下:

  1. struct courseNode //课程链表结点的结构体类型
  2. { int cID; //课程号,取值0~99
  3. char name[10]; //课程名
  4. float credit; //学分,取值0~5
  5. int semester; //学期,取值1~8
  6. struct courseNode *next;
  7. };

结构体链表Lc存储了各学期多门课程的信息。写一函数,求学
期s的总学分。
******/

  1. float creditSum(struct courseNode *Lc, int s)
  2. /* 若Lc是空表,则返回0;
  3. 否则返回学期s的总学分
  4. */
  5. {
  6. struct courseNode *p;
  7. float sum=0.0;
  8. if (NULL==Lc)
  9. {
  10. return 0.0; //注意这里如果是0可能会报错(类型不匹配)
  11. }
  12. else
  13. {
  14. p=Lc;
  15. while (p!=NULL)
  16. {
  17. if (p->semester==s)
  18. sum+=p->credit;
  19. p=p->next;
  20. }
  21. }
  22. return sum;
  23. }

/******

【习题9.133】日期和结构体类型定义如下:

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
};
******/

  1. struct studentNode *CreateLinkList(struct student s[], int n)
  2. {
  3. struct studentNode* L,*p0;
  4. L = (studentNode*) malloc (sizeof(studentNode));
  5. L->next=NULL;
  6. struct student *x;
  7. if (NULL==s || n==0) return NULL;
  8. x=s;
  9. L->birth.year=x->birth.year;
  10. L->birth.month=x->birth.month;
  11. L->birth.day=x->birth.day;
  12. strcpy(L->name,x->name);
  13. x++;
  14. p0=L;
  15. for (;x<s+n;x++)
  16. {
  17. struct studentNode* p;
  18. p = (studentNode*) malloc (sizeof(studentNode));
  19. p->birth.year=x->birth.year;
  20. p->birth.month=x->birth.month;
  21. p->birth.day=x->birth.day;
  22. strcpy(p->name,x->name);
  23. p->next=p0->next;
  24. p0->next=p;
  25. p0=p0->next;
  26. }
  27. return L;
  28. }

/******

【习题9.173】课程链表结点的结构体类型定义如下:

struct courseNode   //课程链表结点的结构体类型
{  int   cID;       //课程号,取值0~99
   char  name[10];  //课程名
   float credit;    //学分,取值0~5
   int   semester;  //学期,取值1~8
   struct courseNode *next;
};

结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程的学分修改为t。
******/

  1. struct courseNode *creditChange(struct courseNode *Lc, int c, float t)
  2. /* 若课程c不存在,则修改不成功,返回null;
  3. 否则修改该课程的学分为t,返回指向该课程结点的指针。
  4. */
  5. {
  6. struct courseNode*p;
  7. p=Lc;
  8. while (p!=NULL)
  9. {
  10. if (p->cID==c)
  11. {
  12. p->credit=t;
  13. return p;
  14. }
  15. p=p->next;
  16. }
  17. return NULL;
  18. }

/******

【习题9.183】课程链表结点的结构体类型定义如下:

struct courseNode   //课程链表结点的结构体类型
{  int   cID;       //课程号,取值0~99
   char  name[10];  //课程名
   float credit;    //学分,取值0~5
   int   semester;  //学期,取值1~8
   struct courseNode *next;
};

结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程结点删除。
******/

  1. struct courseNode *deleteCourse(struct courseNode **Lc, int c)
  2. /* 若在链表Lc中课程c不存在,则删除不成功,返回null;
  3. 否则从链表Lc中删除该课程结点,并返回指向该课程结点的指针。
  4. */
  5. {
  6. struct courseNode*p;
  7. p=*Lc;
  8. while (p!=NULL)
  9. {
  10. if (p->next->cID==c)
  11. {
  12. struct courseNode*t=p->next;
  13. p->next=p->next->next;
  14. return t;
  15. }
  16. if (p->next==NULL)
  17. {
  18. if (p->cID==c)
  19. {
  20. Lc[0]=NULL;
  21. return p;
  22. }
  23. }
  24. p=p->next;
  25. }
  26. return NULL;
  27. }

/******

【习题9.302】单向链表的结点类型定义如下:

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;

}

/******

【习题9.352】单向链表的结点类型定义如下:

struct node{
  char  ch;
  struct node *next;
};

编写函数,对单向链表L实现排序,即按结点的ch值,
从小到大重构链表L,并返回排序后的链表的头指针。
******/

  1. struct node *sorting(struct node *L)
  2. /* 对单向链表L实现从小到大排序,
  3. 并返回重构后的链表的头指针。
  4. */
  5. {
  6. struct node *p=L,*tq;
  7. int n=0,i=0;
  8. if (NULL==L) return NULL;
  9. char t;
  10. while (p!=NULL)
  11. {
  12. p=Lp->next;
  13. n++;
  14. }
  15. p=L;
  16. for (t=L;t!=NULL;t;i<n-1;i++)
  17. {
  18. forp=L;
  19. while (p=L->next;p!=NULL;p++)
  20. {
  21. q=p->next;
  22. if (t(p->ch) > p(q->ch)
  23. )
  24. {
  25. char tmp=t t=p->ch;
  26. t p->ch=pq->ch;
  27. p q->ch=tmp;
  28. }
  29. p=p->next;
  30. t=p
  31. p=p->next;
  32. }
  33. }
  34. return L;
  35. }

答案1(ERROR 1):

  1. struct node *p=L,*q;
  2. int n=0,i=0;
  3. char t;
  4. while (p!=NULL)
  5. {
  6. p=p->next;
  7. n++;
  8. }
  9. p=L;
  10. for (;i<n-1;i++)
  11. {
  12. while (p!=NULL)
  13. {
  14. q=p-<next;
  15. if ((p->ch) > (q->ch))
  16. {
  17. t=p->ch;
  18. p->ch=q->ch;
  19. q->ch=t;
  20. }
  21. if (q->next!=NULL)
  22. p=p->next;
  23. else p=NULL;
  24. }
  25. }
  26. return L;

答案2(ERROR 2):

  1. char t;
  2. struct node*p=L,*q=L;
  3. while (L)
  4. {
  5. p=L-next;
  6. while (p)
  7. {
  8. if (L-ch > p->ch)
  9. {
  10. t=L->ch;
  11. L->ch=p->ch;
  12. p->ch=t;
  13. }
  14. }
  15. }

其他同学的答案:

  1. struct node *p=L,*q;
  2. int n=0,i=0;
  3. char t;
  4. while(p!=NULL)
  5. {
  6. p=p->next;
  7. ++n;
  8. }
  9. p=L;
  10. for(;i<n-1;i++)
  11. {
  12. while(p!=NULL)
  13. {
  14. q=p->next;
  15. if((p->ch)>(q->ch))
  16. {
  17. t=p->ch,p->ch=q->ch,q->ch=t;
  18. }
  19. if(q->next!=NULL)
  20. p=p->next;
  21. else p=NULL;
  22. }
  23. }
  24. p=L;
  25. 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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注