#include #include #include #include using namespace std; ifstream fin; // File input stream. ofstream fout; // File script stream. ofstream fout2; // File output stream. string filepath; // Holds file path of input file. struct Stored_Item // Item stored in linked list { int value; Stored_Item* next; }; class Extended_Queue { public: int size; Stored_Item* head; Stored_Item* tail; void Enqueue(int element); void Dequeue(); bool isEmpty(); int getSize(); int getMid(); void Make_empty_queue(); void High_Priority_Queue(int element); void Medium_Priority_Queue(int element); void printQueueElements(); }; void Extended_Queue::printQueueElements() // Prints the Extended Queue. { Stored_Item* temp; temp = head; cout << "The Queue is now " << size << " nodes long." << endl; cout << "The Queue's elements are: " << endl; fout << "The Queue is now " << size << " nodes long." << endl; fout << "The Queue's elements are: " << endl; while(temp != NULL) { cout << "|" << temp->value << "|"; fout << "|" << temp->value << "|"; temp = temp->next; } cout << endl << endl; fout << endl << endl; } void Extended_Queue::Enqueue(int element) { if(isEmpty()) // first Stored_Item { head = new Stored_Item; tail = head; // head becomes the tail head->value = element; // value holds element head->next = NULL; // head points to nothing } else { tail->next = new Stored_Item; tail->next->value = element; // Sets value of next link. tail->next->next = NULL; // Sets link of next link. tail = tail->next; // Sets link of own link. } cout << element << " has been added to the queue (regular enqueue)." << endl; fout << element << " has been added to the queue (regular enqueue)." << endl; size++; printQueueElements(); } void Extended_Queue::Dequeue() { if(isEmpty()) // Empty list { return; } else if(head == tail) // One item left. { cout << head->value << " has been dequeued from the queue." << endl; fout << head->value << " has been dequeued from the queue." << endl; fout2 << head->value << ", "; head = NULL; } else // Remove the first Stored_Item inserted and point to the next. { Stored_Item *current; current = head; cout << head->value << " has been dequeued from the queue." << endl; fout << head->value << " has been dequeued from the queue." << endl; fout2 << head->value << ", "; head = current->next; current = NULL; } size--; printQueueElements(); } bool Extended_Queue::isEmpty() // Checks if queue is empty or not { if(getSize() == 0) { return true; } else { return false; } } int Extended_Queue::getSize() // Returns the number of nodes in the queue. { return size; } int Extended_Queue::getMid() // Calculates and returns 1/2 the number of nodes in the queue. { return (size/2); } void Extended_Queue::Make_empty_queue() // Constructor, makes a new empty queue. { size = 0; head = NULL; tail = NULL; cout << "An empty queue has been created." << endl << endl; fout << "An empty queue has been created." << endl << endl; } void Extended_Queue::High_Priority_Queue(int element) // Adds an element to the front of queue. { Stored_Item* temp; temp = new Stored_Item; temp->value = element; temp->next = head; head = temp; size++; cout << element << " has been added to the front of the queue (high priority)." << endl; fout << element << " has been added to the front of the queue (high priority)." << endl; printQueueElements(); } void Extended_Queue::Medium_Priority_Queue(int element) // Adds an element to the middle of queue. { Stored_Item* temp; temp = new Stored_Item; temp = head; for (int i = 1; i < getMid(); i++) { temp = temp->next; } Stored_Item* inserted; inserted = new Stored_Item; inserted->value = element; inserted->next = temp->next; temp->next = inserted; inserted = NULL; temp = NULL; size++; cout << element << " has been added to the middle of the queue (medium priority)." << endl; fout << element << " has been added to the middle of the queue (medium priority)." << endl; printQueueElements(); } void main() { Extended_Queue qt; cout << "Note: This program will output the items that are dequeued from the list into \n'Homework4Output.txt' It will also output a script of the program to \n'Homework4Script.txt'" << endl << endl; cout << "Please enter input filepath: "; cin >> filepath; fin.open(filepath.c_str()); fout.open("Homework4Script.txt"); fout2.open("Homework4Output.txt"); fout2 << "The items that are dequeued are: " << endl; qt.Make_empty_queue(); while (!fin.eof()) { char command; // Holds which function will be called. int number; // Holds the value to queue. fin >> command; switch (command) { case 'e': fin >> number; qt.Enqueue(number); break; case 'h': fin >> number; qt.High_Priority_Queue(number); break; case 'm': fin >> number; qt.Medium_Priority_Queue(number); break; case 'd': qt.Dequeue(); break; } } fin.close(); cout << "The input is complete. The queue will now be emptied." << endl << endl; fout << "The input is complete. The queue will now be emptied." << endl << endl; while (!qt.isEmpty()) { qt.Dequeue(); } }