Priority Queue Exercise

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sonicflare9
Posts: 1
Joined: Fri Oct 05, 2018 1:49 pm

Priority Queue Exercise

Post by sonicflare9 » Sat Dec 15, 2018 7:07 am

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

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Priority Queue Exercise

Post by SparkOut » Sat Dec 15, 2018 8:51 am

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.

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Priority Queue Exercise

Post by Klaus » Sat Dec 15, 2018 11:19 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Priority Queue Exercise

Post by dunbarx » Sun Dec 16, 2018 12:21 am

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Priority Queue Exercise

Post by bogs » Sun Dec 16, 2018 1:01 pm

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.
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Priority Queue Exercise

Post by jacque » Sun Dec 16, 2018 6:39 pm

And I think sonicflare9 is a bot programmed to recognize development forums and post something that might sound relevant. But then, I'm jaded.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Priority Queue Exercise

Post by bogs » 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:
Image

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Priority Queue Exercise

Post by SparkOut » Sun Dec 16, 2018 10:01 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Priority Queue Exercise

Post by FourthWorld » Mon Dec 17, 2018 1:43 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Priority Queue Exercise

Post by jacque » 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. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Priority Queue Exercise

Post by bogs » Mon Dec 17, 2018 1:10 pm

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 )
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”