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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Circ Queue Syntax Check


// circ.cpp

#include <iostream>
#include <fstream>

using namespace std;

typedef int QueueElement;

class Queue {

private:

class Node {

public:

      QueueElement data;
      Node *next;
      Node():next(0){} // inline initializer
      Node(QueueElement dataValue): data(dataValue), next(0){}

};

typedef Node *next;
typedef Node *NodePointer;

public:

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

      void display(NodePointer ptr, ostream &out) const;
      NodePointer myFront, myBack;

};

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

Queue::Queue(const Queue &original) {

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

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

            }

      }

}

Queue::~Queue() {

      Queue::NodePointer prev = myBack, 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.first());
                  Queue::NodePointer rhsPtr = rightHandSide.myBack->next;
                  while(rhsPtr != 0) {
                        myBack->next = new Queue::Node(rhsPtr->data);
                        myBack = myBack->next;
                        rhsPtr = rhsPtr->next;

                  }

            }

      }
      return *this;

}

bool Queue::empty() const {

      return myBack == 0;

}

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

      NodePointer ptr = new Node(dataVal);

      if(ptr == 0) {
            cout << "Out of memory. " << endl;

      }
      if(myBack == 0) {
            myBack = myFront = ptr;
            ptr->next = ptr;

      }
      else {
            ptr->next = myBack;
            myBack = ptr;
            myFront->next = ptr;
      }
}

QueueElement Queue::first() const {

      if(myBack == 0) {
            cerr << " Queue empty. " << endl;

      }
      QueueElement *temp = new(QueueElement);
      QueueElement garbage = *temp;
      delete temp;
      return garbage = *temp;
      return myBack->next->data;

}

void Queue::dequeue() {

      if(myBack == 0)
            cerr << " Queue empty. " << endl;
      else {
            NodePointer ptr = myBack->next;
            if(ptr->next == ptr)
                  myBack = 0;
            else
                  myBack->next = ptr->next;
                  delete ptr;

      }

}

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

      if(myFront != NULL) {
            if(myFront == myBack) {
                  out << myFront->data;
            }
            else {
                  display(myBack, out);
            }
      }

}

void Queue::display(NodePointer ptr, ostream &out) const {

      if(ptr != myFront) {
            display(ptr->next, out);

      }
      out << ptr->data << endl;

}

QueueElement Queue::backQueue() const {

      if(myBack != 0)
            return myBack->data;

      cerr << " Queue empty. \n";

      QueueElement temp = new(QueueElement);
      QueueElement  = *temp;

      delete temp;
      return garbage;

}

QueueElement Queue::nthElement(int n) {

      QueueElement ptr = elem;
      while(n > 0 && !empty()) {
            elem = myFront();
            Queue();
            if(n > 0) {
                  cerr << " Insert width of element. " << endl;
                  return garbage;

            }
            return elem;

      }

}

int Queue::sizeQueue() {

      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*i);

      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.first() << 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.first() << endl;
      cout << "Trying to remove front of q2: " << endl;
      q2.dequeue();

      return 0;
      system("pause");

}

output:

circ.cpp
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\circ.cpp(194) : error C2440: 'initializing' : cannot convert from 'QueueElement *' to 'QueueElement'
        There is no context in which this conversion is possible
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\circ.cpp(195) : error C2513: 'int' : no variable declared before '='
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\circ.cpp(195) : error C2100: illegal indirection
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\circ.cpp(197) : error C2541: 'delete' : cannot delete objects that are not pointers
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\circ.cpp(198) : error C2065: 'garbage' : undeclared identifier
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\circ.cpp(204) : error C2065: 'elem' : undeclared identifier
c:\documents and settings\enrique\my documents\cs223_spring06 ~awanni\test\circ.cpp(206) : error C2064: term does not evaluate to a function taking 0 arguments
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 - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
0
edelossantos
Asked:
edelossantos
3 Solutions
 
phuocnhCommented:
Your program is have some errors:
1. new operator provides dynamic storage allocation so when you use
new (QueElement) it return a pointer to an QueElement element. So you cannot use
QueueElement temp = new(QueueElement);
You just change it into
QueueElement*temp = new(QueueElement);
2. You forget to supply the variable in the next statement:
    QueueElement  = *temp;
Please check again your source code in the method named
QueueElement Queue::backQueue() const

Phuoc H. Nguyen
0
 
rajeev_devinCommented:
Some more errors in function
QueueElement Queue::nthElement(int n)

elem is undefined

myFront is not a function. You are using it like a function
elem = myFront();

garbage is undefined

make these changes

good luck!
0
 
jkrCommented:
Summed that up what was said and added some more corrections:

// circ.cpp

#include <iostream>
#include <fstream>

using namespace std;

typedef int QueueElement;

class Queue {

private:

class Node {

public:

     QueueElement data;
     Node *next;
     Node():next(0){} // inline initializer
     Node(QueueElement dataValue): data(dataValue), next(0){}

};

typedef Node *next;
typedef Node *NodePointer;

public:

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

     void display(NodePointer ptr, ostream &out) const;
     NodePointer myFront, myBack;

};

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

Queue::Queue(const Queue &original) {

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

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

          }

     }

}

Queue::~Queue() {

     Queue::NodePointer prev = myBack, 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.first());
               Queue::NodePointer rhsPtr = rightHandSide.myBack->next;
               while(rhsPtr != 0) {
                    myBack->next = new Queue::Node(rhsPtr->data);
                    myBack = myBack->next;
                    rhsPtr = rhsPtr->next;

               }

          }

     }
     return *this;

}

bool Queue::empty() const {

     return myBack == 0;

}

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

     NodePointer ptr = new Node(dataVal);

     if(ptr == 0) {
          cout << "Out of memory. " << endl;

     }
     if(myBack == 0) {
          myBack = myFront = ptr;
          ptr->next = ptr;

     }
     else {
          ptr->next = myBack;
          myBack = ptr;
          myFront->next = ptr;
     }
}

QueueElement Queue::first() const {

     if(myBack == 0) {
          cerr << " Queue empty. " << endl;

     }
     QueueElement *temp = new(QueueElement);
     QueueElement garbage = *temp;
     delete temp;
     return garbage = *temp;
     return myBack->next->data;

}

void Queue::dequeue() {

     if(myBack == 0)
          cerr << " Queue empty. " << endl;
     else {
          NodePointer ptr = myBack->next;
          if(ptr->next == ptr)
               myBack = 0;
          else
               myBack->next = ptr->next;
               delete ptr;

     }

}

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

     if(myFront != NULL) {
          if(myFront == myBack) {
               out << myFront->data;
          }
          else {
               display(myBack, out);
          }
     }

}

void Queue::display(NodePointer ptr, ostream &out) const {

     if(ptr != myFront) {
          display(ptr->next, out);

     }
     out << ptr->data << endl;

}

QueueElement Queue::backQueue() const {

     if(myBack != 0)
          return myBack->data;

     cerr << " Queue empty. \n";

     QueueElement garbage = 0; // no need to create one using 'new'

     return garbage;

}

QueueElement Queue::nthElement(int n) {

     QueueElement elem;
    QueueElement garbage = 0; // was missing
     while(n > 0 && !empty()) {
          elem = myFront->data; // not a function call, and needs to '->data'
          Queue();
          if(n > 0) {
               cerr << " Insert width of element. " << endl;
               return garbage;

          }
          return elem;

     }

    return garbage;

}

int Queue::sizeQueue() {

     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*i);

     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.first() << 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.first() << endl;
     cout << "Trying to remove front of q2: " << endl;
     q2.dequeue();

     return 0;
     system("pause");

}
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now