Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <iostream>
- using std::cin;
- using std::cout;
- struct CListNode {
- int Data;
- CListNode* Next;
- explicit CListNode( int data ) : Data( data ), Next( nullptr ) {}
- };
- class CQueue {
- public:
- CQueue() : head( nullptr ), tail( nullptr ) {}
- CQueue( const CQueue& ) = delete;
- CQueue( CQueue&& ) = delete;
- ~CQueue();
- CQueue& operator=( const CQueue& ) = delete;
- // Добавление в очередь.
- void Enqueue( int data );
- // Извлечение. Если очередь пуста, то возвращает -1.
- int Dequeue();
- bool IsEmpty() const { return head == nullptr; }
- private:
- CListNode* head;
- CListNode* tail;
- };
- CQueue::~CQueue()
- {
- while( !IsEmpty() ) {
- Dequeue();
- }
- }
- void CQueue::Enqueue( int data )
- {
- if( head == nullptr ) {
- assert( tail == nullptr );
- head = new CListNode( data );
- tail = head;
- } else {
- tail->Next = new CListNode( data );
- tail = tail->Next;
- }
- }
- int CQueue::Dequeue()
- {
- if( head == nullptr ) {
- return -1;
- }
- // Случай, когда в очереди остался один элемент, обрабатываем отдельно.
- if( head == tail ) {
- const int result = head->Data;
- delete head;
- head = nullptr;
- tail = nullptr;
- return result;
- }
- const int result = head->Data;
- CListNode* toDelete = head;
- head = head->Next;
- delete toDelete;
- return result;
- }
- int main()
- {
- int commandsCount = 0;
- cin >> commandsCount;
- CQueue queue;
- for( int i = 0; i < commandsCount; ++i ) {
- int command = 0;
- int value = 0;
- cin >> command >> value;
- switch( command ) {
- case 3:
- queue.Enqueue( value );
- break;
- case 2:
- if( queue.Dequeue() != value ) {
- cout << "NO";
- return 0;
- }
- break;
- default:
- assert( false );
- }
- }
- cout << "YES";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement