Stack queue
I want it to display
something like this
Stack:
Now pushing 3
Now pushing 5
now poping 5
now pushing 2
now poping 2
now poping 3
stack is empty // tried to pop again
Queue:
now pushing 3
now pushing 5
now poping 3
now pushing 2
now poping 5
now poping 2
queue is empty //tried to pop again
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include<iostream>
using namespace std;
class IntStack
{
private:
int *stackArray;
int capacity;
int top;
public:
IntStack(int capacity); // Constructor
~IntStack() { delete[] stackArray; }
void push(int value);
void pop(int &value);
bool isEmpty();
// Stack Exceptions
class Overflow {};
class Underflow {};
};
IntStack::IntStack(int capacity)
{
this->capacity = capacity;
stackArray = new int[capacity];
top = 0;
}
//***********************************
// Adds a value to the stack *
//***********************************
void IntStack::push(int value)
{
if (top == capacity) throw IntStack::Overflow();
stackArray[top] = value;
top++;
}
//****************************************
// Determines whether the stack is empty *
//****************************************
bool IntStack::isEmpty()
{
if (top == 0)
return true;
else
return false;
}
void IntStack::pop(int &value)
{
if (isEmpty()) throw IntStack::Underflow();
top --;
value = stackArray[top];
}
class IntQueue
{
private:
int *queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
IntQueue(int);
~IntQueue();
void enqueue(int);
void dequeue(int &);
bool isEmpty();
bool isFull();
void clear();
};
//*************************
// Constructor. *
//*************************
IntQueue::IntQueue(int s)
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
//*************************
// Destructor. *
//*************************
IntQueue::~IntQueue()
{
delete [] queueArray;
}
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void IntQueue::enqueue(int num)
{
if (isFull())
{
cout << "The queue is full.\n";
exit(1);
}
else
{
// Calculate the new rear position.
rear = (rear + 1) % queueSize;
// Insert new item.
queueArray[rear] = num;
// Update item count.
numItems++;
}
}
//**********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies it into num. *
//**********************************************
void IntQueue::dequeue(int &num)
{
if (isEmpty())
{
cout << "The queue is empty.\n";
exit(1);
}
else
{
// Move front.
front = (front + 1) % queueSize;
// Retrieve the front item.
num = queueArray[front];
// Update item count.
numItems--;
}
}
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool IntQueue::isEmpty()
{
if (numItems > 0)
return false;
else
return true;
}
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull()
{
if (numItems < queueSize)
return false;
else
return true;
}
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void IntQueue::clear()
{
front = - 1;
rear = - 1;
numItems = 0;
}
int main()
{
IntQueue iQueue(5);
IntStack stack(5);
int values[5];
int value;
cout << "Pushing...\n";
for (int k = 0; k < 5; k++)
{
cin>>values[k];
}
for (int k = 0; k < 5; k++)
{
cout << values[k] << " ";
stack.push(values[k]);
cout << "\nPopping...\n";
iQueue.enqueue(k*k);
cout << "queuing 5 items...\n";
}
while (!stack.isEmpty())
{
stack.pop(value);
cout << value << " ";
iQueue.dequeue(value);
cout << value << " ";
}
cout << endl;
return 0;
}
|
Topic archived. No new replies allowed.