fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 5
  5.  
  6. int stack[SIZE];
  7. int top = -1; // เริ่มต้น stack ว่าง
  8.  
  9. void push(int value) {
  10. if (top >= SIZE - 1) {
  11. printf("❌ Stack Overflow: ไม่สามารถ push ค่าได้ เพราะ stack เต็ม\n");
  12. } else {
  13. top++;
  14. stack[top] = value;
  15. printf("✅ Push %d ลงใน stack\n", value);
  16. }
  17. }
  18.  
  19. void pop() {
  20. if (top < 0) {
  21. printf("❌ Stack Underflow: ไม่สามารถ pop ค่าได้ เพราะ stack ว่าง\n");
  22. } else {
  23. printf("✅ Pop ค่า %d ออกจาก stack\n", stack[top]);
  24. top--;
  25. }
  26. }
  27.  
  28. void display() {
  29. if (top < 0) {
  30. printf("📭 Stack ว่าง\n");
  31. } else {
  32. printf("📦 ค่าที่อยู่ใน stack: ");
  33. for (int i = 0; i <= top; i++) {
  34. printf("%d ", stack[i]);
  35. }
  36. printf("\n");
  37. }
  38. }
  39.  
  40. int main() {
  41. int choice, value;
  42.  
  43. while (1) {
  44. printf("\n========= เมนู Stack =========\n");
  45. printf("1. Push\n");
  46. printf("2. Pop\n");
  47. printf("3. แสดง stack\n");
  48. printf("4. ออกจากโปรแกรม\n");
  49. printf("เลือกเมนู (1-4): ");
  50. scanf("%d", &choice);
  51.  
  52. switch (choice) {
  53. case 1:
  54. printf("ป้อนค่าที่ต้องการ push: ");
  55. scanf("%d", &value);
  56. push(value);
  57. break;
  58. case 2:
  59. pop();
  60. break;
  61. case 3:
  62. display();
  63. break;
  64. case 4:
  65. printf("👋 ออกจากโปรแกรมแล้ว\n");
  66. exit(0);
  67. default:
  68. printf("⚠️ เลือกเมนูไม่ถูกต้อง!\n");
  69. }
  70. }
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0.02s 25124KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

int stack[SIZE];
int top = -1;  // เริ่มต้น stack ว่าง

void push(int value) {
    if (top >= SIZE - 1) {
        printf("❌ Stack Overflow: ไม่สามารถ push ค่าได้ เพราะ stack เต็ม\n");
    } else {
        top++;
        stack[top] = value;
        printf("✅ Push %d ลงใน stack\n", value);
    }
}

void pop() {
    if (top < 0) {
        printf("❌ Stack Underflow: ไม่สามารถ pop ค่าได้ เพราะ stack ว่าง\n");
    } else {
        printf("✅ Pop ค่า %d ออกจาก stack\n", stack[top]);
        top--;
    }
}

void display() {
    if (top < 0) {
        printf("📭 Stack ว่าง\n");
    } else {
        printf("📦 ค่าที่อยู่ใน stack: ");
        for (int i = 0; i <= top; i++) {
            printf("%d ", stack[i]);
        }
        printf("\n");
    }
}

int main() {
    int choice, value;

    while (1) {
        printf("\n========= เมนู Stack =========\n");
        printf("1. Push\n");
        printf("2. Pop\n");
        printf("3. แสดง stack\n");
        printf("4. ออกจากโปรแกรม\n");
        printf("เลือกเมนู (1-4): ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("ป้อนค่าที่ต้องการ push: ");
                scanf("%d", &value);
                push(value);
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                printf("👋 ออกจากโปรแกรมแล้ว\n");
                exit(0);
            default:
                printf("⚠️ เลือกเมนูไม่ถูกต้อง!\n");
        }
    }

    return 0;
}