I am constructing a class "dequeint" of integers and using circular queue storage. I get my code to compile but its output is garbage?
Please HELP!
.h
#include <iostream.h>
#include <stdlib.h>
const int MAXSIZE = 10;
typedef int NodeType;
class dequeint {
private:
NodeType NodeArray[MAXSIZE];
int headi; //head in NodeArray
int tailsucci; // index of one element beyond tail
public:
dequeint(void) {headi=tailsucci=0;}
void push_back(NodeType x) {
if( ((tailsucci+1)%MAXSIZE) !=headi) {
NodeArray[tailsucci]=x;
tailsucci = (tailsucci +1) % MAXSIZE;
}
}
void push_front(NodeType x) {
if( ((tailsucci+1)%MAXSIZE) !=headi) {
NodeArray[headi]=x;
headi = (headi +1) % MAXSIZE;
}
}
void pop_front(void) {
if( tailsucci != headi ) //if cqueue not empty
headi = (headi + 1) % MAXSIZE;
}
void pop_back(void) {
if( tailsucci != headi ) //if cqueue not empty
tailsucci = (tailsucci + 1) % MAXSIZE;
}
void print (void) {
cout << "deque: ";
for(int i=headi; i!=tailsucci; i = (i + 1) % MAXSIZE)
cout << NodeArray[i] << " ";
cout << "rear of queue " << endl;
}
};
.cpp
#include "deque.h"
int main(void) {
dequeint x;
x.print();
x.push_front(30); x.print();
x.push_front(20); x.print();
x.push_front(10); x.print();
x.push_back(40); x.print();
x.push_back(50); x.print();
x.push_back(60); x.print();
x.pop_front(); x.print();
x.pop_back(); x.print();
x.push_front(100); x.print();
x.push_back(200); x.print();
x.pop_front(); x.pop_front(); x.pop_front();
x.pop_front(); x.pop_front(); x.pop_front();
x.pop_front(); //should do nothing (and not crash)
cout << "push Enter to quit\n"; cin.get();