Created
January 17, 2018 10:46
-
-
Save jason790228/294ac0e6b2df81a579b690f2f0d2a53e to your computer and use it in GitHub Desktop.
11995
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
#include <string> | |
using namespace std; | |
void put_data(const int &input_data, vector<int> &stack, vector<int> &queue, vector<int> &priority_queue) | |
{ | |
stack.push_back(input_data); | |
queue.push_back(input_data); | |
priority_queue.push_back(input_data); | |
} | |
bool is_stack(const int &output_data, const vector<int> &stack) | |
{ | |
return (stack.size() > 0 && output_data == stack.back()); | |
} | |
bool is_queue(const int &output_data, const vector<int> &queue) | |
{ | |
return (queue.size() > 0 && output_data == queue.front()); | |
} | |
bool is_priority_queue(const int &output_data, vector<int> &priority_queue) | |
{ | |
sort(priority_queue.begin(), priority_queue.end()); | |
return (priority_queue.size() > 0 && output_data == priority_queue.back()); | |
} | |
void delete_data(vector<int> &stack, vector<int> &queue, vector<int> &priority_queue, const bool &stack_flag, const bool &queue_flag, const bool &priority_queue_flag) | |
{ | |
if (stack_flag && stack.size() > 0) | |
stack.pop_back(); | |
if (queue_flag && queue.size() > 0) | |
queue.erase(queue.begin()); | |
if (priority_queue_flag && priority_queue.size() > 0) | |
priority_queue.pop_back(); | |
} | |
void take_data(const int &output_data, vector<int> &stack, vector<int> &queue, vector<int> &priority_queue, bool &stack_flag, bool &queue_flag, bool &priority_queue_flag) | |
{ | |
if (stack_flag && !is_stack(output_data, stack)) | |
stack_flag = false; | |
if (queue_flag && !is_queue(output_data, queue)) | |
queue_flag = false; | |
if (priority_queue_flag && !is_priority_queue(output_data, priority_queue)) | |
priority_queue_flag = false; | |
delete_data(stack, queue, priority_queue, stack_flag, queue_flag, priority_queue_flag); | |
} | |
void show_data_structure_type(const bool &stack_flag, const bool &queue_flag, const bool &priority_queue_flag) | |
{ | |
if ((stack_flag && queue_flag) || (stack_flag && priority_queue_flag) || (queue_flag && priority_queue_flag)) cout << "not sure" << endl; | |
else if (stack_flag) cout << "stack" << endl; | |
else if (queue_flag) cout << "queue" << endl; | |
else if (priority_queue_flag) cout << "priority queue" << endl; | |
else cout << "impossible" << endl; | |
} | |
int main() | |
{ | |
while (1) | |
{ | |
string input_data_number; | |
cin >> input_data_number; | |
if (input_data_number.empty()) break; | |
bool stack_flag(true), queue_flag(true), priority_queue_flag(true); | |
vector<int> stack; | |
vector<int> queue; | |
vector<int> priority_queue; | |
for (int i = 0; i < atoi(input_data_number.c_str()); i++) | |
{ | |
int type_command(0), io_data(0); | |
cin >> type_command >> io_data; | |
if (type_command == 1) | |
{ | |
put_data(io_data, stack, queue, priority_queue); | |
} | |
else if (type_command == 2) | |
{ | |
take_data(io_data, stack, queue, priority_queue, stack_flag, queue_flag, priority_queue_flag); | |
} | |
} | |
show_data_structure_type(stack_flag, queue_flag, priority_queue_flag); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment