I have a queue class code is given below:

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.

if(Analysis[no].priority==1)
         cat1->Enqueue(no);
      else if(Analysis[no].priority==2)
         cat2->Enqueue(no);
      else if(Analysis[no].priority==3)
         cat3->Enqueue(no);
      while(!(cat1->Empty()))
         {
            p= cat1->Next();
            TimDep= currtime+expntl();
            slist->Add(TimDep,"D");
            Analysis[p].TEsf=TimDep;
         }

consider we have 3 queues cat1, cat2, cat 3. I want to insert items with pririty 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 untill its empty, then it should move to "cat2" and same process and then to "cat3". I am stuck in the process of iterating through each element of queue how can I do that? can you help me please!!!!

I won't go beyond the struct declaration. You are using the same name for the constructor argument as you are using for the member variable. DON'T DO THAT! As a good practice, use m_ as a prefix for member variables, so instead of value, the member variable name is m_value, or m_pNext for the list link. The 'p' in m_pNext indicates that you intend for it to be a pointer.

As for your particular usage problem, you are only showing a small part of your code, and not enough to determine what you are really doing. A fuller explanation of what you are trying to accomplish would be good. It sounds like you want to process queue 1, then queue 2, etc in order? Is that correct?

yes you are right, Can you help me to write the code well. I mean only the iteration part of all the items of one queue then second and then third.

Hi,

I have written some code for priority queuing but this code is only processing items with priority 1. Can you help with this code please.......I am helpless

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();
         }
      }

See comments:

  while(!(cat1->Empty()))
  {
        // process queue 1
  }

  if(cat1->Empty())    // Didn't you just empty queue 1? 
  {                    // Why do you need to test if it's empty?
     while(!(cat2->Empty()))
     {
        // process queue 2
     }
  }

I don't see a definition for nullptr. Did you define it to be FALSE or 0? Or is it a C++ definition that you are assuming to be FALSE/0?

Output values at key places in the code to see if the values are correct and the code is making the decisions you expect.

Thankyou very much for your reply. I have seen the use of the nullptr keyword instead of 0 to indicate a null pointer value.

is that OK to write

while(!(cat1->Empty()))

But still I am not getting the results what I want. PLEASE HELP !!!!!!!!!!!!!!!!!!!!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.