Guest User

Untitled

a guest
Aug 20th, 2019
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Quack.c: an array-based implementation of a quack
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct node *Quack;
  6.  
  7. Quack createQuack(void); // create and return Quack
  8. Quack destroyQuack(Quack); // remove the Quack
  9. void push(int, Quack); // put int on the top of the quack
  10. void qush(int, Quack); // put int at the bottom of the quack
  11. int pop(Quack); // pop and return the top element on the quack
  12. int isEmptyQuack(Quack); // return 1 is Quack is empty, else 0
  13. void makeEmptyQuack(Quack);// remove all the elements on Quack
  14. void showQuack(Quack); // print the contents of Quack, from the top down
  15.  
  16. #define HEIGHT 1000
  17.  
  18. struct node {
  19. int array[HEIGHT];
  20. int top;
  21. };
  22.  
  23. Quack createQuack(void) {
  24. Quack qs;
  25. qs = malloc(sizeof(struct node));
  26. if (qs == NULL) {
  27. fprintf (stderr, "createQuack: no memory, aborting\n");
  28. exit(1); // should pass control back to the caller
  29. }
  30. qs->top = -1;
  31. return qs;
  32. }
  33.  
  34. void push(int data, Quack qs) {
  35. if (qs == NULL) {
  36. fprintf(stderr, "push: quack not initialised\n");
  37. }
  38. else {
  39. if (qs->top >= HEIGHT-1) {
  40. fprintf(stderr, "push: quack overflow\n");
  41. }
  42. else {
  43. ++qs->top;
  44. qs->array[qs->top] = data;
  45. }
  46. }
  47. return;
  48. }
  49.  
  50. void qush(int data, Quack que) { // adds data to the bottom of the array
  51. if (que == NULL) {
  52. fprintf(stderr, "qush: quack not initialised\n");
  53. }
  54. else {
  55. if (que->top >= HEIGHT-1) {
  56. fprintf(stderr, "qush: quack overflow\n");
  57. }
  58. else {
  59. ++que->top; // next available spot
  60. int i;
  61. for (i=que->top; i>=1; i--) {
  62. que->array[i] = que->array[i-1];// move each element up 1
  63. }
  64. que->array[0] = data;
  65. }
  66. }
  67. return;
  68. }
  69.  
  70.  
  71. int pop(Quack qs) { // return top element, or 0 if error
  72. int retval = 0;
  73. if (qs == NULL) {
  74. fprintf(stderr, "pop: quack not initialised\n");
  75. }
  76. else {
  77. if (isEmptyQuack(qs)) {
  78. fprintf(stderr, "pop: quack underflow\n");
  79. }
  80. else {
  81. retval = qs->array[qs->top]; // top element on stack
  82. --qs->top;
  83. }
  84. }
  85. return retval;
  86. }
  87.  
  88. void makeEmptyQuack(Quack qs) {
  89. if (qs == NULL) {
  90. fprintf(stderr, "makeEmptyQuack: quack not initialised\n");
  91. }
  92. else {
  93. while (!isEmptyQuack(qs)) {
  94. pop(qs);
  95. }
  96. }
  97. return;
  98. }
  99.  
  100. Quack destroyQuack(Quack qs) {
  101. if (qs == NULL) {
  102. fprintf(stderr, "destroyQuack: quack not initialised\n");
  103. }
  104. free(qs);
  105. return qs;
  106. }
  107.  
  108. int isEmptyQuack(Quack qs) {
  109. int empty = 0;
  110. if (qs == NULL) {
  111. fprintf(stderr, "isEmptyQuack: quack not initialised\n");
  112. }
  113. else {
  114. empty = qs->top < 0;
  115. }
  116. return empty;
  117. }
  118.  
  119. void showQuack(Quack qs) {
  120. if (qs == NULL) {
  121. fprintf(stderr, "showQuack: quack not initialised\n");
  122. }
  123. else {
  124. printf("Quack: ");
  125. if (qs->top < 0) {
  126. printf("<< >>\n");
  127. }
  128. else {
  129. int i;
  130. printf("<<"); // start with a <<
  131. for (i = qs->top; i > 0; --i) {
  132. printf("%d, ", qs->array[i]); // print each element
  133. }
  134. printf("%d>>\n", qs->array[0]); // last element includes a >>
  135. }
  136. }
  137. return;
  138. }
RAW Paste Data