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

โปรแกรม C++ สำหรับการใช้งาน Deque ใน STL

Deque (Double Ended Queue) เป็นโครงสร้างข้อมูลแบบคิวที่ทำการแทรกและลบได้ที่ทั้งสองข้าง (หน้าและท้าย) ข้อมูลสามารถแทรกได้ที่หน้าและท้าย และลบได้ที่หน้าและท้ายเช่นกัน

อัลกอริธึมและฟังก์ชันหลัก

  • d.size() – คืนขนาดของ deque
  • d.push_back() – แทรกองค์ประกอบจากท้าย
  • d.push_front() – แทรกองค์ประกอบจากหน้า
  • d.pop_back() – ลบองค์ประกอบจากท้าย
  • d.pop_front() – ลบองค์ประกอบจากหน้า
  • d.front() – คืนองค์ประกอบหน้าสุด
  • d.back() – คืนองค์ประกอบท้ายสุด

โค้ดตัวอย่าง (โปรแกรมโต้ตอบแบบเมนู)

#include <iostream>
#include <deque>
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    deque<int> d;
    deque<int>::iterator it;
    int c, item;

    while (true) {
        cout << "1.Size of the Deque" << endl;
        cout << "2.Insert Element at the End" << endl;
        cout << "3.Insert Element at the Front" << endl;
        cout << "4.Delete Element at the End" << endl;
        cout << "5.Delete Element at the Front" << endl;
        cout << "6.Front Element at Deque" << endl;
        cout << "7.Last Element at Deque" << endl;
        cout << "8.Display Deque" << endl;
        cout << "9.Exit" << endl;
        cout << "Enter your Choice: ";
        cin >> c;

        switch(c) {
            case 1:
                cout << "Size of the Deque: " << d.size() << endl;
                break;
            case 2:
                cout << "Enter value to be inserted at the end: ";
                cin >> item;
                d.push_back(item);
                break;
            case 3:
                cout << "Enter value to be inserted at the front: ";
                cin >> item;
                d.push_front(item);
                break;
            case 4:
                if (!d.empty()) {
                    item = d.back();
                    d.pop_back();
                    cout << "Element " << item << " deleted" << endl;
                } else cout << "Deque is empty" << endl;
                break;
            case 5:
                if (!d.empty()) {
                    item = d.front();
                    d.pop_front();
                    cout << "Element " << item << " deleted" << endl;
                } else cout << "Deque is empty" << endl;
                break;
            case 6:
                if (!d.empty()) cout << "Front Element of the Deque: " << d.front() << endl;
                else cout << "Deque is empty" << endl;
                break;
            case 7:
                if (!d.empty()) cout << "Back Element of the Deque: " << d.back() << endl;
                else cout << "Deque is empty" << endl;
                break;
            case 8:
                cout << "Elements of Deque: ";
                for (it = d.begin(); it != d.end(); it++) cout << *it << " ";
                cout << endl;
                break;
            case 9:
                exit(0);
            default:
                cout << "Wrong Choice" << endl;
        }
    }
    return 0;
}

ตัวอย่างผลลัพธ์ (บางส่วน)

Enter your Choice: 1
Size of the Deque: 0
Enter your Choice: 2
Enter value to be inserted at the end: 1
Enter your Choice: 3
Enter value to be inserted at the front: 2
Enter your Choice: 6
Front Element of the Deque: 2
Enter your Choice: 7
Back Element of the Deque: 1
Enter your Choice: 8
Elements of Deque: 2 1
Enter your Choice: 2
Enter value to be inserted at the end: 4
Enter your Choice: 3
Enter value to be inserted at the front: 5
Enter your Choice: 8
Elements of Deque: 5 2 1 4
Enter your Choice: 4
Element 4 deleted
Enter your Choice: 5
Element 5 deleted
Enter your Choice: 8
Elements of Deque: 2 1
Enter your Choice: 9

ความแตกต่างจาก Vector: Deque รองรับ push/pop ทั้งหน้าและท้ายได้ O(1) ส่วน Vector push_back/pop_back O(1) แต่ push_front/pop_front O(n) Deque เข้าถึงแบบ random access ได้เหมือน vector (แต่ช้ากว่าเล็กน้อย)