I have a simple query, I am using code for "QUEUE IMPLEMENTATION USING SINGLE LINKED LIST" http://ds4beginners.wordpress.com/2006/12/17/queue-implementation-using-single-linked-list/ for my program but having an issue if you pleas help me.

public class queue
{
 gcroot<message^> msg;
  queue* next;
public:
 queue();
 queue* enqueue(queue*,message^);
 queue* dequeue(queue*);
 void queue_display(queue*);
 void setbinding(String^);
 String^ getbinding(void);
}object, *head=nullptr, *tail=nullptr;

the rest of queue functions are same as in the link . In the main function I have defined these queues.when I tried to insert element is "weather" queue and display it shows the same element in all the queues declared. Can you help me why this happening.

//main function

queue* weather = new queue();
  queue* news = new queue();
 
//inserting element is queue

head= weather->enqueue(head,(gcnew message("*144")));

//displaying queue

Console::WriteLine("the items in the queue 'news' are");
  news->queue_display(head);
  
  Console::WriteLine("the items in the queue 'weather' are");
  weather->queue_display(head);

Hoping best from your side

Edited 4 Years Ago by SAM2012: n/a

when I tried to insert element is "weather" queue and display it shows the same element in all the queues declared.

Yes, that's what you appear to have told it to do:

// Insert message and return the resulting queue (I'm assuming)
head= weather->enqueue(head,(gcnew message("*144")));

// Display the provided queue, which refers to weather
news->queue_display(head);

// Display the provided queue, which refers to weather
weather->queue_display(head);

I'm assuming that queue::enqueue() returns a pointer to the front of the queue, and queue::queue_display() displays the contents of the argument instead of the contents of the object on which it's being called. I can confirm or reject those assumptions if you post the full definition of the queue class.

Thank you very much for your reply.

public class queue
{
 gcroot<message^> msg;
 gcroot<String^> bindingkey;
 queue* next;
public:
 queue();
 queue* enqueue(queue*,message^);
 queue* dequeue(queue*);
 void queue_display(queue*);
 void setbinding(String^);
 String^ getbinding(void);
}object, *head=nullptr, *tail=nullptr;

queue::queue()
{
  
}

queue* queue::enqueue(queue* head,message^ key)
{
 queue* temp;
 temp=new queue;
 temp->msg =key;
 temp->next=nullptr;
 if(head==nullptr)
  head=temp;
 else
  tail->next=temp;
 tail=temp;
 return head;
}
//****************************************************
queue* queue::dequeue(queue* head)
{
queue* temp;
if(head==nullptr)
{
 return nullptr;
}
else if(head->next==nullptr)
{
  return nullptr;
}
else
{
 //Console::WriteLine("the element that is dequeued {0}",head->element);
 temp=head->next;
 head=temp;
 return head;
}
}
//***************************************************************
void queue::queue_display(queue* head)
{
 if(head!=nullptr)
 {
  while(head->next!=nullptr)
  {
   Console::WriteLine(" the element is {0}",head->msg->routingk);
  // Console::WriteLine(" the element is {0}",head->bindingkey);
   head=head->next;
  }
  Console::WriteLine("the last element in queue is {0}",head->msg->routingk);

 }
 else
  Console::WriteLine("the queue is empty");
}

The main function is this:

void main()
{
  
  //four queues are created and added to the list of the queues
  queue usa* = new queue();
  queue weather* = new queue();
  queue news* = new queue();
  queue europe* = new queue();

//Insertion of the message in the queue that is another class.
head= europe->enqueue(head,(gcnew message("pak.nasdaq")));

 Console::WriteLine("the items in the queue 'news' are");
  news->queue_display(head);
  
  Console::WriteLine("the items in the queue 'weather' are");
  weather->queue_display(head);
  
  usa->queue_display(head);

 Console::ReadLine();
}

waiting for your kind reply anxiuosly.

