You are here:

C++/Data structure using C++

Advertisement


Question
I am not sure how to pay. so let me know.I see some declined , does that mean they counldnt solove it or help ? I am studying this on my own as I am on disability.....
Hope this code comes out correct.  
The textbook: Data Structures Using C++ by D. S. Malik, ISBN: 0-619-15907-3
(site: http://www.course.com/catalog/product.cfm?isbn=0-619-15907-3 )

The textbook's question is (Chap 8 Programming Exercise. 3, pg. 522): "The array implementation of queues given in this chapter uses a special array slot - a reserved slot- to distinguish between an empty queue and a full queue.* Write the array implementation of queues as an ADT, in which a variable count is used to keep track of the number of elements in the queue.  Write the definitions of the function members of this queue design.  Also, write a program to test various operations on the queue.  
From the authors website: 2 code parts:  queuesAsArray.h & testProgQueueArray.cpp  I've attached both of the code snippets to this email

I'm not sure how to answer this question. Am I missing something in the author's code? Author's variable “count” is already being used to keep track of the “add's” and “deletes”.  What else is supposed to be done? And the BIG question… how? In this test this is the only concept giving me problems.  Now to your problem :

Specifically, I am stuck on how to do the process requested
Hope you can follow this code.
Jerry9126@yahoo.com
//Header file QueueAsArray

#ifndef H_QueueAsArray
#define H_QueueAsArray

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>
class queueType
{
public:
   const queueType<Type>& operator=(const queueType<Type>&);
       // overload the assignment operator

   bool isEmptyQueue();
     //Function to determine if the queue is empty.
     //Postcondition: returns true if the queue is empty;
     //          otherwise, it returns false.

   bool isFullQueue();
     //Function to determine if the queue is full.
     //Postcondition: returns true if the queue is full;
     //          otherwise, it returns false.

   void initializeQueue();
     //Function to initialize the queue to an empty state.
     //Postcondition: count = 0, queueFront = 0;
     //          queueRear = maxQueueSize - 1;
   void destroyQueue();
     //Function to remove all the elements from the queue.
     //Postcondition: count = 0, queueFront = 0;
     //          queueRear = maxQueueSize - 1;

   Type front();
     //Function to returns the first element of the queue.
     //Precondition: The queue exists and is not empty
      //Postcondition: If queue is empty, program terminates;
      //          otherwise the first element of the queue
      //          is returned  
   Type back();
     //Function to returns the last element of the queue.
     //Precondition: The queue exists and is not empty
      //Postcondition: If the queue is empty, program terminates;
      //          otherwise the last element of the queue
     //          is returned

  void addQueue(const Type& queueElement);
     //Function to add queueElement to the queue.
     //Precondition: The queue exists and is not full.
     //Postcondition: The queue is changed and queueElement
        //          is added to the queue.

  void deleteQueue();
     //Function to remove the first element of the queue.
     //Precondition: The queue exists and is not empty.
     //Postcondition: The queue is changed and the first
     //          element is removed from the queue.

   queueType(int queueSize = 100);
     //constructor
   queueType(const queueType<Type>& otherQueue);
      // copy constructor
   ~queueType();
      //destructor

private:
   int maxQueueSize;
   int count;
   int queueFront;
   int queueRear;
   Type *list;     //pointer to the array that holds
         //the queue elements
};


template<class Type>
void queueType<Type>::initializeQueue()
{
  queueFront = 0;
   queueRear = maxQueueSize - 1;
  count = 0;
}


template<class Type>
void queueType<Type>::destroyQueue()
{
  queueFront = 0;
   queueRear = maxQueueSize - 1;
  count = 0;
}


template<class Type>
bool queueType<Type>::isEmptyQueue()
{
  return(count == 0);
}

template<class Type>
bool queueType<Type>::isFullQueue()
{
  return(count == maxQueueSize);
}


template<class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
  if(!isFullQueue())
  {   
     queueRear = (queueRear + 1) % maxQueueSize; //use mod operator
         //to advance queueRear because
         //the array is circular
        count++;
        list[queueRear] = newElement;
  }
  else
       cerr<<"Cannot add to a full queue"<<endl;
}


template<class Type>
Type queueType<Type>::front()
{
  assert(!isEmptyQueue());
  return list[queueFront];
}


template<class Type>
Type queueType<Type>::back()
{
    assert(!isEmptyQueue());
    return list[queueRear];
}


template<class Type>
void queueType<Type>::deleteQueue()
{
  if(!isEmptyQueue())
  {   
     count--;
        queueFront = (queueFront + 1) % maxQueueSize; //use the mod
         //operator to advance queueFront
         //because the array is circular
  }
  else
     cerr<<"Cannot remove from an empty queue"<<endl;
}


//constructor
template<class Type>
queueType<Type>::queueType(int queueSize)   
{
   if(queueSize <= 0)
   {
     cerr<<"Size of the array to hold the queue must "
        <<"be positive."<<endl;
     cerr<<"Creating an array of size 100."<<endl;

     maxQueueSize = 100;
   }
   else
        maxQueueSize = queueSize;  //set maxQueueSize to queueSize

   queueFront = 0;          //initialize queueFront
   queueRear = maxQueueSize - 1;       //initialize queueRear
   count = 0;
   list = new Type[maxQueueSize];  //create the array to
         //hold the queue elements
  assert(list != NULL);
}


template<class Type>
queueType<Type>::~queueType()   //destructor
{
  delete [] list;
}

template<class Type>
const queueType<Type>& queueType<Type>::operator=
         (const queueType<Type>& otherQueue)
{
  cout<<"Write the definition of the function "
     <<"to overload the assignment operator"<<endl;
}

template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
  cout<<"Write the definition of the copy constructor"<<endl;
}


#endif

==================================
//Test Program Queue as Array

#include <iostream>
#include "queueAsArray.h"

using namespace std;

int main()
{
  queueType<int> queue;
  int x, y;

  queue.initializeQueue();
  x = 4;
  y = 5;
  queue.addQueue(x);
  queue.addQueue(y);
  x = queue.front();
  queue.deleteQueue();
  queue.addQueue(x + 5);
  queue.addQueue(16);
  queue.addQueue(x);
  queue.addQueue(y - 3);

  cout<<"Queue Elements: ";

  while(!queue.isEmptyQueue())
  {
     cout<<" "<<queue.front();
     queue.deleteQueue();
     
  }

  cout<<endl;

  return 0;
}

==============
If you can understand this I can go to $10 if that is what you ask . As I see your fees $5 ..I am on disability and taking on line courses;s funds are short.I cant get it to complie and not sure what is wrong


Answer
Hello jerry, thank you for the question.

The code you have provided is very clean and concise. It adheres to the quote from the book. It uses a variable to keep track of its place in a queue, which is implemented as an array. I was able to cut and paste the code into my compiler and run it without a problem. So what exactly is the problem? I'm not too clear on your actual question.

If they mean to implement it as an array, do you have to overload the [] operator so you can index the queue like a normal array? That wouldn't be hard at all:

Type& queueType::operator [] (const int rhs)
{
return list[rhs];
}

That way you can do:

cout << queue[2]

When using it in code in the test program. What operating system and compiler are you using that can't compile that code? What errors are you getting? If you could let me know I'll be more than happy to help.

Also, AllExperts is completely free. There is no charge or fee for asking questions.

I hope to hear back from you.

- Eddie  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.