Trouble with Int Queue
From: Barry & Angela Hynes (base_at_isllandtelecom.com)
Date: 05/24/04
- Previous message: Edo: "Debugging with jGRASP"
- Next in thread: Leor Zolman: "Re: Trouble with Int Queue"
- Reply: Leor Zolman: "Re: Trouble with Int Queue"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 24 May 2004 00:50:16 GMT
Good Day Folks,
The following is the code for an integer queue using a namespace. it compile
ok but i do not get the output that i expected...any help greatly
appreciated
thnaks
Barry
//*****************************************************
#ifndef INTQUEUE_H
#define INTQUEUE_H
namespace queueNamespace{
class IntQueue{
public:
IntQueue(int = 20);
IntQueue(const IntQueue&);
~IntQueue();
IntQueue& operator=(const IntQueue&);
void enQueue(int) throw (logic_error);
int deQueue() throw (logic_error);
int firstInLine() throw (logic_error);
int size() throw (logic_error);
private:
int* queue_;
int size_;
int front_;
int back_;
};
}
#endif
//****************************************
//file: intQueue.cpp
#include <iostream.h>
using namespace std;
#include "intQueue.h"
using namespace queueNamespace;
//IntQueue class definitions.
IntQueue::IntQueue(int size){
queue_ = new int[size];
size_ = size;
front_ = 0;
back_ = 0;
for(int i = 0; i <= size_; ++i)
queue_[i] = 0;//initializing array slots to 0
}
IntQueue::IntQueue(const IntQueue& s){
front_ = s.front_;
back_ = s.back_;
size_ = s.size_;
for(int i = 0; i <= size_; ++i)
queue_[i] = s.queue_[i];
}
IntQueue& IntQueue::operator=(const IntQueue& s){
if(this == &s)
return *this;
if(size_ != s.size_){
delete [] queue_;
queue_ = new int[s.size_];
}
front_ = s.front_;
back_ = s.back_;
size_ = s.size_;
for(int i = 0; i <= size_; ++i)
queue_[i] = s.queue_[i];
return *this;
}
IntQueue::~IntQueue(){
delete [] queue_;
}
void IntQueue::enQueue(int i) throw (logic_error){
if(back_ == size_ - 1)
throw logic_error("Queue Full...");
queue_[++back_] = i;
}
int IntQueue::deQueue() throw (logic_error){
if(back_ == 0)
throw logic_error("Queue Empty...");
return queue_[front_--];
}
int IntQueue::firstInLine() throw (logic_error){
if(back_ == 0)
throw logic_error("Queue Empty...");
return queue_[front_];
}
int IntQueue::size() throw (logic_error){
if(size_ < 0)
throw logic_error("Invalid Size...");
return size_;
}
//****************************************
#include <iostream.h>
using namespace std;
#include "intQueue.h"
using namespace queueNamespace;
int main() {
queueNamespace::IntQueue s;
try{
s.enQueue(115);
s.enQueue(10);
s.enQueue(15);
s.enQueue(20);
s.enQueue(25);
}
catch(const bad_alloc&){
cout << "run out of memory" << endl;
}
catch(const logic_error& e) {
cout << e.what() << endl;
}
try{
for(int x = 0; x <= 5; x++)
cout << "Slot " << x << ": " << s.deQueue() << endl;
}
catch(const bad_alloc&){
cout << "run out of memory" << endl;
}
catch(const logic_error& e) {
cout << e.what() << endl;
}
};
I added an initialization to the constructor to set all the elements of the
array to 0...but it does not seem to work.
only for element 0...not sure why???
any help greatly appreciated
- Previous message: Edo: "Debugging with jGRASP"
- Next in thread: Leor Zolman: "Re: Trouble with Int Queue"
- Reply: Leor Zolman: "Re: Trouble with Int Queue"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|