Advertisement
VSS2

I Can Guess The Data Structure

Mar 24th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4. #include <string>
  5. #include <cstdio>
  6.  
  7. using namespace std;    //Comando para evitar ter que declarar variaveis globais
  8.  
  9. int main(){
  10.  
  11.     int endoffile;
  12.  
  13.     while(cin >> endoffile){
  14.  
  15.     bool pilha = true;
  16.     bool lista = true;
  17.     bool listap = true;
  18.  
  19.     //Cria fila, pilha e fila prioritaria de ints
  20.  
  21.     queue<int> l;  
  22.     stack<int> p;
  23.     priority_queue<int> lp;
  24.  
  25.         int AdcRem = 0;
  26.         int valor = 0;
  27.        
  28.         while(endoffile > 0){
  29.             scanf("%d %d",&AdcRem,&valor);
  30.            
  31.             //Se os valores forem um,
  32.             //adicione em cada tipo de estrutura o valor
  33.  
  34.             if (AdcRem == 1){
  35.                 if(pilha == true){
  36.                     p.push(valor);
  37.                 }
  38.                 if(lista == true){
  39.                     l.push(valor);
  40.                 }
  41.                 if(listap == true){
  42.                     lp.push(valor);
  43.                 }
  44.                
  45.             } else {
  46.  
  47.                 //Se o valor nao for 1 remova de cada estrutura
  48.                 //Antes veja se esta vazia,
  49.                 //Depois veja se o ultimo/primeiro/maior valor e igual ao removido
  50.  
  51.                 if (pilha == true){
  52.                     if(!p.empty() && p.top() == valor){
  53.                         p.pop();
  54.                     } else {
  55.                         pilha = false;
  56.                     }
  57.                 }
  58.                 if (lista == true){
  59.                     if(!l.empty() && l.front() == valor){
  60.                         l.pop();
  61.                     } else {
  62.                         lista = false;
  63.                     }
  64.                 }
  65.                 if (listap == true){
  66.                     if(!lp.empty() && lp.top() == valor){
  67.                         lp.pop();
  68.                     } else {
  69.                         listap = false;
  70.                     }
  71.                 }
  72.             }
  73.             endoffile--;
  74.         }
  75.        
  76.         int contador;
  77.  
  78.         contador = 0;
  79.        
  80.        if (pilha == true){contador++;}
  81.        if (lista == true){contador++;}
  82.        if (listap == true){contador++;}
  83.        
  84.        if (contador == 0){cout << "impossible" << endl;}
  85.        else if (contador > 1){cout << "not sure" << endl;}
  86.        else if (contador == 1){
  87.          if (pilha == true){cout << "stack" << endl;}
  88.          else if (lista == true){cout << "queue" << endl;}
  89.          else {cout << "priority queue" << endl;}
  90.        }
  91.     }
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement