Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include<string.h>
  4.  
  5. int position1=0;
  6. int position2=0;
  7.  
  8. int length=1;
  9.  
  10. typedef struct Node
  11. {
  12. int record;
  13. int CardNum;
  14. char CustomerType[20];
  15. struct Node* next;
  16.  
  17. }Node;
  18.  
  19. typedef struct queue
  20. {
  21. Node* front;
  22. Node* rear;
  23. }Queue;
  24.  
  25. Queue q1,q2;
  26.  
  27.  
  28. void Enqueue(Queue *q, int x, char *y, int position);
  29. void Dequeue(Queue *q);
  30. void List(Queue *q);
  31.  
  32. int main()
  33. {
  34. q1.front=NULL;
  35. q2.front=NULL;
  36. q1.rear=NULL;
  37. q2.rear=NULL;
  38.  
  39. char command[10];
  40. int card;
  41. char client[10],*ptrclient;
  42.  
  43. while(1)
  44. {
  45. scanf("%s",command);
  46.  
  47. if(strcmp(command,"IN") == 0)
  48. {
  49. printf("IN:");
  50.  
  51. scanf("%d",&card);
  52. scanf("%s",client);
  53. ptrclient=&client[0];
  54.  
  55. if(strcmp(client,"VIP")==0)
  56. {
  57. //printf("Entered if VIP");
  58. Enqueue(&q1,card,client,position1);
  59. position1++;
  60. }
  61. else if(strcmp(client,"Ordinary")==0)
  62. {
  63. //printf("Entered if Ordinary");
  64. Enqueue(&q2,card,client,position2);
  65. position2++;
  66. }
  67. }
  68.  
  69. else if(strcmp(command,"OUT") == 0)
  70. {
  71. if(q1.front == NULL && q1.rear == NULL && q2.front == NULL && q2.rear == NULL)
  72. {
  73. printf("FAILED:n");
  74. }
  75.  
  76. else if(strcmp(q1.front->CustomerType,"VIP")==0)
  77. {
  78. Dequeue(&q1);
  79. position1--;
  80. }
  81. else if(strcmp(q2.front->CustomerType,"Ordinary")==0)
  82. {
  83. Dequeue(&q2);
  84. position2--;
  85. }
  86. }
  87. else if(strcmp(command,"LIST") == 0)
  88. {
  89. printf("LIST:n");
  90. List(&q1);
  91. List(&q2);
  92. }
  93.  
  94. else if(strcmp(command,"QUIT") ==0)
  95. {
  96. printf("GOOD BYE!n");
  97. break;
  98. }
  99. }
  100. return 0;
  101. }
  102.  
  103. void Enqueue(Queue *q, int x, char *y, int position)
  104. {
  105. Node* temp = (Node*)malloc(sizeof(Node));
  106.  
  107. temp->CardNum=x;
  108. strcpy(temp->CustomerType,y);
  109. temp->record=length;
  110. temp->next=NULL;
  111.  
  112. if(q->front == NULL && q->rear == NULL)
  113. {
  114. q->front=q->rear=temp;
  115. }
  116. else
  117. {
  118. q->rear->next=temp;
  119. q->rear=temp;
  120. }
  121.  
  122. printf("%d %d %s %dn",temp->record,temp->CardNum,temp->CustomerType,position);
  123.  
  124. length++;
  125. }
  126. void Dequeue(Queue *q)
  127. {
  128. Node* temp;
  129. temp=q->front;
  130.  
  131. if(q->front == q->rear)
  132. {
  133. q->front = q->rear = NULL;
  134. printf("OUT:%d %d %sn",temp->record,temp->CardNum,temp->CustomerType);
  135. }
  136. else
  137. {
  138. q->front = q->front->next;
  139. printf("OUT:%d %d %sn",temp->record,temp->CardNum,temp->CustomerType);
  140.  
  141. }
  142. free(temp);
  143.  
  144. }
  145. void List(Queue *q)
  146. {
  147. Node *temp;
  148.  
  149. if(q->front != NULL)
  150. {
  151. temp = q->front;
  152. while(temp != NULL)
  153. {
  154. printf("%d %d %sn",temp->record,temp->CardNum,temp->CustomerType);
  155. temp = temp->next;
  156. }
  157. }
  158. }
Add Comment
Please, Sign In to add comment