Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 10
  3.  
  4. int stack[N];
  5. int current = -1;
  6.  
  7. int shift() {
  8. if(current < 0)
  9. {
  10. return -1;
  11. }
  12. else
  13. {
  14. int tmp = stack[0];
  15. for(int i = 0; i < current; i++)
  16. {
  17. stack[i] = stack[i+1];
  18. }
  19. stack[current] = 0;
  20. current--;
  21. return tmp;
  22. }
  23. }
  24.  
  25. void push(int value) {
  26. if(current >= 9)
  27. {
  28. printf("Buffer overflow\n");
  29. return;
  30. }
  31. else
  32. {
  33. current++;
  34. stack[current] = value;
  35. }
  36. }
  37.  
  38. void list() {
  39. for(int i = 0; i<=current; i++)
  40. {
  41. printf("%d, ", stack[i]);
  42. }
  43. printf("\n");
  44. }
  45.  
  46. int main() {
  47. printf("shift(); %d\n", shift());
  48. for (int i = 0; i < 11; i++) {
  49. push(i);
  50. }
  51. list();
  52. printf("shift(); %d\n", shift());
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement