Page 1 of 1

Priority Queue Exercise

Posted: Sat Dec 15, 2018 7:07 am
by sonicflare9
I have to do this


Imagine you're writing a little program to categorize bugs in a game and you want to push the bugs into a queue. The bugs will have a char priority(level) of 'A', 'B', or 'C'.
I want you to create a small class (or struct) for the bugs and they only have to have two properties:
A char for the priority with a suggested name of m_cPriority
A string or char* for the description with a suggested name of m_sDesc

You'll be creating a queue of these objects as the nodes and this will be somewhat of a custom priority queue exercise.
For the most part, you're going to change the enQueue method of the BasicQueue to account for the priority of the Bug. For maximum templatability(?), you should have the char priority as the second parameter in the new enQueue in case you want to test the queue with other types. Then you would somehow need to set the priority of the node in the main when enQueue is called.
For example, when enQueueing a Bug node, invoke a GetPriority method for the Bug that returns the char priority and have that function invocation as the second parameter of the enQueue call.
Or, if you want to enQueue a simple type like int, give the simple nodes an arbitrary char priority. They would, of course, not be Bugs but you're still able to use this priority queue for other purposes.

So, in the enQueue, you have to check the priority of the Node and then insert the Node in the queue based on these priorities. 'A' have the highest priority, 'B' second and 'C' third. Nodes of the same priority are pushed to the end of their respective priorities. For example a new 'A' bug, will get pushed to the end of the 'A's already in the queue.
All I want you to alter is the enQueue and add the Bug struct and account for this new priority.

i have this so far basicqueue.h

Code: Select all

 #pragma once
#include <iostream>
using namespace std;
 
class Unit
{
private:
    string m_sName;
public:
    Unit(const string s) : m_sName(s) {}
    friend ostream& operator<<(ostream& s, const Unit& u);
};
 
template <class T>
class Node
{
public:
    T m_tData;
    Node<T>* m_pNext;
    Node(T val) : m_tData(val), m_pNext(nullptr) {}
};
 
class Bugs
{
private:
  char m_cPriority;
  string m_sDesc;
 
};
 
template <class T>
class BasicQueue
{
private:
    Node<T>* m_pHead;
    Node<T>* m_pTail;
public:
    BasicQueue() : m_pHead(nullptr), m_pTail(nullptr) {}
    ~BasicQueue()
    {
        //Node<T>* current = m_pHead; // Consider this an iterator.
        Node<T>* next;
        while (m_pHead != nullptr)
        {
            next = m_pHead->m_pNext;
            delete m_pHead;
            m_pHead = next;
        }
    }
    void enQueue(T val) // 'Push' method.
    {
        Node<T>* newNode = new Node<T>(val);
        if (m_pHead == nullptr) // List is empty.
        {
            m_pHead = m_pTail = newNode;
        }
        else // New node becomes the new tail.
        {
            m_pTail->m_pNext = newNode;
            m_pTail = m_pTail->m_pNext;
        }
    }
    T deQueue() // 'Pop' method. 
    {
        if (m_pHead != nullptr)
        {
            T val = m_pHead->m_tData; // Storing the data to be returned.
            Node<T>* next;
            next = m_pHead->m_pNext;
            delete m_pHead; // Deallocating head.
            m_pHead = next;
            return val; // Throwing copy of old head's data.
        }
    }
    bool isEmpty()
    {
        return m_pHead == nullptr;
    }
    void print()
    {
        if (isEmpty())
            return;
        cout << "Queue contents: ";
        Node<T>* current = m_pHead;
        while (current != nullptr)
        {
            cout << current->m_tData << ' ';
            current = current->m_pNext;
        }
        cout << endl;
    }
};
 
ostream& operator<<(ostream& s, const Unit& u)
{
    s << u.m_sName;
    return s;
}
main.cpp

Code: Select all

 #include <iostream>
#include <string>
#include "BasicQueue.h"
using namespace std;

