use_q.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* use_q.c -- driver testing the Queue interface */
  2. /* compile with queue.c */
  3. #include <stdio.h>
  4. #include "queue.h" /* defines Queue, Item */
  5. int main(void)
  6. {
  7. Queue line;
  8. Item temp;
  9. char ch;
  10. InitializeQueue(&line);
  11. puts("Testing the Queue interface. Type a to add a value,");
  12. puts("type d to delete a value, and type q to quit.");
  13. while ((ch = getchar()) != 'q')
  14. {
  15. if (ch != 'a' && ch != 'd') /* ignore other input */
  16. continue;
  17. if ( ch == 'a')
  18. {
  19. printf("Integer to add: ");
  20. scanf("%d", &temp);
  21. if (!QueueIsFull(&line))
  22. {
  23. printf("Putting %d into queue\n", temp);
  24. EnQueue(temp,&line);
  25. }
  26. else
  27. puts("Queue is full!");
  28. }
  29. else
  30. {
  31. if (QueueIsEmpty(&line))
  32. puts("Nothing to delete!");
  33. else
  34. {
  35. DeQueue(&temp,&line);
  36. printf("Removing %d from queue\n", temp);
  37. }
  38. }
  39. printf("%d items in queue\n", QueueItemCount(&line));
  40. puts("Type a to add, d to delete, q to quit:");
  41. }
  42. EmptyTheQueue(&line);
  43. puts("Bye!");
  44. return 0;
  45. }