Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

LQueue.cpp Syntax Check

Posted on 2006-05-21
1
Medium Priority
?
511 Views
Last Modified: 2008-02-26

// lQueue.cpp

#include <iostream>

using namespace std;

typedef int QueueElement;

class Queue {

public:

      Queue();
      Queue(const Queue& original);
      ~Queue();
      const Queue& operator = (const Queue& rightHandSide);
      bool empty() const;
      bool fullQueue();
      int sizeQueue();
      void enqueue(const QueueElement& value);
      void display(ostream& out) const;
      QueueElement front() const;
      QueueElement nthElement(int);
        void dequeue();
      QueueElement backQueue();

private:

      class Node {

      public:

            QueueElement data;
            Node* next;
            Node(QueueElement value, Node* link = 0): data(value), next(link){}

      };
      typedef Node* NodePointer;

      NodePointer myFront, myBack;

};

Queue::Queue(): myFront(0), myBack(0){}

Queue::Queue(const Queue& original) {

      myFront = myBack = 0;
      if(!original.empty()) {
            myFront = myBack = new Queue::Node(original.front());
            Queue::NodePointer origPtr = original.myFront->next;

            while(origPtr != 0) {
                  myBack->next = new Queue::Node(origPtr->data);
                  myBack = myBack->next;
                  origPtr = origPtr->next;

            }
      }

}

Queue::~Queue() {

      Queue::NodePointer prev = myFront, ptr;

      while(prev != 0) {
            ptr = prev->next;
            delete prev;
            prev = ptr;

      }

}

const Queue& Queue::operator = (const Queue& rightHandSide) {

      if(this != &rightHandSide) {
            this->~Queue();
            if(rightHandSide.empty()) myFront = myBack = 0;
            else {
                  myFront = myBack = new Queue::Node(rightHandSide.front());
                  Queue::NodePointer rhsPtr = rightHandSide.myFront->next;
                  while(rhsPtr != 0) {
                        myBack->next = new Queue::Node(rhsPtr->data);
                        myBack = myBack->next;
                        rhsPtr = rhsPtr->next;

                  }
            }

      }
      return *this;

}

bool Queue::empty() const {

      return(myFront == 0);

}

void Queue::enqueue(const QueueElement& value) {

      Queue::NodePointer newptr = new Queue::Node(value);
      if(empty()) myFront = myBack = newptr;
      else {
            myBack->next = newptr;
            myBack = newptr;

      }

}

void Queue::display(ostream& out) const {

      Queue::NodePointer ptr;
      for(ptr = myFront; ptr != 0; ptr = ptr->next)
            out << ptr->data << " " << endl;

}

QueueElement Queue::front() const {

      if(!empty())
            return(myFront->data);
      else {
            cerr << "Queue is empty, returning garbage. \n";
            QueueElement* temp = new(QueueElement);
            QueueElement garbage = *temp;
            delete temp;
            return garbage;

      }

}

void Queue::dequeue() {

      if(!empty()) {

            Queue::NodePointer ptr = myFront;
            myFront = myFront->next;
            delete ptr;
            if(myFront == 0) myBack = 0;

      }
      else
            cerr << " Queue is empty, can not remove value. \n";

}

QueueElement Queue::backQueue() {  // need checked for Linked List

      if(!empty()){
            Queue::NodePointer tempPtr = myBack;
   
            while(tempPtr->next != NULL) {
                  tempPtr = tempPtr->next;
   
                  return tempPtr->data;
            }
 
      }
      return 0;
}

QueueElement Queue::nthElement(int n) {  // need checked for Linked List

      if(!empty()) {
    int ctr = 1;
      Queue::NodePointer tempPtr1 = myFront;
            while(tempPtr1->next != NULL && ctr < n) {
        tempPtr1 = tempPtr1->next;
        ctr++ ;
     
            }
      }
    QueueElement any = 0;
    return any;
      
}

bool Queue::fullQueue() { // need checked for Linked List

      NodePointer ptr = new Node();

      if(ptr == 0) return true;
      delete ptr;
      return false;

}

int Queue::sizeQueue() {  // need checked for Linked List

            int count = 0;
            Node* p = myFront;

            while(p != NULL) {
                  count++;
                  p = p->next;

            }
            return count;

}

void print(Queue& q){q.display(cout);}

int main() {

      Queue q1;
      cout << "Queue created. Empty? " << boolalpha << q1.empty() << endl;

      cout << "How many elements to add to the queue? ";
      int numItems;
      cin >> numItems;

      for(int i = 1; i <= numItems; i++)
            q1.enqueue(100*1);

      cout << "Contents of queue q1 (via print):\n";
      print(q1); cout << endl;

      Queue q2;
      q2 = q1;
      cout << "Contents of queue q2 after q2 = q1 (via print):\n";
      print(q2); cout << endl;

      cout << "Queue q2 empty? " << q2.empty() << endl;
      cout << "Front value in q2: " << q2.front() << endl;

      while(!q2.empty()) {
            cout << "Remove front -- Queue contents: ";
            q2.dequeue();
            q2.display(cout);

      }
      cout << "Queue q2 empty? " << q2.empty() << endl;
      cout << "Front value in q2? " << endl << q2.front() << endl;
      cout << "Trying to remove front of q2: " << endl;
      q2.dequeue();

      return 0;
      system("pause");

}

output:

lQueue.cpp
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\lqueue.cpp(187) : error C2512: 'Queue::Node' : no appropriate default constructor available
Build log was saved at "file://c:\Documents and Settings\Enrique\My Documents\Visual Studio 2005\Projects\Lab 6\Lab 6\Debug\BuildLog.htm"
Lab 6 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========





0
Comment
Question by:edelossantos
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 16730369
Well, in

bool Queue::fullQueue() { // need checked for Linked List

     NodePointer ptr = new Node(); // <--- here

     if(ptr == 0) return true;
     delete ptr;
     return false;

}

'Node' does not have a default constructor. Anyway, the implementation does not make sense, since it always will return true except when the memory is completely exhausted. If that is what you want, use

bool Queue::fullQueue() { // need checked for Linked List

     NodePointer ptr = new Node(0);

     if(ptr == 0) return true;
     delete ptr;
     return false;

}

Here's the whole thing:

// lQueue.cpp

#include <iostream>

using namespace std;

typedef int QueueElement;

class Queue {

public:

     Queue();
     Queue(const Queue& original);
     ~Queue();
     const Queue& operator = (const Queue& rightHandSide);
     bool empty() const;
     bool fullQueue();
     int sizeQueue();
     void enqueue(const QueueElement& value);
     void display(ostream& out) const;
     QueueElement front() const;
     QueueElement nthElement(int);
       void dequeue();
     QueueElement backQueue();

private:

     class Node {

     public:

          QueueElement data;
          Node* next;
          Node(QueueElement value, Node* link = 0): data(value), next(link){}

     };
     typedef Node* NodePointer;

     NodePointer myFront, myBack;

};

Queue::Queue(): myFront(0), myBack(0){}

Queue::Queue(const Queue& original) {

     myFront = myBack = 0;
     if(!original.empty()) {
          myFront = myBack = new Queue::Node(original.front());
          Queue::NodePointer origPtr = original.myFront->next;

          while(origPtr != 0) {
               myBack->next = new Queue::Node(origPtr->data);
               myBack = myBack->next;
               origPtr = origPtr->next;

          }
     }

}

Queue::~Queue() {

     Queue::NodePointer prev = myFront, ptr;

     while(prev != 0) {
          ptr = prev->next;
          delete prev;
          prev = ptr;

     }

}

const Queue& Queue::operator = (const Queue& rightHandSide) {

     if(this != &rightHandSide) {
          this->~Queue();
          if(rightHandSide.empty()) myFront = myBack = 0;
          else {
               myFront = myBack = new Queue::Node(rightHandSide.front());
               Queue::NodePointer rhsPtr = rightHandSide.myFront->next;
               while(rhsPtr != 0) {
                    myBack->next = new Queue::Node(rhsPtr->data);
                    myBack = myBack->next;
                    rhsPtr = rhsPtr->next;

               }
          }

     }
     return *this;

}

bool Queue::empty() const {

     return(myFront == 0);

}

void Queue::enqueue(const QueueElement& value) {

     Queue::NodePointer newptr = new Queue::Node(value);
     if(empty()) myFront = myBack = newptr;
     else {
          myBack->next = newptr;
          myBack = newptr;

     }

}

void Queue::display(ostream& out) const {

     Queue::NodePointer ptr;
     for(ptr = myFront; ptr != 0; ptr = ptr->next)
          out << ptr->data << " " << endl;

}

QueueElement Queue::front() const {

     if(!empty())
          return(myFront->data);
     else {
          cerr << "Queue is empty, returning garbage. \n";
          QueueElement* temp = new(QueueElement);
          QueueElement garbage = *temp;
          delete temp;
          return garbage;

     }

}

void Queue::dequeue() {

     if(!empty()) {

          Queue::NodePointer ptr = myFront;
          myFront = myFront->next;
          delete ptr;
          if(myFront == 0) myBack = 0;

     }
     else
          cerr << " Queue is empty, can not remove value. \n";

}

QueueElement Queue::backQueue() {  // need checked for Linked List

     if(!empty()){
          Queue::NodePointer tempPtr = myBack;
   
          while(tempPtr->next != NULL) {
               tempPtr = tempPtr->next;
   
               return tempPtr->data;
          }
 
     }
     return 0;
}

QueueElement Queue::nthElement(int n) {  // need checked for Linked List

     if(!empty()) {
    int ctr = 1;
     Queue::NodePointer tempPtr1 = myFront;
          while(tempPtr1->next != NULL && ctr < n) {
        tempPtr1 = tempPtr1->next;
        ctr++ ;
     
          }
     }
    QueueElement any = 0;
    return any;
     
}

bool Queue::fullQueue() { // need checked for Linked List

     NodePointer ptr = new Node(0);

     if(ptr == 0) return true;
     delete ptr;
     return false;

}

int Queue::sizeQueue() {  // need checked for Linked List

          int count = 0;
          Node* p = myFront;

          while(p != NULL) {
               count++;
               p = p->next;

          }
          return count;

}

void print(Queue& q){q.display(cout);}

int main() {

     Queue q1;
     cout << "Queue created. Empty? " << boolalpha << q1.empty() << endl;

     cout << "How many elements to add to the queue? ";
     int numItems;
     cin >> numItems;

     for(int i = 1; i <= numItems; i++)
          q1.enqueue(100*1);

     cout << "Contents of queue q1 (via print):\n";
     print(q1); cout << endl;

     Queue q2;
     q2 = q1;
     cout << "Contents of queue q2 after q2 = q1 (via print):\n";
     print(q2); cout << endl;

     cout << "Queue q2 empty? " << q2.empty() << endl;
     cout << "Front value in q2: " << q2.front() << endl;

     while(!q2.empty()) {
          cout << "Remove front -- Queue contents: ";
          q2.dequeue();
          q2.display(cout);

     }
     cout << "Queue q2 empty? " << q2.empty() << endl;
     cout << "Front value in q2? " << endl << q2.front() << endl;
     cout << "Trying to remove front of q2: " << endl;
     q2.dequeue();

     return 0;
     system("pause");

}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question