int main()
{
	BasicQueue<int> iQ;
	iQ.enQueue(42);
	iQ.enQueue(117);
	iQ.print();
	Unit uOne("Infantry");
	Unit uTwo("Commando");
	BasicQueue<Unit> uQ;
	uQ.enQueue(uOne);
	uQ.enQueue(uTwo);
	uQ.print();
	uQ.deQueue();
	uQ.print();
	system("pause");
	return 0;
}



this is my basicqueue.h

Re: Priority Queue Exercise

Posted: Sat Dec 15, 2018 8:51 am
by SparkOut
I am sorry but I doubt you will be able to get any help here. This forum is for user self-support with the high-level language LiveCode and its development environment. The language and syntax for LiveCode is very different to your sample.
Good luck with your project.

Re: Priority Queue Exercise

Posted: Sat Dec 15, 2018 11:19 am
by Klaus
Hi sonicflare9,

welcome to the forum!
BTW, a little "Hello" wouldn't have hurt. 8)

What SparkOut said, this is a https://livecode.com forum.


Best

Klaus

Re: Priority Queue Exercise

Posted: Sun Dec 16, 2018 12:21 am
by dunbarx
I think that sonicflare9 should abandon whatever tools he is currently using, and use LiveCode instead.

Then his problem would be fun to solve, easy to solve, and take little time to solve.

Craig

Re: Priority Queue Exercise

Posted: Sun Dec 16, 2018 1:01 pm
by bogs
dunbarx wrote:
Sun Dec 16, 2018 12:21 am
I think that sonicflare9 should abandon whatever tools he is currently using, and use LiveCode instead.
Well, I think sonicflare9 should finish learning C/C++ (looks like to me), and then go on to compliment his programming with Lc or the tools of their choice. You could certainly do worse than learn how C works.

Re: Priority Queue Exercise

Posted: Sun Dec 16, 2018 6:39 pm
by jacque
And I think sonicflare9 is a bot programmed to recognize development forums and post something that might sound relevant. But then, I'm jaded.

Re: Priority Queue Exercise

Posted: Sun Dec 16, 2018 8:39 pm
by bogs
jacque wrote:
Sun Dec 16, 2018 6:39 pm
And I think sonicflare9 is a bot...
That doesn't mean it can't learn to program while moving on to become a fully developed AI... :twisted:

Re: Priority Queue Exercise

Posted: Sun Dec 16, 2018 10:01 pm
by SparkOut
I thought along the same lines as Jwack originally too. I dithered about whether just to report as suspicious at the time, but gave the benefit of the doubt.

Re: Priority Queue Exercise

Posted: Mon Dec 17, 2018 1:43 am
by FourthWorld
A single post from an account opened two months ago, and the only thing posted here isn't relevant to LC.

I try to give the benefit of the doubt, but if the post was made in earnest the OP will be back and explain what's up. If I see no such explanation within a couple days I'll nix this thread.

Re: Priority Queue Exercise

Posted: Mon Dec 17, 2018 2:31 am
by jacque
bogs wrote:
Sun Dec 16, 2018 8:39 pm
jacque wrote:
Sun Dec 16, 2018 6:39 pm
And I think sonicflare9 is a bot...
That doesn't mean it can't learn to program while moving on to become a fully developed AI... :twisted:
So true. For all we know, you're a bot. :)

Re: Priority Queue Exercise

Posted: Mon Dec 17, 2018 1:10 pm
by bogs
jacque wrote:
Mon Dec 17, 2018 2:31 am
bogs wrote:
Sun Dec 16, 2018 8:39 pm
jacque wrote:
Sun Dec 16, 2018 6:39 pm
And I think sonicflare9 is a bot...
That doesn't mean it can't learn to program while moving on to become a fully developed AI... :twisted:
So true. For all we know, you're a bot. :)
No no, Thierry had it right (see siggy), I'm a disembodied synapses connection, only part of the whole (0.583333333333 of 7)!
(But not quite 'scary' :D )