Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help required regarding priority queueing implemented via linked list (http://www.go4expert.com/forums/help-required-regarding-priority-t29162/)

sam2012 6Oct2012 02:59

Help required regarding priority queueing implemented via linked list
 
I have a queue class code is given below:
Code:

struct ListNode
{
    ListNode(int value);
    int value;
    ListNode* next;
};
ListNode::ListNode(int value): value(value), next(nullptr) {}

public class queue
{
public:
    queue();
    ~queue();
    bool Empty() const;
    int Next() const;
    void Enqueue(int value);
    void Dequeue();
        void DisplayAll() const;
        int getP() const;


      private:
    // Disable copying to keep the example simple

  queue(queue const& obj);

    ListNode* head;
    ListNode* tail;
    gcroot<String^> name; // Added this
};

queue::queue():head(nullptr), tail(nullptr) {}

queue::~queue()
{
    while (head != nullptr)
    {
        Dequeue();
    }
}


bool queue::Empty() const
{
    return head == nullptr;
}

int queue::Next() const
{
    return head->value;
}

void queue::Enqueue(int value)
{
    if (head == nullptr)
    {
        head = tail = new ListNode(value);
    }
    else
    {
        tail->next = new ListNode(value);
        tail = tail->next;
    }
}

void queue::Dequeue()
{
    ListNode* temp = head->next;
    delete head;
    head = temp;
}

void queue::DisplayAll() const
{
    for (ListNode* p = head; p; p = p->next)
    {
        Console::WriteLine("the element is {0}", p->value);
    }

}

I need some help regarding queues operation, which I already have made. To help explain the problem you need to look at this code and the queue operations given in the above posted code.consider we have 3 queues cat1, cat2, cat 3. I want to insert items with priority 1,2,3 respectively. which I am doing via this code. Now I want to process these inserted messages(or items "no"). First I want to process all messages in queue "cat1" and dequeue them one by one until its empty, then it should move to "cat2" and same process and then to "cat3".
Code:

no=noofmsg-1;
    if(Analysis[no].prio==1)
        cat1->Enqueue(no);

    else if(Analysis[no].prio==2)
        cat2->Enqueue(no);

    else if(Analysis[no].prio==3)
        cat3->Enqueue(no);

      while(!(cat1->Empty()))
        {
            p= cat1->Next();
            TimDep= currtime+expntl();
          // slist->Add(TimDep,"D");

            Analysis[p].TEsf=TimDep;
            Console::WriteLine(" the value at  is  Analysis{0} is {1}", p, Analysis[p].TEsf );
            /*avg1= Analysis[p].TEsf-Analysis[p].TEa;
            Console::WriteLine(" The priority 1 messages has a waiting time of {0} ", avg1 );*/
            cat1->Dequeue();

        }

  if(cat1->Empty())
    {
      while(!(cat2->Empty()))
        {
            p= cat2->Next();
            TimDep= currtime+expntl();
            //slist->Add(TimDep,"D");

            Analysis[p].TEsf=TimDep;
            //Console::WriteLine(" the value at  is  Analysis{0} is {1}", p, Analysis[p].TEsf );
            cat2->Dequeue();
        }
      }
  if((cat1->Empty())&&(cat2->Empty()))
    {
      while(!(cat3->Empty()))
        {
            p= cat3->Next();
            TimDep= currtime+expntl();
            //slist->Add(TimDep,"D");

            Analysis[p].TEsf=TimDep;
            //Console::WriteLine(" the value at  is  Analysis{0} is {1}", p, Analysis[p].TEsf );
            cat3->Dequeue();
        }
      }

but this code is only processing items with priority 1. Can you help with this code please.......I am helpless. How can I process priority 2 and 3 items if they come before 1.


All times are GMT +5.5. The time now is 10:24.