Edited 4 Years Ago by SAM2012: n/a

waiting for your kind reply anxiuosly.

My reply remains the same, the problem is exactly as speculated: your queue methods are nothing but glorified functions. They don't work with any internal state, so it's totally irrelevant which object you call queue_display() on; it'll still only display the contents of the argument.

Can you recommend some changes that every queue maintains its own head and tail. That is the main problem with this. Should I change the definition of queue class or functions of queue class. Please Help !

Infact this program is for one queue and if I wanna declare more queues I am wondering what should I have to do.

The queue class should maintain an internal state that doesn't need to be shared with the outside world. If you do that then every object will represent a completely different queue and you can create as many as you want without them overlapping:

struct ListNode
{
    ListNode(int value);
    
    int value;
    ListNode* next;
};

ListNode::ListNode(int value): value(value), next(nullptr) {}

class Queue
{
public:
    Queue();
    ~Queue();
    bool Empty() const;
    int Next() const;
    void Enqueue(int value);
    void Dequeue();
private:
    // Disable copying to keep the example simple
    Queue(Queue const& obj);
    
    ListNode* head;
    ListNode* tail;
};

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

#include <iostream>

using namespace std;

void FillQueue(Queue& q, int start, int finish)
{
    for (int i = start; i < finish; i++)
    {
        q.Enqueue(i);
    }
}

void DumpQueue(Queue& q)
{
    while (!q.Empty())
    {
        cout << q.Next() << ' ';
        q.Dequeue();
    }
    
    cout << endl;
}

int main()
{
    Queue q1, q2;
    
    FillQueue(q1, 0, 10);
    FillQueue(q2, 10, 15);

    DumpQueue(q1);
    DumpQueue(q2);
}

Thank you very much I would try to implement my program using this. If I wanna show the items in queue. what reference it should send as argument. I appreciate your reply. Thanks

If I wanna show the items in queue. what reference it should send as argument.

It shouldn't send any reference, barring an ostream reference if you want to write to something other than stdout. Such an operation would be a member function that works with the internal state but doesn't change it. Using my class as an example:

void Queue::DisplayAll() const
{
    for (ListNode* p = head; p; p = p->next)
    {
        cout << p->value << ' ';
    }
}

I have tried to write my queue class as yours given code but having lot of errors in it. One of important is
1. 'ListNode::ListNode(message ^)' : overloaded member function not found in 'ListNode'
See declaration of ListNode

struct ListNode
{
    ListNode(gcroot<message^> msg);    
     gcroot<message^> msg;    
    ListNode* next;
};

ListNode::ListNode(message^ msg): msg(msg), next(nullptr) {}

class Queue
{
public:
    Queue();
    ~Queue();
    bool Empty() const;
    int Next() const;
    void Enqueue(message^);
    void Dequeue();
	void DisplayAll();
private:
    // Disable copying to keep the example simple
    Queue(Queue const& obj);
    
    ListNode* head;
    ListNode* tail;
};

PLEASE HELP. Here message is a ref class.

Edited 4 Years Ago by SAM2012: n/a

Hi,

I have solved the query I gave above. Sorry about that. But now the only problem is this error.

"cannot access private member declared in class 'queue'". I wanted to use the objects of one class in another class. Infact the queue class worked well for me. Now I want to make a "queuelist class" having list of queue objects.

I made this class exactly in same way as queue class but now its giving above error. I declared queue class as "public" but problem is same. Again asking for help :(

Edited 4 Years Ago by SAM2012: n/a

I declared queue class as "public" but problem is same.

It's a member inside the queue class that's private and being accessed from outside of the class definition. I can't tell you exactly what it is because you didn't post your most recent code.

Thank you very much. I have solved it with your kind guidance. Can you guide me towards managed data string matching. actually I have a function doing simple wild-card string matching. But I want the same to work for managed strings. Any help would be highly appreciated.

This article has been dead for over six months. Start a new discussion instead.