Reputation:
I am working on a homework assignment and have everything almost complete. I am having trouble with a certain section in my code. I have tried a few things and can not seem to figure out what i have done wrong or what i need to do to get it to work.
I am trying to implement a queue that takes in a specific object called QueueData. When i execute the program it will run all the way up until line 44 in the P2Queue.cpp file. The intention at this point in the code is to add the new QueueData object to the queue at the tail and then set the pointer of temp pointing at null, signifying the end of the queue. The program stops running at line 44 in the file P2Queue.cpp. What am i doing wrong, what do i need to change, this is killing me.
Thanks in advance! If you need in further clarification, please let me know.
P2Queue.cpp
#include"P2Queue.h"
#include<iostream>
#include<string.h>
using namespace std;
P2Queue::P2Queue() //: head(NULL), tail(NULL), count(0)
{
head=NULL;
tail=NULL;
count=0;
}
P2Queue::~P2Queue()
{
QueueData* temp;
while(head!=NULL)
{
temp=head;
head=temp->next;
delete temp;
}
head=tail=NULL;
}
void P2Queue::Enqueue(int x, char *y)
{
cout<<"I'm HERE in the function call"<<endl;
QueueData* temp;
temp=new QueueData();
temp->num=x;
cout<<temp->num<<endl;
strcpy(temp->data, y);
cout<<temp->data<<endl;
temp->next=NULL;
cout<<"LALALA"<<endl;
tail->next=temp;
cout<<"I'm here11!!"<<endl;
tail=temp;
cout<<"I'm here!!"<<endl;
}
void P2Queue::Enqueue(QueueData* ptr)
{
tail->next=ptr;
tail=ptr;
tail->next=NULL;
}
QueueData* P2Queue::Dequeue()
{
QueueData* temp;
temp=new QueueData();
temp=head;
head=temp->next;
return temp;
}
int P2Queue::QueueSize()
{
QueueData* temp;
temp=head;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
delete temp;
return count;
}
QueueData* P2Queue::getHead()
{
return head;
}
QueueData* P2Queue::getTail()
{
return tail;
}
P2Queue.h
#ifndef P2QUEUE_H
#define P2QUEUE_H
struct QueueData
{
int num;
char data[128];
QueueData* next;
};
class P2Queue
{
private:
int count;
QueueData* head;
QueueData* tail;
public:
QueueData* getHead();
QueueData* getTail();
P2Queue();
~P2Queue();
void Enqueue(int x, char* y);
void Enqueue(QueueData* ptr);
QueueData* Dequeue();
int QueueSize();
};
#endif
BSSim.cpp
#include"BSSim.h"
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <time.h>
using namespace std;
BSSim::BSSim()
{
A = new P2Queue();
B = new P2Queue();
C = new P2Queue();
}
BSSim::~BSSim()
{
delete A;
delete B;
delete C;
}
bool BSSim::getNextLine(char *line, int lineLen)
{
bool done = false;
while(!done)
{
inFile.getline(line, lineLen); // Read a line from the file
// Note: inFile is a private class variable
if(inFile.good()) // If a line was successfully read
{
if(strlen(line) == 0) // Skip any blank lines
continue;
else if(line[0] == '#') // Skip any comment lines
continue;
else done = true; // Got a valid data line so return with this line
}
else
{
strcpy(line, ""); // Clear the buffer array
return false; // Flag end of file
}
} // end while
return true; // Flag a successful read
}
bool BSSim::runSimulation(char *cmdFile)
{
inFile.open(cmdFile, ifstream::in);
int x;
char ch;
char ch2;
char cArray[32];
char str[128];
struct _timeb tStruct;
double currenttime;
double nexttime=0;
bool done = false;
if(!inFile.is_open()) // If the file was not opened successfully, bail out.
{
// inFile.is_open() returns false if the file could not be found or
// if for some other reason the open failed.
cout << "Unable to open command file.\nProgram terminating.\n";
return 0;
}
else
{
while(!done)
{
_ftime(&tStruct);
currenttime = (tStruct.time*1000) + tStruct.millitm;
if(currenttime>=nexttime)
{
getNextLine(line, 128);
sscanf(line, "%s", cArray);
cout<<cArray<<endl;
cout<<"I'm here!"<<endl;
if(strcmp(cArray, "ENQUEUE")==0)
{
cout<<"I'm in the Enqueue if"<<endl;
sscanf(line, "%s %d %s", cArray, &x, str);
cout<<"HERE!"<<endl;
cout<<x<<" "<<str<<endl;
A->Enqueue(x, str);
cout<<"Enqueue A - ID="<<x<<", Data="<<str<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
else if(strcmp("DEQUEUE", cArray)==0)
{
sscanf(line, "%s %c", cArray, &ch);
if(ch=='A')
{
if(A->QueueSize()==0)
{
cout<<"Queue A is empty"<<endl;
}
else
{
sscanf(line, "%s %c %s %c", cArray, &ch, str, &ch2);
if(ch2=='B')
{
B->Enqueue(A->Dequeue());
cout<<"Dequeue from A into B - ID="<<B->getTail()->num<<", Data="<<B->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
else
{
C->Enqueue(A->Dequeue());
cout<<"Dequeue from A into C - ID="<<C->getTail()->num<<", Data="<<C->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
if(ch=='B')
{
if(B->QueueSize()==0)
{
cout<<"Queue B is empty"<<endl;
}
else
{
B->Dequeue();
cout<<"Dequeue from B - ID="<<B->getTail()->num<<", Data="<<B->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
else
{
if(C->QueueSize()==0)
{
cout<<"Queue C is empty"<<endl;
}
else
{
C->Dequeue();
cout<<"Dequeue from C - ID="<<C->getTail()->num<<", Data="<<C->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
}
else if(strcmp("NOACTION", cArray)==0)
{
cout<<"NO ACTION"<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
else
{
inFile.close();
cout<<"The simulation has terminated normally."<<endl;
return done;
}
}
}
}
}
BSSim.h
#ifndef BSSIM_H
#define BSSIM_H
#include"P2Queue.h"
#include<iostream>
#include<fstream>
using namespace std;
class BSSim
{
private:
P2Queue* A;
P2Queue* B;
P2Queue* C;
ifstream inFile;
char line[128];
public:
BSSim();
~BSSim();
bool runSimulation(char* cmdFile);
bool getNextLine(char* line, int lineLen);
};
#endif
#include"BSSim.h"
#include"P2Queue.h"
using namespace std;
void main()
{
BSSim x;
x.runSimulation("SimData.txt");
}
Example data file:
ENQUEUE 1234 QueueTest_01
ENQUEUE 2345 QueueTest_02
ENQUEUE 3456 QueueTest_03
ENQUEUE 4567 QueueTest_04
ENQUEUE 5678 QueueTest_05
ENQUEUE 6789 QueueTest_06
NOACTION
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE B
DEQUEUE C
NOACTION
Upvotes: 4
Views: 2346
Reputation: 34614
I believe your problem, like others have said, is that your program is not handling when the queue is empty. What you need to do is add a check into your function for if the queue is empty. If the queue is empty you need to handle the Enqueue a little differently. I believe this will fix your issue. Below is some pseudo code that will possibly help you. Let me know if this helps! <3
void P2Queue::Enqueue(int x, char *y)
{
QueueData* temp;
temp=new QueueData();
temp->num=x;
strcpy(temp->data, y);
temp->next=NULL;
if(queue is empty) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
}
void P2Queue::Enqueue(QueueData* ptr)
{
ptr->next = NULL
if(queue is empty) {
head = ptr;
tail = ptr;
}
else {
tail->next = ptr;
tail = ptr;
}
}
Upvotes: 0
Reputation: 856
From what I can see you implementation of the queue is complete wrong. You can find information on how to implement it here: http://en.wikipedia.org/wiki/Queue_(data_structure).
For ex: this doesn't make any sense
void P2Queue::Enqueue(QueueData* ptr)
{
tail->next=ptr;
tail=ptr;
tail->next=NULL;
}
First it will crush or do something strange when the list is empty. Second it adds just one element and removes all existing elements from the queue.
it should be at least like this:
void P2Queue::Enqueue(QueueData* ptr)
{
ptr->next = tail;
tail = ptr;
}
Upvotes: 1
Reputation: 20664
Initially the queue is empty and head == tail == NULL.
What happens you enqueue the first item?
Upvotes: 1