STL 中实现队列的 C++ 程序


队列是一种遵循先进先出 (FIFO) 顺序的线性结构,其中的操作在队列中的元素上执行。

算法

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.

示例代码

#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;
}

输出

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

更新日期:2019 年 7 月 30 日

696 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告