Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 KB | None | 0 0
  1. #include "Stack.h"
  2. #include "stdlib.h"
  3.  
  4. //Linked List based data structure.
  5. typedef struct Stack {
  6. int data;
  7. int size;
  8. struct Stack *next;
  9. }Stack;
  10.  
  11. /**
  12. * initStackWithData initializes a new Stack by allocating a new pointer with
  13. * the ammount of memory a Stack needs. Then after checking if the newly
  14. * created stack is NULL or not its values are initialized by using the value
  15. * from the parameters and setting the next Stack it points to to NULL.
  16. * This initial Stack will serve as the bottom of the stack so it will never
  17. * point to another Stack with its next property.
  18. *
  19. * @param val - Value to place in the Stack structures data property.
  20. *
  21. * @returns pointer to the just initialized Stack structure(object).
  22. */
  23. Stack* initStackWithData(int val) {
  24. Stack *newStack = (Stack *) malloc(sizeof(Stack));
  25.  
  26. if(newStack == NULL) {
  27. return NULL;
  28. }
  29.  
  30. newStack->data = val;
  31. newStack->size = 1;
  32. newStack->next = NULL;
  33.  
  34. return newStack;
  35. }
  36.  
  37. /**
  38. * push is pushing a new value to the top of the stack. Since the Stack
  39. * structure is a LIFO(Last In First Out) the value pushed is stored into a new
  40. * Stack structure and the old head Stack (Top) is getting replace with the new
  41. * Stack created. That makes it so that the new Stack is the new top pointing
  42. * to the old top.
  43. *
  44. * @param **head - Pointer to the pointer of head, we use pointer to pointers to
  45. * make it easier by letting us change the entire head Stack from this function.
  46. * @param val - Value to place in the Stack structures data property.
  47. */
  48. void push(Stack **head, int val) {
  49. Stack *newStack = (Stack *) malloc(sizeof(Stack));
  50.  
  51. newStack->data = val;
  52. newStack->next = *head;
  53. if (!isEmpty(*head)) {
  54. newStack->size = newStack->next->size + 1;
  55. } else {
  56. newStack->size = 1;
  57. }
  58. *head = newStack;
  59. }
  60.  
  61. /**
  62. * pop is removing the first element of the Stack. Since all Stacks are linked
  63. * through the next property pointing to the next Stack in the linked list it
  64. * can remove the current top and then make the new top become the next Stack
  65. * of the linked list.
  66. *
  67. * @param **head - Pointer to the pointer of head, we use pointer to pointers to
  68. * make it easier by letting us change the entire head Stack from this function.
  69. * @param val - Value to place in the Stack structures data property.
  70. *
  71. * @returns The data value from the removed Stack. Returns -1 if the Stack is
  72. * empty.
  73. */
  74. int pop(Stack **head){
  75. if (isEmpty(*head)) {
  76. return -1;
  77. }
  78.  
  79. Stack *newStack = (*head)->next;
  80. int retval = (*head)->data;
  81. free(*head);
  82. *head = newStack;
  83.  
  84. return retval;
  85. }
  86.  
  87. /**
  88. * peek is a function that lets the caller view the value of the element at the
  89. * top of the stack without removing it.
  90. *
  91. * @param **head - Pointer to the pointer of head, we use pointer to pointers to
  92. * make it easier by letting us change the entire head Stack from this function.
  93. *
  94. * @returns The data value found on the top of the Stack. Returns -1 if the
  95. * Stack is empty.
  96. */
  97. int peek(Stack **head) {
  98. if (!isEmpty(*head)) {
  99. return (*head)->data;
  100. }
  101. return -1;
  102. }
  103.  
  104. /**
  105. * deleteStack is a function that lets the caller delete their entire Stack
  106. * structure by calling this function.
  107. *
  108. * @param **head - Pointer to the pointer of head, we use pointer to pointers to
  109. * make it easier by letting us change the entire head Stack from this function.
  110. */
  111. void deleteStack(Stack **head) {
  112. Stack *newStack;
  113. while (!isEmpty(*head)) {
  114. newStack = (*head)->next;
  115. free(*head);
  116. *head = newStack;
  117. }
  118. free(*head);
  119. }
  120.  
  121. /**
  122. * isEmpty checks if the Stack passed into the parameters is empty.
  123. *
  124. * @param *head - The Stack to check if empty or not.
  125. *
  126. * @returns 1 if the Stack is empty. Returns 0 if the Stack is not empty.
  127. */
  128. int isEmpty(Stack *head) {
  129.  
  130. if (head == NULL) {
  131. return 1;
  132. }
  133.  
  134. return 0;
  135. }
  136.  
  137. /**
  138. * display loops through the entire Stacks linked list priting out every
  139. * data property found while looping through the entire list of linked Stacks.
  140. *
  141. * @param *head - The target Stack to loop through its linked elements.
  142. */
  143. void display(Stack *head) {
  144. Stack *current = head;
  145.  
  146. while (current != NULL) {
  147. printf("Stack Data: %i, Stack Size: %in", current->data, current->size);
  148. current = current->next;
  149. }
  150. }
  151.  
  152. #include "ArrayStack.h"
  153. #include "stdlib.h"
  154. #define STACK_MAX 30
  155.  
  156. //Array based stack data structure.
  157. struct ArrayStack {
  158. int top;
  159. int data[STACK_MAX];
  160. };
  161.  
  162. /**
  163. * initWithData initializes a new ArrayStruct by allocating a new pointer with
  164. * the ammount of memory a Stack needs. Then after checking if the newly
  165. * created stack is NULL or not its values are initialized by using the value
  166. * from the parameters and setting the next Stack it points to to NULL.
  167. *
  168. * @returns pointer to the just initialized ArrayStruct.
  169. */
  170. struct ArrayStack* initArrayStruct() {
  171. struct ArrayStack *newStack =
  172. (struct ArrayStack*) malloc(sizeof(struct ArrayStack));
  173.  
  174. if(newStack == NULL) {
  175. return NULL;
  176. }
  177.  
  178. newStack->top = -1;
  179. return newStack;
  180. }
  181.  
  182. /**
  183. * pushToArrayStruct pushes a new integer value to the ArrayStruct provided in
  184. * the parameters. It first checks that the stack is not overflowing the array
  185. * size and if not proceeding with incrementing the top property and placing
  186. * a new value into the data property at the current top.
  187. *
  188. * @param *stack - The target stack to push a value to.
  189. * @param val - The target value to push into the array of the stack.
  190. */
  191. void pushToArrayStruct(struct ArrayStack *stack, int val) {
  192. if (stack->top >= STACK_MAX-1) {
  193. return;
  194. }
  195. stack->top = stack->top + 1;
  196. stack->data[stack->top] = val;
  197. }
  198.  
  199. /**
  200. * pushToArrayStructEnd pushes a new integer value to the ArrayStruct provided
  201. * in the parameters. It first checks that the stack is not overflowing
  202. * the array size and if not proceeding with looping through the stacks entire
  203. * data array moving each element up one position in the stack and then places
  204. * the new value at the bottom of the stack at position 0.
  205. *
  206. * @param *stack - The target stack to push a value to.
  207. * @param val - The target value to push into the array of the stack.
  208. */
  209. void pushToArrayStructEnd(struct ArrayStack *stack, int val) {
  210. if (stack->top >= STACK_MAX-1) {
  211. return;
  212. }
  213.  
  214. int x;
  215. for (x = stack->top; x >= 0; x--) {
  216. stack->data[x+1] = stack->data[x];
  217. }
  218. stack->top = stack->top + 1;
  219. stack->data[0] = val;
  220. }
  221.  
  222. /**
  223. * popFromArrayStruct removes the value at the top position of the stacks array
  224. * and reuturns it.
  225. *
  226. * @param *stack - The target stack to pop the top values from.
  227. *
  228. * @returns the value on the top of the stack. Returns -1 if the stack is empty.
  229. */
  230. int popFromArrayStruct(struct ArrayStack *stack) {
  231. if (stack->top < 0) {
  232. return -1;
  233. }
  234. return stack->data[stack->top--];
  235. }
  236.  
  237. /**
  238. * peekAtArrayStrut returns the top value of the array based stack
  239. * without removing it from the stack.
  240. *
  241. * @param *stack - Pointer to the target stack to peek at.
  242. * @returns the value located at the top of the stack.
  243. */
  244. int peekAtArrayStruct(struct ArrayStack *stack) {
  245. return stack->data[stack->top];
  246. }
  247.  
  248. /**
  249. * isEmptyArrayStruct checks if the array based stack is empty or not
  250. * and returns a true of false value depending on if it is or not.
  251. *
  252. * @param *stack - Pointer to the target stack to check if empty.
  253. * @return a true or false value depending on if the target stack is empty
  254. * or not.
  255. */
  256. int isEmptyArrayStruct(struct ArrayStack *stack) {
  257. if (stack->top >= 1) {
  258. return 1;
  259. }
  260. return 0;
  261. }
  262.  
  263. /**
  264. * displayArrayStruct loops through the entire array based stack
  265. * printing out all the values within the stack.
  266. *
  267. * @param *stack - Pointer to the target stack to display values from.
  268. */
  269. void displayArrayStruct(struct ArrayStack *stack) {
  270. for (int x = 0; x <= stack->top; x++) {
  271. printf("Stack Data: %in", stack->data[x]);
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement