I have design two program and both have errors on it. the first is supposed to handle queue on integer, double, string and date. I have done for integer and double. When I compile it there was error on it. If you can just help me debug it and i will complete the string and date myself. i have to submit this by Thursday. below is the code for it:
//listnd.h
//ListNode template definition
#ifndef LISTND_H
#define LISTND_H
template< class NODETYPE > class List; //forward declaration
template<class NODETYPE>
class ListNode {
friend class List< NODETYPE >; //make List a friend
public:
ListNode( const NODETYPE & ); //constructor
NODETYPE getData() const; //return data in the node
private:
NODETYPE data; //data
ListNode< NODETYPE > *nextPtr; //next node in the list
};
//Constructor
template<class NODETYPE>
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
: data( info ), nextPtr( 0 ) { }
//Return a copy of the data in the node
template< class NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const { return data; }
#endif
//-------------------------------------------------------------------------
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <cassert>
#include "listnd.h"
using std::cout;
template< class NODETYPE >
class List {
public:
List(); //constructor
~List(); //destructor
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
ListNode< NODETYPE > *firstPtr; //pointer to first node
ListNode< NODETYPE > *lastPtr; //pointer to last node
//Utility function to allocate a new node
ListNode< NODETYPE > *getNewNode( const NODETYPE & );
};
//Default constructor
template< class NODETYPE >
List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { }
//Destructor
template< class NODETYPE >
List< NODETYPE >::~List()
{
if( !isEmpty() ) { //List is not empty
cout << "Destroying nodes...\n";
ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr;
while ( currentPtr != 0 ) { //delete remaining nodes
tempPtr = currentPtr;
cout << tempPtr->data << '\n';
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}
cout << "All nodes destroyed\n\n";
}
//Insert a node at the front of the list
template< class NODETYPE >
void List< NODETYPE>::insertAtFront( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );
if ( isEmpty() ) //List is empty
firstPtr = lastPtr = newPtr;
else { //List is not empty
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}
//Insert a node at the back of the list
template< class NODETYPE >
void List< NODETYPE >::insertAtBack( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );
if (isEmpty() ) //List is empty
firstPtr = lastPtr = newPtr;
else { //List is not empty
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}
//Delete a node from the front of the list
template< class NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if ( isEmpty() ) //List is empty
return return; //delete unsuccessful
else {
ListNode< NODETYPE > *tempPtr = firstPtr;
if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;
value = tempPtr->data; //data being removed
delete tempPtr;
return; //delete successful
}
}
//Delete a node from the back of the list
template< class NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if ( isEmpty() )
return return; //delete succesful
else {
ListNode< NODETYPE > *tempPtr = lastPtr;
if ( firstPtr == lastPtr )
firstPtr = LastPtr = 0;
else {
ListNode< NODETYPE > *currentPtr = firstPtr;
while ( currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr;
lastPtr = currentPtr;
currentPtr->nextPtr = 0;
}
value = tempPtr->data;
delete tempPtr;
return return; //delete succesful
}
}
//Is the List empty
template< class NODETYPE >
bool List< NODETYPE >::isEmpty() const
{ return firstPtr ==0; }
//return a pointer to a newly allocated node
template < class NODETYPE >
ListNode< NODETYPE > *List< NODETYPE >::getNewNode(
const NODETYPE &value )
{
ListNode< NODETYPE > *ptr =
new ListNode< NODETYPE >( value );
assert( ptr != 0 );
return ptr;
}
//Display the contents of the List
template< class NODETYPE >
void List< NODETYPE >::print() const
{
if ( isEmpty() ) {
cout << "The list is empty\n\n";
return;
}
ListNode< NODETYPE > *currentPtr = firstPtr;
cout << "The list is: ";
while ( currentPtr != 0 ) {
cout << currentPtr->data << ' ';
currentPtr = currentPtr->nextPtr;
}
cout << "\n\n";
}
#endif
//-------------------------------------------------------------------------
#ifndef QUEUE_H
#define QUEUE_H
#include "list.h"
template< class QUEUETYPE >
class Queue: private List< QUEUETYPE > {
public:
void enqueue( const QUEUETYPE &d ) { insertAtBack( d ); }
bool dequeue( QUEUETYPE &d )
{ return removeFromFront( d ); }
bool isQueueEmpty() const { return isEmpty(); }
void printQueue() const { print(); }
};
#endif
//-------------------------------------------------------------------------
#include <iostream>
#include "queue.h"
using std::endl;
int main()
{
Queue< int > intQueue;
int dequeueInteger, i;
cout << "processing an integer Queue" << endl;
for ( i = 0; i < 4; i++ ) {
intQueue.enqueue( i );
intQueue.printQueue();
}
while ( !intQueue.isQueueEmpty() ) {
intQueue.dequeue( dequeueInteger );
cout << dequeueInteger << " dequeued" << endl;
intQueue.printQueue();
}
Queue< double > doubleQueue;
double val = 1.1, dequeueDouble;
cout << "processing a double Queue" << endl;
for ( i = 0; i < 4; i++ ) {
doubleQueue.enqueue( val );
doubleQueue.printQueue();
val += 1.1;
}
while ( !doubleQueue.isQueueEmpty() ) {
doubleQueue.dequeue( dequeueDouble );
cout << dequeueDouble << " dequeued" << endl;
doubleQueue.printQueue();
}
return 0;
}
The second problem is a program that is supposed to do a quicksort on a set of array type double and later do a bsearch. I actually modified the actual program into a class file. I'm actually weak at class and i hope you can help me. Below is the code:
#include <iostream.h>
#include <algorithm>
//class quicksort
class qsort
{
public:
int compare(const void*, const void*);
//qsort (list, 10, sizeof(int), compare); //error
public:
int list;
int element;
};
//class bsearch
class bsearch
{
public:
int compare(const void*, const void*);
public:
double list;
int element;
double key;
};
int main()
{
double list[10] = {9.2,5.1,2.6,8.5,1.7,7.1,3.2,6.3,4.6,0.1};
double key = 3;
//display unsorted list
for (int element = 0; element <= 9; element++)
{
cout << list[element] << " ";
}
cout << endl;
qsort (list, 10, sizeof(int), compare); //error
//display sorted list
for (element = 0; element <= 9; element++)
{
cout << list[element] << " ";
}
//displays whether the number in the array or not
/*if (bsearch(&key,list,10,sizeof(int),compare)) //error
{
cout<<key<<" is in the array"<<endl; //number in array
}
else
{
cout << key <<" is NOT in the array2" <<endl; //number not in array
}*/
return 0;
}
int compare1(const void* a_PTR, const void* b_PTR)
{
return *((int*)a_PTR) - *((int*)b_PTR);
}
Thank you for your help.