Thursday, May 8, 2008

8 Puzzle Code - Queue.cpp

#include "stdafx.h"
#include "queue.h"

Queue::Queue(){
head=NULL;
tail=NULL;
}

BOOL Queue::isEmpty(){

return (head==NULL && tail==NULL);

}



void Queue::add(char* p, char* pp){

StateNode* newnode = new StateNode();
newnode->setPuzzle(p);
newnode->setParentState(pp);

if(isEmpty()){
head=newnode;
tail=head;
}
else{
tail->next=newnode;
tail=newnode;
}
}

StateNode* Queue::get(){
StateNode* temp;
temp=head;
head=head->next;
if(head==NULL) tail=NULL;
return temp;
}

void Queue::deleteNode(StateNode* n){
StateNode* curr=head;
while(curr!=NULL){
head=head->next;
delete curr;
curr=head;
}
}

Queue::~Queue(){
TRACE(_T("Delete Q...\n"));
deleteNode(head);
}

No comments: