I was trying to implement a queue using linked list and the problem is when i try to call dequeue() method, nothing shows up and no errors too. just NULL values for all nodes. take a look @ this code and tell me if there is anything wrong...

ps. Do i have to call the Linked list call when implementing the Queue?
how can I access the same Queue using many windows forms?
please help me b/c i am new to this field

--MQueue.cpp--

#include "stdafx.h"
#include "LLQueue.h"

void LnkQueue::enqueue(int id, char *str,char *state, int priority)
{//Add new node to rear
   LinkNode* newNode= new LinkNode(id,str,state,priority);//Create a node for new element
    if(isEmpty())
        front = newNode;//Q is empty
	//q.insertFront(id,str,state,priority);
    else
        rear = newNode;//Q isn't empty
	//q.insertBack(id,str,state,priority);
}

int LnkQueue::dequeue()
{//Remove a node from front
    if(isEmpty())
    {	
        MessageBox::Show("Queue is empty","Error",MessageBoxButtons::OK,MessageBoxIcon::Error);
        return 0;
    }
    else
    {
       LinkNode *temp = front;
	   front = front->next;
		delete temp;
		return 1;
    }
}

LinkNode* LnkQueue::peekFront(){
    if(isEmpty())
    {	
        MessageBox::Show("Queue is empty","Error",MessageBoxButtons::OK,MessageBoxIcon::Error);
        return NULL;
    }
    else
    {
        return front;
    }
}

-----------------
LLQueue.h

#include <iostream>
#include <string.h>

using namespace std;
using namespace System::Windows::Forms;
using namespace System;

class LinkNode
{
public:
	int job_id; 
	char jstr[100];
	int priority;
	char jstatus[20];
	LinkNode * next;//pointer to next node 
	LinkNode * prev;//pointer to previous node 
    LinkNode(int id, char *str,char *state, int ppriority);
	LinkNode();
};

class LinkedList
{
public:
	LinkNode * firstNode;       //pointer to front of list   
	LinkNode * lastNode;        //pointer to rear of list  
	
LinkedList()
{
	firstNode = NULL;
	lastNode = NULL;
}

void insertFront(int Jid, char *str,char *state, int Jpriority); 
void insertBack(int Jid, char *str,char *state, int Jpriority);
void insertBefore(int id, char *str,char *state, int priority, LinkNode *nodeB, LinkNode * newNode) ;
void insertAfter(int id,char *str,char *state, int priority, LinkNode *nodeA, LinkNode * newNode) ; 

//Removing a node is easier, only requiring care with the firstNode and lastNode:
LinkNode * removeFront();
LinkNode * removeBack();//FIFO Mode - no use f removing from back

LinkNode *  removeNode(LinkNode *newNode);

void printDListFront();
void printDListBack();
};

class LnkQueue
{
private:
    LinkNode* front; //Pointer to 1st node
    LinkNode* rear; //Pointer to last node
    int nItems;
public:
	LnkQueue(){ //Constructor
        front = NULL;
        rear = NULL;
	    nItems = 0;
    }

    void enqueue(int id, char *str,char *state, int priority);
	int dequeue();
    LinkNode* peekFront();
   
    bool isEmpty(){
        return (front == NULL);
    }
   
   ~LnkQueue()//Destructor
    {
        LinkNode * current;
        while(current == NULL){
            current = front->next;
            delete front;
            front = current;
        }
    }
};
------LLQueue.cpp-----
#include "stdafx.h"
#include "LLQueue.h"

LinkNode::LinkNode(int id, char *str,char *state, int ppriority)
	{
		job_id = id;
		strcpy_s(jstr, str);
		strcpy_s(jstatus,state);
		priority = ppriority;
	}
//insert a node before the front node 
void LinkedList::insertFront (int id,char *str,char *state, int priority)
{
	LinkNode *newNode;
	newNode = new LinkNode(id,str,state,priority);
    if(this->firstNode==NULL)
	{
		this->firstNode = newNode;
		this->lastNode  = newNode;
		newNode->prev = NULL;
		newNode->next = NULL;
	}
	else
		insertBefore(id, str, state, priority, this->firstNode,newNode );
}

