smatskevich

QueueOnList2017

Sep 30th, 2017
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <assert.h>
  2. #include <iostream>
  3. using std::cin;
  4. using std::cout;
  5.  
  6. struct CListNode {
  7.     int Data;
  8.     CListNode* Next;
  9.  
  10.     explicit CListNode( int data ) : Data( data ), Next( nullptr ) {}
  11. };
  12.  
  13. class CQueue {
  14. public:
  15.     CQueue() : head( nullptr ), tail( nullptr ) {}
  16.     CQueue( const CQueue& ) = delete;
  17.     CQueue( CQueue&& ) = delete;
  18.     ~CQueue();
  19.     CQueue& operator=( const CQueue& ) = delete;
  20.  
  21.     // Добавление в очередь.
  22.     void Enqueue( int data );
  23.     // Извлечение. Если очередь пуста, то возвращает -1.
  24.     int Dequeue();
  25.     bool IsEmpty() const { return head == nullptr; }
  26.  
  27. private:
  28.     CListNode* head;
  29.     CListNode* tail;
  30. };
  31.  
  32. CQueue::~CQueue()
  33. {
  34.     while( !IsEmpty() ) {
  35.         Dequeue();
  36.     }
  37. }
  38.  
  39. void CQueue::Enqueue( int data )
  40. {
  41.     if( head == nullptr ) {
  42.         assert( tail == nullptr );
  43.         head = new CListNode( data );
  44.         tail = head;
  45.     } else {
  46.         tail->Next = new CListNode( data );
  47.         tail = tail->Next;
  48.     }
  49. }
  50.  
  51. int CQueue::Dequeue()
  52. {
  53.     if( head == nullptr ) {
  54.         return -1;
  55.     }
  56.  
  57.     // Случай, когда в очереди остался один элемент, обрабатываем отдельно.
  58.     if( head == tail ) {
  59.         const int result = head->Data;
  60.         delete head;
  61.         head = nullptr;
  62.         tail = nullptr;
  63.         return result;
  64.     }
  65.  
  66.     const int result = head->Data;
  67.     CListNode* toDelete = head;
  68.     head = head->Next;
  69.     delete toDelete;
  70.     return result;
  71. }
  72.  
  73. int main()
  74. {
  75.     int commandsCount = 0;
  76.     cin >> commandsCount;
  77.  
  78.     CQueue queue;
  79.     for( int i = 0; i < commandsCount; ++i ) {
  80.         int command = 0;
  81.         int value = 0;
  82.         cin >> command >> value;
  83.         switch( command ) {
  84.         case 3:
  85.             queue.Enqueue( value );
  86.             break;
  87.         case 2:
  88.             if( queue.Dequeue() != value ) {
  89.                 cout << "NO";
  90.                 return 0;
  91.             }
  92.             break;
  93.         default:
  94.             assert( false );
  95.         }
  96.     }
  97.     cout << "YES";
  98.     return 0;
  99. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×