Getting garbage...[Queue question]
Input does exactly what it looks like... Some user enters a string of characters and that is then appended char by char to preQ....
This is code from my main, I bolded that cout because it doesn't even get there... It just prints garbage, and it doesn't iterate at all... So there's a problem with retrieve/append I believe... But I'm not sure what.
Code:
void input (Queue preQ)
{
string expression;
int i;
cout<<"Enter a prefix expression: ";
cin>>expression;
for (i=0; i<=expression.length(); ++i)
{
preQ.append(expression[i]);
}
}
void pre_to_post (Queue preQ, Queue postQ)
{
Stack opStack;
Stack_entry qItem; //queue item
Stack_entry sItem; //stack item
do
{
preQ.retrieve(qItem.element);
preQ.serve();
cout<<qItem.element;
if(is_operator(qItem.element)==1)
opStack.push(qItem);
else
{
postQ.append(qItem.element);
opStack.top(sItem);
opStack.pop();
if(sItem.flag==0)
{
sItem.flag=1;
opStack.push(sItem);
}
else
{
postQ.append(sItem.element);
}
}
}while (!(preQ.empty()));
}
Stack Class:
Code:
// Stack.cpp
#include "Stack.h"
Error_code Stack::push(const Stack_entry &item)
/*Pre: None
Post: If the Stack is not full, item is added to the top of the Stack.
If the Stack is empty, an Error_code of underflow is returned.*/
{
Error_code outcome = success;
if (myTop >= MAXSTACK-1)
outcome = overflow;
else
entry[myTop++] = item;
return outcome;
}
Error_code Stack::pop()
/*Pre: None
Post: If the Stack is not empty, the top of the Stack is removed.
If the stack is empty, an Error_code of underflow is returned.*/
{
Error_code outcome = success;
if (myTop == -1)
outcome = underflow;
else --myTop;
return outcome;
}
Error_code Stack::top(Stack_entry &item) const
/*Pre: None
Post: If the Stack is not empty, the top of the Stack is returned
in item. If the Stack is empty an Error_code of underflow is
returned.*/
{
Error_code outcome = success;
if (myTop == -1)
outcome = underflow;
else
item = entry[myTop];
return outcome;
}
bool Stack::empty() const
/*Pre: None
Post: If the Stack is empty, true is returned. Otherwise false
is returned.*/
{
bool outcome = true;
if (myTop > -1) outcome = false;
return outcome;
}
Stack::Stack()
/*Pre: None
Post: The stack is initialized to be empty.*/
{
myTop = -1;
}
Queue class:
Code:
// Queue.cpp
#include "Queue.h"
Queue::Queue()
/*Post: The Queue is initialized to be empty. */
{
count = 0;
rear = maxqueue -1;
front = 0;
}
bool Queue::empty() const
/*Post: Return true if the Queue is empty, otherwise
return false.*/
{
return count == 0;
}
Error_code Queue::append(const Queue_entry &item)
/*Post: item is added to the rear of the Queue. If
the Queue is full return an Error_code of overflow
and leave the Queue unchanged. */
{
if (count>=maxqueue) return overflow;
count++;
rear = ((rear + 1)== maxqueue)?0:(rear+1);
entry[rear] = item;
return success;
}
Error_code Queue::serve()
/*Post: The front of the Queue is removed. If
the Queue is empty return an Error_code of
underflow. */
{
if (count <= 0) return underflow;
count--;
front = ((front+1) == maxqueue)?0:(front+1);
return success;
}
Error_code Queue::retrieve(Queue_entry &item) const
/*Post: The front of the Queue retrieved to
the output parameter item. If the Queue is
empty return an Error_code of underflow. */
{
if (count <= 0) return underflow;
item = entry[front];
return success;
}
[edit] - Agh! forgot the header files, sorry!!
Stack.h
Code:
// Stack.h
#ifndef STACK_H
#define STACK_H
#include "Utility.h"
const int MAXSTACK = 10;
struct Stack_entry
{ char element;
bool flag;
};
class Stack {
public:
Stack();
bool empty() const;
Error_code pop();
Error_code top(Stack_entry &item) const;
Error_code push(const Stack_entry &item);
private:
int myTop; //myTop is -1 when stack is empty
Stack_entry entry[MAXSTACK];
};
#endif
Queue.h
Code:
// Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include "Utility.h"
const int maxqueue = 10; // small value for testing
typedef char Queue_entry;
class Queue {
public:
Queue();
bool empty() const;
Error_code serve();
Error_code append(const Queue_entry &item);
Error_code retrieve(Queue_entry &item) const;
private:
int count;
int front, rear;
Queue_entry entry[maxqueue];
};
#endif
I've looked it over and over but I can't seem to spot the problem.