Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ ใช้ Array สร้าง Stack (สแต็ก)

Stack (สแต็ก) แบบ Array ใช้ LIFO (Last In, First Out) เก็บข้อมูลในอาร์เรย์ขนาดคงที่

โครงสร้างข้อมูล

const int MAX_SIZE = 100;
int stack[MAX_SIZE];
int top = -1;  // -1 หมายถึงสแต็กว่าง

โค้ดโปรแกรม Stack ด้วย Array

#include <iostream>
using namespace std;

const int MAX_SIZE = 100;
int stack[MAX_SIZE];
int top = -1;

void push(int val) {
    if (top >= MAX_SIZE - 1) {
        cout << "Stack Overflow!" << endl;
        return;
    }
    stack[++top] = val;  // เพิ่ม top แล้วเก็บค่า
    cout << val << " pushed." << endl;
}

void pop() {
    if (top < 0) {
        cout << "Stack Underflow!" << endl;
        return;
    }
    cout << "Popped: " << stack[top--] << endl;  // แสดงค่า แล้วลด top
}

void display() {
    if (top < 0) {
        cout << "Stack is empty" << endl;
        return;
    }
    
    cout << "Stack (top to bottom): ";
    for (int i = top; i >= 0; i--)
        cout << stack[i] << " ";
    cout << endl;
}

int main() {
    int choice, val;
    
    do {
        cout << "\n1. Push\n2. Pop\n3. Display\n4. Exit\nChoice: ";
        cin >> choice;
        
        switch (choice) {
            case 1:
                cout << "Enter value: ";
                cin >> val;
                push(val);
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                cout << "Exiting..." << endl;
                break;
            default:
                cout << "Invalid choice!" << endl;
        }
    } while (choice != 4);
    
    return 0;
}

ผลลัพธ์ตัวอย่าง

1. Push
2. Pop
3. Display
4. Exit
Choice: 1
Enter value: 10
10 pushed.
Choice: 1
Enter value: 20
20 pushed.
Choice: 3
Stack (top to bottom): 20 10
Choice: 2
Popped: 20
Choice: 3
Stack (top to bottom): 10
Choice: 4
Exiting...

การทำงานของ Push และ Pop แบบ Array

Push

top = -1 (ว่าง)
push(10): top=0, stack[0]=10
push(20): top=1, stack[1]=20
Stack: [10][20]  ← top=1

Pop

Stack: [10][20]  ← top=1
pop(): return stack[1]=20, top=0
Stack: [10]      ← top=0
pop(): return stack[0]=10, top=-1

ข้อจำกัดของ Array Stack

ปัญหา คำอธิบาย
Fixed Sizeต้องกำหนด MAX_SIZE ล่วงหน้า
OverflowPush ไม่ได้เมื่อ top = MAX_SIZE-1
Memory Wasteจองพื้นที่มากกว่าที่ใช้จริง

ข้อดีของ Array Stack

  • Speed: เร็วกว่า Linked List (Cache locality ดี)
  • Simple: เขียนง่าย เข้าใจง่าย
  • No Memory Fragmentation: หน่วยความจำติดกัน
  • Random Access: เข้าถึง index ใดก็ได้ O(1)

เมื่อไหร่ควรใช้แบบไหน?

  • Array Stack: รู้ขนาดสูงสุดล่วงหน้า ต้องการความเร็วสูงสุด
  • Linked List Stack: ไม่รู้ขนาดล่วงหน้า อาจใหญ่มาก หรือต้องการความยืดหยุ่น

Modern C++: ใช้ std::vector แทน

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> stack;  // ไดนามิกอัตโนมัติ
    
    stack.push_back(10);  // push
    stack.push_back(20);
    
    cout << "Top: " << stack.back() << endl;  // peek
    stack.pop_back();       // pop
    
    return 0;
}