fork download
  1. // Stack - Array based implementation.
  2. // Creating a stack of integers.
  3. #include<stdio.h>
  4.  
  5. #define MAX_SIZE 5
  6.  
  7. int A[MAX_SIZE]; // integer array to store the stack
  8. int top = -1; // variable to mark top of stack in array
  9.  
  10. // Push operation to insert an element on top of stack.
  11. void Push(int x)
  12. {
  13. if(top == MAX_SIZE-1) { // overflow case.
  14. printf(" stack overflow\n");
  15. return;
  16. }
  17. A[++top] = x;
  18. }
  19.  
  20. // Pop operation to remove an element from top of stack.
  21. void Pop()
  22. {
  23. if(top == -1) { // If stack is empty, pop should throw error.
  24. printf("Error: No element to pop\n");
  25. return;
  26. }
  27. top--;
  28. }
  29.  
  30.  
  31. // This will print all the elements in the stack at any stage.
  32. void display() {
  33. int i;
  34. printf("Stack: ");
  35. for(i = 0;i<=top;i++)
  36. printf("%d ",A[i]);
  37. printf("\n");
  38. }
  39.  
  40. int main() {
  41. // Code to test the implementation.
  42. // calling display() after each push or pop to see the state of stack.
  43. Push(10);
  44. push(20);
  45. push(30);
  46. push(40);
  47. push(50);
  48. pop();
  49. display();
  50. }
  51.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:44:1: warning: implicit declaration of function 'push' [-Wimplicit-function-declaration]
 push(20);
 ^
prog.c:48:1: warning: implicit declaration of function 'pop' [-Wimplicit-function-declaration]
 pop();
 ^
prog.c:50:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
/home/cX4FWD/ccxLK83f.o: In function `main':
prog.c:(.text.startup+0x20): undefined reference to `push'
prog.c:(.text.startup+0x2c): undefined reference to `push'
prog.c:(.text.startup+0x38): undefined reference to `push'
prog.c:(.text.startup+0x44): undefined reference to `push'
prog.c:(.text.startup+0x49): undefined reference to `pop'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty