Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

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