I am trying to push items into a circular array by push front and then pushback.
it pushes 1 -4 in front then suppose to push 0 in back then push 9 in front.
What i get is 0 in front. The book i have only shows how to push in back.
Any help is very appreciative since i have worked on this for hours.
Sorry about the format of my code. I didnt notice it was really bad until i hit preview and im too lazy to start this post over.
Code:
const static int QUACK_SIZE = 7;
static void push(quack& q, bool front, int n)
{
bool ok;
if (front)
ok = q.pushFront(n);
else
ok = q.pushBack(n);
cout << ">>> push" << (front ? "Front " : "Back ") << n << (ok ? " succeeded\n" : " failed\n");
}
static void pop(quack& q, bool front)
{
bool ok;
int n;
if (front)
ok = q.popFront(n);
else
ok = q.popBack(n);
cout << ">>> pop" << (front ? "Front " : "Back ");
if (ok)
cout << "succeeded: " << n << endl;
else
cout << "failed\n";
}
int main (void)
{
quack q(QUACK_SIZE);
#ifdef _WIN32
// request memory leak report in Output Window after main returns
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
cout << q;
push(q, true, 1);
push(q, true, 2);
push(q, true, 3);
push(q, true, 4);
push(q, false, 0);
push(q, true, 9);
cout << q;
cout << "--- # of items: " << q.itemCount() << endl << endl;
Code:
#pragma once
#include <ostream>
using namespace std;
class quack
{
public:
quack(int capacity);
~quack(void);
bool pushFront(const int n); // push an item onto the front
bool pushBack(const int n); // push an item onto the back
bool popFront(int& n); // pop an item off the front
bool popBack(int& n); // pop an item off the back
void rotate(int r); // "rotate" the stored items (see note below)
void reverse(void); // reverse the order of the stored items
int itemCount(void); // return the current number of stored items
private:
struct item // definition of each item stored by the quack
{
int n;
int top;
int count;
int maxsize;
int front;
int back;
};
item *items; // pointer to storage for the circular array
public:
friend ostream& operator<<(ostream& out, quack& q);
friend ostream& operator<<(ostream& out, quack::item& i);
};
Code:
quack::quack(int capacity)
{
items = new item[capacity];
items->n = 0;
items->top = 0;
items->count = 0;
items->maxsize = capacity;
items->back = capacity;
items->front = capacity -1;
}
Code:
bool quack::pushFront(const int n)
{
if ( items->count == items->maxsize )
{
return false;
}
items->front = (items->front - 1) % items->maxsize;
items[items->front].n = n;
items->count++;
return true;
}
bool quack::pushBack(const int n)
{
items->back = (items->back + 1) % items->maxsize;
items[items->back].n = n;
items->count++;
return true;
//items[items->count].n = n;
//items->count++;
}
my output:
Code:
quack: empty
>>> pushFront 1 succeeded
>>> pushFront 2 succeeded
>>> pushFront 3 succeeded
>>> pushFront 4 succeeded
>>> pushBack 0 succeeded
>>> pushFront 9 succeeded
quack: 0, 9, 4, 3, 2, 1
--- # of items: 6
>>> popFront failed
quack: 0, 9, 4, 3, 2, 1