Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. //Declaration of struct "stack"
  2. //INV: FILO - "First in - last out"
  3. typedef struct {
  4. int* arrayOfIntegers;
  5. int size;
  6. } stack;
  7.  
  8. //Creating instance of struct "stack"
  9. stack* createStack(size_t size) {
  10. stack *newStack = calloc(1, sizeof(stack));
  11. newStack -> arrayOfIntegers = (int*)calloc(size, sizeof(int));
  12. newStack -> size = 0;
  13. return newStack;
  14. }
  15.  
  16. void ensureCapacityOfStack(stack* stack) {
  17. int* newArray = (int*)calloc(2 * (size_t)stack -> size, sizeof(int));
  18. memcpy(newArray, stack -> arrayOfIntegers, stack -> size);
  19. stack -> arrayOfIntegers = newArray;
  20. }
  21.  
  22. void ensureCapacity(char* expression, size_t size) {
  23. char* newLine = (char*)calloc(2 * size + 1, sizeof(char));
  24. memcpy(newLine, expression, size);
  25. expression = newLine;
  26. }
  27.  
  28. //Add the element to stack Time: O(1)
  29. void push(stack* stack, int value) {
  30. stack -> arrayOfIntegers[stack -> size++] = value;
  31. if (stack -> size % 1000 == 0) {
  32. ensureCapacityOfStack(stack);
  33. }
  34. }
  35.  
  36. //Returns the element on the peek of stack Time: O(1)
  37. int peek(stack* stack) {
  38. return stack -> arrayOfIntegers[stack -> size - 1];
  39. }
  40.  
  41. //Delete the peek element from stack Time: O(1)
  42. void pop(stack* stack) {
  43. if (stack -> size == 0) {
  44. printf("bad input");
  45. exit(1);
  46. }
  47. stack -> arrayOfIntegers[stack -> size--] = 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement