Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct cell
- {
- int value;
- cell* next;
- cell(int value, cell* next)
- {
- this->value = value;
- this->next = next;
- }
- };
- struct queue
- {
- cell* first;
- cell* last;
- queue()
- {
- first = last = nullptr;
- };
- bool isEmpty()
- {
- return first == nullptr;
- };
- void push(int value)
- {
- cell* newCell = new cell(value, nullptr);
- if (isEmpty())
- {
- first = last = newCell;
- }
- else
- {
- last->next = newCell;
- last = newCell;
- }
- };
- int pop()
- {
- if (isEmpty())
- {
- throw exception("queue is empty");
- }
- else if (first == last)
- {
- int toReturn = first->value;
- delete first;
- first = last = nullptr;
- return toReturn;
- }
- else
- {
- cell* tmp = first;
- int toReturn = tmp->value;
- first = first->next;
- delete tmp;
- return toReturn;
- }
- };
- void print()
- {
- if (isEmpty())
- {
- cout << "Queue is empty" << endl;
- return;
- }
- cell* tmp = first;
- size_t k = 1;
- while (tmp != nullptr)
- {
- cout << k++ << ") " << tmp->value << endl;
- tmp = tmp->next;
- }
- }
- };
- int main()
- {
- try
- {
- size_t arraySize;
- ifstream in("input.txt");
- if (!in.is_open())
- throw exception("File is not opened");
- in >> arraySize;
- int* array = new int[arraySize];
- size_t k = 0;
- while (!in.eof())
- in >> array[k++];
- in.close();
- queue correctQueue;
- for (size_t i = 0; i < arraySize; i++)
- if (array[i] > 0)
- correctQueue.push(array[i]);
- for (size_t i = 0; i < arraySize; i++)
- if (array[i] <= 0)
- correctQueue.push(array[i]);
- correctQueue.print();
- }
- catch (exception ex)
- {
- cout << "Error: " << ex.what() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment