Solved

ADT Reversing Queue() { struct

Posted on 2004-10-21
873 Views
Last Modified: 2013-12-14
// This is a code that I put together...when I enter 2 4 6 8 10 I need as an output 10 8 6 4 2.  This algorithm is generating errors.  The errors are listed below.  Please advise.

// revQueue.cpp

#include<iostream.h>

using namespace std;

struct Queue {

int key;
Queue *next;

};

//function proto to print in reverse
void printInReverse(Queue *head);  

int main() {
  int value;
  Queue q;    
  q.append(5);
  q.append(10);
 
  Queue *header = NULL;
  Queue *back = NULL ;
   
   while(! q.empty() )
   {
     q.serve_retrieve(value);
     s.push(value);
   }
   while ( ! s.empty() )
   {
      s.pop_top(value);
      q.append(value);
   }


   for (int i = 1; i < 10 ; i ++) { //loop to populate enqueue

   Queue *newPtr = new Queue;
   newPtr->key = i;
   newPtr->next = NULL;

   if( header == NULL) {header = back = newPtr;}
   
   else {
   back->next = newPtr;
   back = newPtr;
   newPtr = NULL;
   }

 }

    printInReverse(header); // call function to print list in reverse
   
    //printf("\n");
    cout << endl;

    system("pause") ;

    return 0;
}
 
// ******BEGIN function to print in reverse*********

void printInReverse(Queue *head) {
 
   if(head == NULL)

}
   return;
   }
   else {
   printInReverse(head->next) ;
   }

   cout << Head->key ;
}

generated output:

[edeloss2@pegasus exp]$ cxx revQueue.cpp
cxx: Error: revQueue.cpp, line 5: name must be a namespace name
using namespace std;
----------------^
cxx: Error: revQueue.cpp, line 20: class "Queue" has no member "append"
  q.append(5);
----^
cxx: Error: revQueue.cpp, line 21: class "Queue" has no member "append"
  q.append(10);
----^
cxx: Error: revQueue.cpp, line 26: class "Queue" has no member "empty"
   while(! q.empty() )
-------------^
cxx: Error: revQueue.cpp, line 28: class "Queue" has no member
          "serve_retrieve"
     q.serve_retrieve(value);
-------^
cxx: Error: revQueue.cpp, line 29: identifier "s" is undefined
     s.push(value);
-----^
cxx: Error: revQueue.cpp, line 31: identifier "s" is undefined
   while ( ! s.empty() )
-------------^
cxx: Error: revQueue.cpp, line 34: class "Queue" has no member "append"
      q.append(value);
--------^
cxx: Error: revQueue.cpp, line 59: identifier "system" is undefined
    system("pause") ;
----^
cxx: Error: revQueue.cpp, line 70: expected a statement
}
^
cxx: Error: revQueue.cpp, line 71: expected a declaration
   return;
---^
cxx: Error: revQueue.cpp, line 72: expected a declaration
   }
---^
cxx: Warning: revQueue.cpp, line 77: parsing restarts here after previous
          syntax error
   cout << Head->key ;
---------------------^
cxx: Error: revQueue.cpp, line 78: expected a declaration
}
^
cxx: Info: 13 errors detected in the compilation of "revQueue.cpp".


0
Question by:edelossantos
    8 Comments
     
    LVL 86

    Assisted Solution

    by:jkr
    You need to

    #include <iostream>

    instead of

    #include<iostream.h>

    Then, 'append()' and 'empty()' are just not defined for

    struct Queue {

    int key;
    Queue *next;

    };
    0
     

    Author Comment

    by:edelossantos
    // revQueue.cpp

    #include<iostream>

    using namespace std;

    typedef int Entry_type;
    const int maxsize = 20;

    struct Queue {

      int key;
      Queue *next;
      void append(const Entry_type &);
      bool empty() const;
      int size() const;
      void display() const;

    private:

      int front, rear, count;
      Entry_type queue[maxsize];


    };

    //function proto to print in reverse
    void printInReverse(Queue *head);  

    int main() {
      int value;
      Queue q;    
      q.append(5);
      q.append(10);
     
      Queue *header = NULL;
      Queue *back = NULL ;
       Stack s;
       while(!q.empty()) {
         
         q.serve_retrieve(value);
         s.push(value);
       }
       while ( ! s.empty() ) {
         
          s.pop_top(value);
          q.append(value);
       }

       for (int i = 1; i < 10 ; i ++) { //loop to populate enqueue

       Queue *newPtr = new Queue;
       newPtr->key = i;
       newPtr->next = NULL;

       if( header == NULL) {header = back = newPtr;}
       
       else {
       back->next = newPtr;
       back = newPtr;
       newPtr = NULL;
       }

     }

        printInReverse(header); // call function to print list in reverse
       
        //printf("\n");
        cout << endl;

        system("pause") ;

        return 0;
    }
     
    // ******BEGIN function to print in reverse*********

    void printInReverse(Queue *head) {
     
       if(head == NULL)

    }
       return;
       }
       else {
       printInReverse(head->next) ;
       }

       cout << Head->key ;
    }

    void Queue::append(const Entry_type &value) {

      if(full())
        return overflow;
      else {
        rear = ++rear % maxsize;
        queue[rear] = value;
        count++;

      }
      return value;

    }

    bool Queue::empty() const {

      if(count == 0)
        return true;
      else
        return false;

    }
     
    int Queue::size() const{return count;}

    void Queue::display() const {

      clearScreen();

      for(int i=0; i<maxsize; i++)
        if(queue[i] != -1)
          cout << queue[i] << " ";
        else
          cout << " ";

    }

    generated output:

    [edeloss2@pegasus exp]$ g++ revQueue.cpp
    revQueue.cpp: In function `int main()':
    revQueue.cpp:38: `Stack' undeclared (first use this function)
    revQueue.cpp:38: (Each undeclared identifier is reported only once
    revQueue.cpp:38: for each function it appears in.)
    revQueue.cpp:38: parse error before `;'
    revQueue.cpp:41: no matching function for call to `Queue::serve_retrieve (int &)
    '
    revQueue.cpp:42: `s' undeclared (first use this function)
    revQueue.cpp:71: implicit declaration of function `int system(...)'
    revQueue.cpp: In function `void printInReverse(Queue *)':
    revQueue.cpp:82: parse error before `}'
    revQueue.cpp:82: confused by earlier errors, bailing out

    0
     
    LVL 86

    Assisted Solution

    by:jkr
    >>revQueue.cpp:38: `Stack' undeclared (first use this function)

    The compiler is right, there is no previous declaration for "Stack" in the code you posted. Maybe there's just a header file missing.
    0
     

    Author Comment

    by:edelossantos
    // I made a change.

    // revQueue.cpp

    #include<iostream>

    using namespace std;

    typedef int Entry_type;
    const int maxsize = 20;

    struct Queue {

      int key;
      Queue *next;
      void append(const Entry_type &);
      bool empty() const;
      int size() const;
      void display() const;

    private:

      int front, rear, count;
      Entry_type queue[maxsize];


    };

    //function proto to print in reverse
    void printInReverse(Queue *head);  

    int main() {
      int value;
      Queue q;    
      q.append(5);
      q.append(10);
     
      Queue *header = NULL;
      Queue *back = NULL ;
       int s;
       while(!q.empty()) {
         
         q.serve_retrieve(value);
         s.push(value);
       }
       while ( ! s.empty() ) {
         
          s.pop_top(value);
          q.append(value);
       }

       for (int i = 1; i < 10 ; i ++) { //loop to populate enqueue

       Queue *newPtr = new Queue;
       newPtr->key = i;
       newPtr->next = NULL;

       if( header == NULL) {header = back = newPtr;}
       
       else {
       back->next = newPtr;
       back = newPtr;
       newPtr = NULL;
       }

     }

        printInReverse(header); // call function to print list in reverse
       
        //printf("\n");
        cout << endl;

        //const system("pause") ;

        return 0;
    }
     
    // ******BEGIN function to print in reverse*********

    void printInReverse(Queue *head) {
     
       if(head == NULL)

    }
       return;
       }
       else {
       printInReverse(head->next) ;
       }

       cout << Head->key ;
    }

    void Queue::append(const Entry_type &value) {

      if(full())
        return overflow;
      else {
        rear = ++rear % maxsize;
        queue[rear] = value;
        count++;

      }
      return value;

    }

    bool Queue::empty() const {

      if(count == 0)
        return true;
      else
        return false;

    }
     
    int Queue::size() const{return count;}

    void Queue::display() const {

      clearScreen();

      for(int i=0; i<maxsize; i++)
        if(queue[i] != -1)
          cout << queue[i] << " ";
        else
          cout << " ";

    }

    output:

    [edeloss2@pegasus exp]$ g++ revQueue.cpp
    revQueue.cpp: In function `int main()':
    revQueue.cpp:41: no matching function for call to `Queue::serve_retrieve (int &)
    '
    revQueue.cpp:42: request for member `push' in `s', which is of non-aggregate typ
    e `int'
    revQueue.cpp:44: request for member `empty' in `s', which is of non-aggregate ty
    pe `int'
    revQueue.cpp:46: request for member `pop_top' in `s', which is of non-aggregate
    type `int'
    revQueue.cpp: In function `void printInReverse(Queue *)':
    revQueue.cpp:82: parse error before `}'
    revQueue.cpp:82: confused by earlier errors, bailing out

    0
     
    LVL 86

    Assisted Solution

    by:jkr
    You are apparently believing in the success of salami tactics? :o)

    Well, now we're getting further, but as gcc states:

    revQueue.cpp:41: no matching function for call to `Queue::serve_retrieve (int &)

    That class does not have such a member function (so far)
    0
     

    Author Comment

    by:edelossantos
    // revQueue.cpp

    #include <iostream>

    using namespace std;

    typedef int Entry_type;
    const int maxsize = 20;

    struct Queue {

      int key;
      Queue *next;
      void append(const Entry_type &);
      void pop();
      void push(const Entry_type &);
      void pop_top(Entry_type &);
      void serve_retrieve(Entry_type &);
      void top(Entry_type &) const;
      bool empty() const;
      bool full() const;
      int size() const;
      void display() const;

    private:

      int front, rear, count, stack_top;
      Entry_type queue[maxsize];

    };

    //function proto to print in reverse
    void printInReverse(Queue *head);  

    int main() {
      int value;
      Queue q;    
      q.append(5);
      q.append(10);
     
      Queue *header = NULL;
      Queue *back = NULL ;
       int s;
       while(!q.empty()) {
         
         q.serve_retrieve(value);
         s.push(value);
       }
       while ( ! s.empty() ) {
         
          s.pop_top(value);
          q.append(value);
       }

       for (int i = 1; i < 10 ; i ++) { //loop to populate enqueue

       Queue *newPtr = new Queue;
       newPtr->key = i;
       newPtr->next = NULL;

       if( header == NULL) {header = back = newPtr;}
       
       else {
       back->next = newPtr;
       back = newPtr;
       newPtr = NULL;
       }

     }

        printInReverse(header); // call function to print list in reverse
       
        //printf("\n");
        cout << endl;

        //const system("pause") ;

        return 0;
    }
     
    // ******BEGIN function to print in reverse*********

    void printInReverse(Queue *head) {
     
       if(head == NULL)

    }
       return;
       }
       else {
       printInReverse(head->next) ;
       }

       cout << Head->key ;
    }

    void Queue::append(const Entry_type &value) {

      if(full())
        return overflow;
      else {
        rear = ++rear % maxsize;
        queue[rear] = value;
        count++;

      }
      return value;

    }

    bool Queue::empty() const {

      if(count == 0)
        return true;
      else
        return false;

    }

    bool Queue::full() const {

      if(count == maxsize)
        return true;
      else
        return false;

    }
     
    int Queue::size() const{return count;}

    void Queue::display() const {

      clearScreen();

      for(int i=0; i<maxsize; i++)
        if(queue[i] != -1)
          cout << queue[i] << " ";
        else
          cout << " ";

    }

    void Queue::serve_retrieve(Entry_Type &value) {

      if(empty())
        return underflow;
      else {
        value = queue[front];
        front = (++front) % maxsize;
        count--;

      }
      return value;

    }

    void Queue::pop_top(Entry_type &value) {

      if(stack_top == 0)
        return underflow;
      else {
        value = stack[stack_top--];

      }
      return value;

    }

    void Queue::push(const Entry_type &value) {

      if(stack_top >= maxsize - 0)
        return overflow;
      else
        stack[++stack_top] = value;
        return value;

    }

    void Queue::pop() {

      if(stack_top == 0)
        return underflow;
      else
        stack_top--;
        return value;

    }

    void Queue::top(Entry_type &value) const {

      if(stack_top == 0)
        return underflow;
      else
        value = stack[stack_top];
        return value;

    }
     
    output:

    [edeloss2@pegasus exp]$ g++ revQueue.cpp
    revQueue.cpp: In function `int main()':
    revQueue.cpp:47: request for member `push' in `s', which is of non-aggregate typ
    e `int'
    revQueue.cpp:49: request for member `empty' in `s', which is of non-aggregate ty
    pe `int'
    revQueue.cpp:51: request for member `pop_top' in `s', which is of non-aggregate
    type `int'
    revQueue.cpp: In function `void printInReverse(Queue *)':
    revQueue.cpp:87: parse error before `}'
    revQueue.cpp:87: confused by earlier errors, bailing out

    // I do not understand these.  I thought I had repaired with the new implementations.




    0
     

    Author Comment

    by:edelossantos
    jkr,

    I've tried to repair the codes myself and I am stuck.  Please advise when you can.  Sorry about the salami tactics.  Del
    0
     
    LVL 86

    Accepted Solution

    by:
    In

     int value;
     Queue q;    
     q.append(5);
     q.append(10);
     
     Queue *header = NULL;
     Queue *back = NULL ;
       int s;
      while(!q.empty()) {
       
         q.serve_retrieve(value);
        s.push(value);
      }
      while ( ! s.empty() ) {
       
          s.pop_top(value);
         q.append(value);
      }

    's' is an integer, which you cannot use as a queue. Did you mean

       Queue s;
      while(!q.empty()) {
       
         q.serve_retrieve(value);
        s.push(value);
      }
      while ( ! s.empty() ) {
       
          s.pop_top(value);
         q.append(value);
      }

    ?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    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…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now