Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ADT Reversing Queue() { struct

Posted on 2004-10-21
8
Medium Priority
?
887 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
Comment
Question by:edelossantos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 2000 total points
ID: 12377064
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
ID: 12377366
// 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
jkr earned 2000 total points
ID: 12377412
>>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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:edelossantos
ID: 12377476
// 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
jkr earned 2000 total points
ID: 12377509
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
ID: 12377769
// 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
ID: 12377899
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:
jkr earned 2000 total points
ID: 12381481
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

  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 …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

604 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