Computer >> Máy Tính >  >> Lập trình >> C ++

Chương trình C ++ để triển khai hàng đợi trong STL

Hàng đợi là một cấu trúc tuyến tính tuân theo thứ tự nhập trước xuất trước (FIFO) trong đó các hoạt động được thực hiện trên các phần tử của hàng đợi.

Thuật toán

Functions used here:
   q.size() = Returns the size of queue.
   q.push() = It is used to insert elements to the queue.
   q.pop() = To pop out the value from the queue.
   q.front() = Returns the front element of the array.
   q.back() = Returns the back element of the array.

Mã mẫu

#include<iostream>
#include <queue>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
   queue<int> q;
   int c, i;
   while (1) {
      cout<<"1.Size of the Queue"<<endl;
      cout<<"2.Insert Element into the Queue"<<endl;
      cout<<"3.Delete Element from the Queue"<<endl;
      cout<<"4.Front Element of the Queue"<<endl;
      cout<<"5.Last Element of the Queue"<<endl;
      cout<<"6.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of the Queue: ";
            cout<<q.size()<<endl;
         break;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            q.push(i);
         break;
         case 3:
            i = q.front();
            q.pop();
            cout<<"Element "<<i<<" Deleted"<<endl;
         break;
         case 4:
            cout<<"Front Element of the Queue: ";
            cout<<q.front()<<endl;
         break;
         case 5:
            cout<<"Back Element of the Queue: ";
            cout<<q.back()<<endl;
         break;
         case 6:
            exit(1);
         break;
         default:
            cout<<"Wrong Choice"<<endl;
      }
   }
   return 0;
}

Đầu ra

1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 1
Size of the Queue: 0
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 2
Enter value to be inserted: 2
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 3
Element 1 Deleted
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 2
Enter value to be inserted: 7
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 2
Enter value to be inserted: 6
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 4
Front Element of the Queue: 2
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit

Enter your Choice: 5
Back Element of the Queue: 6
1.Size of the Queue
2.Insert Element into the Queue
3.Delete Element from the Queue
4.Front Element of the Queue
5.Last Element of the Queue
6.Exit
Enter your Choice: 6
Exit code: 1