//insert a node after the last node 
void LinkedList::insertBack(int Jid, char *str,char *state, int Jpriority)
{   	LinkNode *newNode;
	newNode = new LinkNode(Jid,str,state,Jpriority);


	if(this->lastNode==NULL)
		insertFront(Jid, str, state, Jpriority);
	else
		insertAfter(Jid, str, state, Jpriority, this->lastNode,newNode);
}

//insert a node before nodeB
void LinkedList::insertBefore(int id, char *str,char *state, int priority, LinkNode *nodeB, LinkNode * newNode)    
{
	newNode = new LinkNode(id,str,state,priority);
	newNode->prev=nodeB->prev;
	newNode->next =nodeB;
	if(nodeB->prev==NULL)
        this->firstNode =newNode;	
	else
		nodeB->prev->next=newNode;
	nodeB->prev=newNode;
}
//insert a node after  nodeB
void LinkedList::insertAfter(int id,char *str,char *state, int priority, LinkNode *nodeA, LinkNode * newNode)
{
	newNode = new LinkNode(id,str,state,priority);
	newNode->prev = nodeA;
	newNode->next = nodeA->next ;
	if(nodeA->next==NULL)
		this->lastNode = newNode;	
	else
		nodeA->next = newNode;
	nodeA->next = newNode;
}

//remove the front node 

LinkNode * LinkedList::removeFront()
{
    return removeNode(this->firstNode);
}


//remove a back node 
LinkNode * LinkedList::removeBack  ()
{
    return removeNode(this->lastNode);
}

LinkNode * LinkedList::removeNode(LinkNode *nodeToRemove)
{
	if(nodeToRemove->prev == NULL)
        this->firstNode = nodeToRemove->next;
	else 
		nodeToRemove->prev->next = nodeToRemove->next;

	if (nodeToRemove->next == NULL)
		this->lastNode = nodeToRemove->prev;
	else
		nodeToRemove->next->prev = nodeToRemove->prev;
    return nodeToRemove;
	delete nodeToRemove;

}

I don't see where your enqueue function sets the next and prev members of the node.

When the queue is empty, front and rear should both be NULL.

When you add the first node, front and rear should point to the new node and its next and prev should be NULL.

When you add the second node, the next from the first node should point to the new node and the queue's rear should point to the new node. If you're going to keep the list doubly linked (with both next and prev pointers) the prev pointer for the new node should point to the previously added node.

Then when you 'dequeue' the first node, the front pointer can be set to the 'next' pointer from the node you are removing.

I'm not sure I'm being clear, but the point is that as written, your first node never gets a pointer to the next node (which is what forms the linked list). So there is no 'link' to the next node when you go to remove it.

Hope that helps some, if not, ask for more details and I'll try to explain it better.

I don't see where your enqueue function sets the next and prev members of the node.

When the queue is empty, front and rear should both be NULL.

When you add the first node, front and rear should point to the new node and its next and prev should be NULL.

When you add the second node, the next from the first node should point to the new node and the queue's rear should point to the new node. If you're going to keep the list doubly linked (with both next and prev pointers) the prev pointer for the new node should point to the previously added node.

Then when you 'dequeue' the first node, the front pointer can be set to the 'next' pointer from the node you are removing.

I'm not sure I'm being clear, but the point is that as written, your first node never gets a pointer to the next node (which is what forms the linked list). So there is no 'link' to the next node when you go to remove it.

Hope that helps some, if not, ask for more details and I'll try to explain it better.

I think i saw the problem ....thanks a lot Murtan.
after little thinking, I have realized that I really don't need Queue Class for the Queue Implementation, so I used both Linked List and Node classes and I now have a perfectly working Linked List Queue.

and Thanks again, I could understand your explanation. but there is a little problem.

How can I declare an Global Object in Windows Forms? (which I can access from many forms)

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.