Advertisement
svederni

stack.c with less errors

Oct 19th, 2016
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. /*Samuel Vedernikoff
  2. A12906756
  3. CS12xeq
  4. Assignment 3*/
  5.  
  6. #include <malloc.h>
  7. #include <stdio.h>
  8. #include "mylib.h"
  9. #include "stack.h"
  10.  
  11. #define STACK_POINTER_INDEX (-1) /* Index of last used space */
  12. #define STACK_SIZE_INDEX (-2) /* Index of size of the stack */
  13. #define STACK_COUNT_INDEX (-3) /* Index of which stack allocated */
  14. #define STACK_OFFSET 3 /* offset from allocation to where user info begins */
  15.  
  16. /* catastrophic error messages */
  17. static const char DELETE_NONEXIST[] = "Deleting a non-existent stack!!!\n";
  18. static const char EMPTY_NONEXIST[] = "Emptying a non-existent stack!!!\n";
  19. static const char INCOMING_NONEXIST[] =
  20. "Incoming parameter does not exist!!!\n";
  21. static const char ISEMPTY_NONEXIST[] =
  22. "Isempty check from a non-existent stack!!!\n";
  23. static const char ISFULL_NONEXIST[] =
  24. "Isfull check from a non-existent stack!!!\n";
  25. static const char NUM_NONEXIST[] =
  26. "Num_elements check from a non-existent stack!!!\n";
  27. static const char POP_NONEXIST[] = "Popping from a non-existent stack!!!\n";
  28. static const char POP_EMPTY[] = "Popping from an empty stack!!!\n";
  29. static const char PUSH_NONEXIST[] = "Pushing to a non-existent stack!!!\n";
  30. static const char PUSH_FULL[] = "Pushing to a full stack!!!\n";
  31. static const char TOP_NONEXIST[] = "Topping from a non-existent stack!!!\n";
  32. static const char TOP_EMPTY[] = "Topping from an empty stack!!!\n";
  33. static const char WRITE_NONEXIST_FILE[] =
  34. "Attempt to write using non-existent file pointer!!!\n";
  35. static const char WRITE_NONEXIST_STACK[] =
  36. "Attempt to write to a non-existent stack!!!\n";
  37.  
  38. /* Debug messages. HEX messages used for negative numbers on the stack. */
  39. static const char ALLOCATED[] = "[Stack %ld has been allocated]\n";
  40. static const char DEALLOCATE[] = "[Stack %ld has been deallocated]\n";
  41. static const char HEXPOP[] = "[Stack %ld - Popping 0x%lx]\n";
  42. static const char HEXPUSH[] = "[Stack %ld - Pushing 0x%lx]\n";
  43. static const char HEXTOP[] = "[Stack %ld - Topping 0x%lx]\n";
  44. static const char POP[] = "[Stack %ld - Popping %ld]\n";
  45. static const char PUSH[] = "[Stack %ld - Pushing %ld]\n";
  46. static const char TOP[] = "[Stack %ld - Topping %ld]\n";
  47.  
  48. /* static variable allocation */
  49. static int debug = FALSE; /* allocation of debug flag */
  50. static int stack_counter = 0; /* number of stacks allocated so far */
  51.  
  52. /* Debug state methods */
  53. void debug_off (void) {
  54. debug = FALSE;
  55. }
  56.  
  57. void debug_on (void) {
  58. debug = TRUE;
  59. }
  60.  
  61. /* function deallocates all the memory in the
  62. Stack and sets the stack pointer to NULL*/
  63. void delete_Stack (Stack ** spp) {
  64. /*assigns the pointer to the memory
  65. * to null thus deleting the stack*/
  66. *spp = NULL;
  67. /*deallocates the memory originally allocated to Stack*/
  68. free(*spp);
  69.  
  70. }
  71.  
  72. /* functions empties the stack of all its elements and sets the stack pointer to 0;
  73. * checks whether there are stacks to manipulate*/
  74. void empty_Stack (Stack * this_Stack) {
  75. if (this_Stack[-3] > 0)
  76. {
  77. this_Stack = (Stack *)-1;
  78. }
  79. }
  80.  
  81. /*function checks whether the stack is empty*/
  82. long isempty_Stack (Stack * this_Stack) {
  83. if (this_Stack[-1] == -1 )
  84. {
  85. return 1;
  86. }
  87. else {
  88. return 0;
  89. }
  90. }
  91.  
  92. /* function checks whether stack is full
  93. * makes sure that the stack_ counter is greater than 0
  94. * makes sure that there are elements in the stack*/
  95. long isfull_Stack (Stack * this_Stack) { //Step 2
  96. if (this_Stack[-3] > 0 && this_Stack[-2] >= 0)
  97. {
  98. if ( (this_Stack[-2] -1) != 0)
  99. {
  100. return 1;
  101. }
  102. else {
  103. return 0;
  104. }
  105. }
  106. else
  107. {
  108. printf ("%s\n", ISFULL_NONEXIST);
  109. }
  110. }
  111. /* function: allocates memory and intializes the a new Stack Object.
  112. * allocates memory to input number of longs, initializes the stack
  113. * infrastructure,
  114. * and returns a pointer to the first storage space in the stack.*/
  115. Stack * new_Stack (unsigned long stacksize) {
  116. Stack *memory = malloc((stacksize + STACK_OFFSET) * sizeof(long));
  117. Stack * this_Stack = (Stack*)memory + 3;
  118. /* Counter of stacks*/
  119. this_Stack[-3] = stack_counter;
  120. /*size of the stack*/
  121. this_Stack[-2] = stacksize;
  122. /*last used space*/
  123. this_Stack[-1] = 0;
  124. stack_counter++;
  125. return this_Stack;
  126. }
  127.  
  128. /*function returns the size of the stack
  129. checks whether the stack counter is greater than 0*/
  130. long num_elements (Stack * this_Stack) { //Step 4
  131. if (this_Stack[-3] > 0)
  132. {
  133. return this_Stack[-2];
  134. }
  135. else {
  136. return 0;
  137. }
  138. }
  139.  
  140. /* function takes the last element entered into the stack and removes it.
  141. * Then it assignes the last used space to the previous element. */
  142. long pop (Stack * this_Stack, long * item) {
  143. item = (long *)this_Stack[this_Stack[-1]];
  144. this_Stack[-1] = this_Stack[-1] - 1;
  145. }
  146.  
  147. /* function takes a long passed into the parameter
  148. * and adds that value to the top of the stack
  149. * the function first checks if the stack is full
  150. * and whether there are stacks to manipulate*/
  151. long push (Stack * this_Stack, long item) {
  152. if (!isfull_Stack(this_Stack) && this_Stack[-3] > 0)
  153. {
  154. this_Stack[-1] = this_Stack[-1] + 1;
  155. this_Stack[this_Stack[-1]] = item;
  156. return 1;
  157. }
  158. else{
  159. return 0;
  160. }
  161. }
  162.  
  163.  
  164. /* function takes a stack pointer and a pointer to a long
  165. *it will store the last element in the stack to the long pointer*/
  166. long top (Stack * this_Stack, long * item) {
  167. if (this_Stack[-3] > 0 && this_Stack[-2] >= 0)
  168. {
  169. item[0] = this_Stack[-1];
  170. }
  171. }
  172.  
  173. FILE * write_Stack (Stack * this_Stack, FILE * stream) {
  174. /* index into the stack */
  175. long index = 0;
  176.  
  177. if (this_Stack == NULL) {
  178. fprintf (stderr, WRITE_NONEXIST_STACK);
  179. return stream;
  180. }
  181.  
  182. if (stream == NULL) {
  183. fprintf (stderr, WRITE_NONEXIST_FILE);
  184. return stream;
  185. }
  186.  
  187. if (stream == stderr)
  188. fprintf (stream, "Stack has %ld items in it.\n",
  189. num_elements (this_Stack));
  190.  
  191. for (index = STACK_COUNT_INDEX + STACK_OFFSET;
  192. index < num_elements (this_Stack); index++) {
  193.  
  194. if (stream == stderr)
  195. fprintf (stream, "Value on stack is |0x%lx|\n",
  196. this_Stack[index]);
  197. else {
  198. if (this_Stack[index] < 0)
  199. fprintf (stream, "%c ",
  200. (char) this_Stack[index]);
  201. else
  202. fprintf (stream, "%ld ", this_Stack[index]);
  203. }
  204. }
  205.  
  206. return stream;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement