• 看病排队问题

    看病排队问题

    1. #include <stdio.h>
    2. #include <malloc.h>
    3. #include <string.h>
    4. typedef struct QNode
    5. {
    6. char data[10];
    7. struct QNode *next;
    8. } QType; /*链队结点类型*/
    9. typedef struct
    10. {
    11. QType *front,*rear;
    12. } LinkQueue; /*链队类型*/
    13. void SeeDoctor()
    14. {
    15. int sel,flag=1;
    16. LinkQueue *lq;
    17. QType *s;
    18. char name[10];
    19. lq=(LinkQueue *)malloc(sizeof(LinkQueue));
    20. lq->front=(QType *)malloc(sizeof(QType));
    21. lq->front->next=NULL;
    22. lq->rear=lq->front;
    23. while (flag==1) /*未下班时循环执行*/
    24. {
    25. printf("1:排队 2:看医生 3:查看排队 0:下班 请选择:");
    26. scanf("%d",&sel);
    27. switch(sel)
    28. {
    29. case 0:
    30. if (lq->front!=lq->rear) /*队不空*/
    31. printf(" >>请排队的患者明天就医\n");
    32. flag=0;
    33. break;
    34. case 1:
    35. printf(" >>输入患者姓名:");scanf("%s",name);
    36. s=(QType *)malloc(sizeof(QType));
    37. strcpy(s->data,name);s->next=NULL;
    38. lq->rear->next=s;lq->rear=s;
    39. break;
    40. case 2:
    41. if (lq->front==lq->rear) /*队空*/
    42. printf(" >>没有排队的患者\n");
    43. else
    44. {
    45. s=lq->front->next;
    46. if (lq->rear==s)
    47. lq->rear=lq->front;
    48. printf(" >>患者%s看医生\n",s->data);
    49. lq->front->next=s->next;
    50. free(s);
    51. }
    52. break;
    53. case 3:
    54. if (lq->front==lq->rear) /*队空*/
    55. printf(" >>没有排列的患者\n");
    56. else
    57. {
    58. s=lq->front->next;
    59. printf(" >>排队患者:");
    60. while (s!=NULL)
    61. {
    62. printf("%s ",s->data);
    63. s=s->next;
    64. }
    65. printf("\n");
    66. }
    67. break;
    68. }
    69. }
    70. }
    71. void main()
    72. {
    73. SeeDoctor();
    74. }