Question

Write a C++ program that will implement a Priority Queue class using the List Class provided...

Write a C++ program that will implement a Priority Queue class using the List Class provided below. Test your class with the input file QueueInput.txt. You will have to edit the file and eliminate the text at the top of the file. You will not alter the commands

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complete Program:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Entry
{
public:
   int value;
   int priority;

   Entry()
   {
       value = 0;
       priority = 0;
   }

   Entry(int aValue, int aPriority)
   {
       value = aValue;
       priority = aPriority;
   }
};

class List
{
private:
   int CAPACITY;
   Entry *arr;
   int currentPos, size1;

public:
   List()
   {
       CAPACITY = 20;
       arr = (Entry *)malloc(CAPACITY * sizeof(Entry *));
       size1 = 0;
       currentPos = -1;
   }

   List(const List &obj)
   {
       CAPACITY = obj.CAPACITY;
       size1 = obj.size1;
       currentPos = obj.currentPos;
       if(arr != NULL)
       {
           for(int i = 0; i < size1; i++)
               arr[i] = obj.arr[i];
       }
   }

   bool empty()
   {
       return size1 == 0;
   }

   void front()
   {
       if(size1 == 0)
           cout << "List is empty!" << endl;
       else
           currentPos = 0;
   }

   void end()
   {
       if(size1 == 0)
           cout << "List is empty!" << endl;
       else
           currentPos = size1 - 1;
   }

   void prev()
   {
       if(size1 <= 0)
           cout << "Previous element is empty.\n";
       else
       {
           currentPos--;
       }
   }

   void next()
   {
       if(currentPos >= CAPACITY - 1)
           cout << "Next element is empty.\n";
       else
       {
           currentPos++;
       }
   }

   int getPos()
   {
       return currentPos;
   }

   void setPos(int n)
   {
       if(n < 0 || n >= size1)
           cout << "Position should not be out of range." << endl;
       else
           currentPos = n;
   }

   void insertBefore(Entry ele)
   {
       if(size1 == CAPACITY)
       {
           cout << "The list has reached max capacity. Cannot add more elements.\n";
           return;
       }
       else if(currentPos - 1 < 0)
       {
           cout << "No previous position.\n";
           return;
       }
       else
       {
           currentPos--;
           arr[currentPos] = ele;
           size1++;
       }
   }

   void insertAfter(Entry ele)
   {
       if(size1 == CAPACITY)
       {
           cout << "The list is full. Elements have NOT been added.\n";
           return;
       }
       else if(currentPos + 1 >= CAPACITY)
       {
           cout << "No next position.\n";
           return;
       }
       else
       {
           currentPos++;
           arr[currentPos] = ele;
           size1++;
       }
   }

   Entry getElement()
   {
       return arr[currentPos];
   }

   int size()
   {
       return size1;
   }

   void replace(Entry n)
   {
       if(currentPos < 0)
           cout << "List is empty!" << endl;
       else
           arr[currentPos] = n;
   }

   void erase()
   {
       if(currentPos < 0)
           cout << "List is empty!" << endl;
       else
       {
           for(int i = currentPos; i < size1 - 1; i++)
           {
               arr[i] = arr[i + 1];
           }
           size1--;
       }
   }

   void clear()
   {
       currentPos = -1;
       size1 = 0;
       arr = new Entry[CAPACITY];
   }

   void reverse()
   {
       for(int i = 0; i < size1 / 2; i++)
       {
           Entry tmp = arr[i];
           arr[i] = arr[size1 - i];
           arr[size1 - i] = tmp;
       }
   }

   void swap(List a)
   {
       Entry *tmp = arr;
       arr = a.arr;
       a.arr = tmp;
   }

   friend ostream &operator<<(ostream &out, List &l)
   {              
       for(int i = 0; i < l.size(); i++)
       {          
           if(l.arr[i].value != 0)
               out << l.arr[i].value << " - " << l.arr[i].priority << endl;
       }
       out << "\n";
       return out;
   }

   bool operator ==(List &l)
   {
       if(size1 != l.size1)
           return false;
  

       for(int i = 0; i < size1; i++)
       {
           if(arr[i].priority != l.arr[i].priority || arr[i].value != l.arr[i].value)
               return false;
       }

       return true;
   }

   void operator=(List &l)
   {
       if(this != &l)
       {
           delete [] arr;
           CAPACITY = l.CAPACITY;
           size1 = l.size1;
           arr = new Entry[CAPACITY];
           currentPos = l.currentPos;

           if(arr != NULL)
           {
               for(int i = 0; i < size1; i++)
                   arr[i] = l.arr[i];
           }      
       }
   }
};

class PriorityQueue
{
private:
   List list;

public:
   PriorityQueue()
   {}

   void enqueue(Entry ent)
   {
       list.insertAfter(ent);
   }

   Entry dequeue()
   {
       list.front();

       Entry ent = list.getElement();
       int index = 0;

       for(int i = 1; i < list.size(); i++)
       {
           list.next();

           if(list.getElement().priority > ent.priority)
           {
               ent = list.getElement();
               index = i;
           }
       }

       list.setPos(index);
       list.erase();

       return ent;
   }

   bool empty()
   {
       return list.empty();
   }
};


int main()
{
   ifstream infile;
   infile.open("QueueInput.txt");
   if(!infile)
   {
       cout << "Input file is not found!" << endl;
       exit(1);
   }

   PriorityQueue pq;
   string str;
   int value;
   int priority;

   infile >> str;
   while(infile)
   {
       if(str == "Enqueue")
       {
           infile >> value;
           infile >> priority;

           Entry ent(value, priority);

           pq.enqueue(ent);
       }
       else
       {
           Entry ent = pq.dequeue();
           cout << "Dequeue: " << ent.value << " - " << ent.priority << endl;
       }

       infile >> str;
   }
   infile.close();
  

   cout << endl;
   system("pause");
   return 0;
}

-----------------------------------------------------------------

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will implement a Priority Queue class using the List Class provided...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • Write a program in C++ to simulate a dispatcher using a priority queue system. A GUI...

    Write a program in C++ to simulate a dispatcher using a priority queue system. A GUI has to be used to enter new processes with priority included (numbering should be automatic). GUI command should be used to terminate processes. Context switches are to be by command with the cause of the switch being immaterial. Assume only one CPU. Priorities and numbers of processes can be kept small, big enough to demonstrate the listed functions below. You can pre-populate the queues...

  • solving using C. Use a singly linked list to implement a priority queue with two operations:...

    solving using C. Use a singly linked list to implement a priority queue with two operations: enqueue and dequeue. Each node contains an integer value, a priority, and a pointer to the next node. The priority is a value between 1- 10 (where 10 is the highest priority). When a value is added to the queue, it is added with a value and priority. When a value is removed from the priority queue, the first element with the highest priority...

  • (Please do in C++) Implement a Queue using a vector or the STD ::queue:: class Note...

    (Please do in C++) Implement a Queue using a vector or the STD ::queue:: class Note the difference in what it takes to implement using a char[] array vs a vector or the STD ::queue:: class The user will input a string and the program will return the string with each letter in the string duplicated. Displaying correct Queue implementation. Utilize the conventional methods as needed. Please input String: happy hhaappyy

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...

    Data Structure Java code Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to the order in which they were enqueued A typical priority queue supports following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT