Advertisement
Guest User

stack.c

a guest
Nov 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include "stack.h"
  2. #include <inttypes.h>
  3.  
  4. Stack theStack;
  5.  
  6. StackError_t init()
  7. {
  8. if (STACK_SIZE == 0)
  9. return StackError_t::STACK_INIT_ERROR;
  10.  
  11. for( size_t i = 0; i < STACK_SIZE; ++i )
  12. theStack.data[ i ] = 0;
  13.  
  14. theStack.length = 0;
  15.  
  16. return StackError_t::STACK_NO_ERROR;
  17. }
  18.  
  19. StackError_t push(uint8_t value)
  20. {
  21. if (theStack.length < STACK_SIZE)
  22. {
  23. theStack.data[STACK_SIZE - ++theStack.length] = value;
  24. return StackError_t::STACK_NO_ERROR;
  25. }
  26. else
  27. return StackError_t::STACK_OVERFLOW_ERROR;
  28. }
  29.  
  30. StackError_t pop( uint8_t* value )
  31. {
  32. if (theStack.length > 0)
  33. {
  34. *value = theStack.data[STACK_SIZE - theStack.length--];
  35. return StackError_t::STACK_NO_ERROR;
  36. }
  37. else
  38. return StackError_t::STACK_UNDERFLOW_ERROR;
  39. }
  40.  
  41. StackError_t peek(const uint8_t* index)
  42. {
  43. for (size_t i = 0; i < theStack.length; ++i)
  44. if (theStack.data[i] == *index)
  45. return StackError_t::STACK_NO_ERROR;
  46.  
  47. return StackError_t::STACK_VALUE_NOT_FOUND;
  48. }
  49.  
  50. bool isEmpty()
  51. {
  52. if (theStack.length == 0)
  53. return true;
  54.  
  55. return false;
  56. }
  57.  
  58. bool isFull()
  59. {
  60. if (theStack.length == STACK_SIZE)
  61. return true;
  62.  
  63. return false;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement