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