1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
quack.h
#pragma once
#include <ostream>
class Quack
{
public:
static const char YOUR_NAME[]; // used for printing out programmer's name
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:
int items; // pointer to storage for the circular array.
// Each item in the array is an int.
int maxSize; // is for the size of the item stack
int back; // is for the back or "bottom" of the stack
int count; // to count the items added to the stack
int top;
struct item // Definition of each item stored by the quack.
{
int n;
};
//item *items; // Pointer to storage for the circular array.
item *backPtr;
item *frontPtr;
item *midPtr;
item *current; // Each item in the array is an int.
public:
friend std::ostream& operator<<(std::ostream& out, Quack *q);
};
lab2driver.cpp
#include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
#include <iostream>
#include "quack.h"
using namespace std;
const static int QUACK_SIZE = 7;
static Quack *quack;
static void push(Quack *quack, bool front, int n)
{
bool ok;
if (front)
ok = quack->pushFront(n);
else
ok = quack->pushBack(n);
cout << ">>> push" << (front ? "Front " : "Back ") << n << (ok ? " succeeded" : " failed") << endl;
}
static void pop(Quack *quack, bool front)
{
bool ok;
int n;
if (front)
ok = quack->popFront(n);
else
ok = quack->popBack(n);
cout << ">>> pop" << (front ? "Front " : "Back ");
if (ok)
cout << "succeeded: " << n;
else
cout << "failed";
cout << endl;
}
int main(int argc, char **argv)
{
cout << "CS260 - Lab2 - " << Quack::YOUR_NAME << endl << endl;
quack = new Quack(QUACK_SIZE);
cout << quack;
push(quack, true, 1);
push(quack, true, 2);
push(quack, true, 3);
push(quack, true, 4);
push(quack, false, 0);
push(quack, true, 9);
cout << quack;
cout << "--- # of items: " << quack->itemCount() << endl << endl;
pop(quack, true);
cout << quack;
pop(quack, true);
cout << quack;
push(quack, false, 7);
cout << quack;
push(quack, false, 8);
cout << quack;
cout << ">>> rotate(2)" << endl;
quack->rotate(2);
cout << quack;
cout << ">>> rotate(-3)" << endl;
quack->rotate(-3);
cout << quack;
cout << ">>> reverse" << endl;
quack->reverse();
cout << quack;
push(quack, true, 6);
cout << quack;
cout << ">>> rotate(3)" << endl;
quack->rotate(3);
cout << quack;
cout << ">>> rotate(-4)" << endl;
quack->rotate(-4);
cout << quack;
cout << "--- # of items: " << quack->itemCount() << endl << endl;
while (quack->itemCount() > 0) {
pop(quack, false);
cout << quack;
}
cout << "--- # of items: " << quack->itemCount() << endl << endl;
delete quack;
// report on memory leaks in the Output Window
#ifdef _DEBUG
if (argc == 2) {
_CrtSetReportMode( _CRT_WARN , _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN , _CRTDBG_FILE_STDERR );
}
_CrtDumpMemoryLeaks();
#endif
return 0;
}
memoryleakdetect.h
#pragma once // include this .h file file only once
// enable Visual C++ memory leak checking
#ifdef _DEBUG
#include <ostream>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
|