157 lines
3.2 KiB
C++
157 lines
3.2 KiB
C++
|
#include <iostream>
|
||
|
#include <new>
|
||
|
#include "stack.h"
|
||
|
|
||
|
|
||
|
int Max_Stack; // Global variable for stack size
|
||
|
Stack::Stack(int n) // Parameterized constructor dynamically allocates an empty stack array
|
||
|
{ // that may hold no more than n elements and initializes other private variables
|
||
|
Max_Stack=n;
|
||
|
array = new int[n];
|
||
|
top=-1;
|
||
|
num=n;
|
||
|
}
|
||
|
|
||
|
void Stack::Resize(int n) // Attempts to increase size of stack array to 2*num and then push n onto stack
|
||
|
{ // If unable to resize, throw StackFull exception
|
||
|
try
|
||
|
{
|
||
|
int largerArry[2*Max_Stack];
|
||
|
for (int i = 0; i < num; i++)
|
||
|
{
|
||
|
largerArry[i]=array[i];
|
||
|
}
|
||
|
delete [] array;
|
||
|
num= num * 2; // num to twice original size
|
||
|
Max_Stack=num;
|
||
|
array = largerArry;
|
||
|
array[top]=n;
|
||
|
//top++;
|
||
|
}
|
||
|
catch( ... )
|
||
|
{
|
||
|
throw StackFull();
|
||
|
//return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Stack::~Stack()
|
||
|
{
|
||
|
delete [] array;
|
||
|
}
|
||
|
|
||
|
void Stack::Push(int n)
|
||
|
{
|
||
|
bool full = IsFull();
|
||
|
if (full)
|
||
|
{
|
||
|
Resize(n);
|
||
|
array[top]=n;
|
||
|
}
|
||
|
//array[top]=n;
|
||
|
top++;
|
||
|
array[top]=n;
|
||
|
}
|
||
|
|
||
|
void Stack::Pop()
|
||
|
{
|
||
|
bool empty=IsEmpty();
|
||
|
if (empty)
|
||
|
{
|
||
|
throw StackEmpty();
|
||
|
}
|
||
|
top--;
|
||
|
}
|
||
|
|
||
|
bool Stack::IsEmpty() const
|
||
|
{
|
||
|
return (top == -1);
|
||
|
}
|
||
|
|
||
|
bool Stack::IsFull() const
|
||
|
{
|
||
|
return (top == Max_Stack);
|
||
|
}
|
||
|
|
||
|
void Stack::MakeEmpty()
|
||
|
{
|
||
|
top = -1;
|
||
|
}
|
||
|
|
||
|
int Stack::Top() const
|
||
|
{
|
||
|
bool empty=IsEmpty();
|
||
|
if (empty)
|
||
|
{
|
||
|
throw StackEmpty();
|
||
|
//return;
|
||
|
}
|
||
|
return array[top];
|
||
|
}
|
||
|
|
||
|
int Stack::Size() const // Returns number of items on stack WITHOUT modifying the stack
|
||
|
{
|
||
|
return top;
|
||
|
}
|
||
|
|
||
|
int Stack::Max() const // Returns value of largest integer on stack WITHOUT modifying the stack
|
||
|
{ // If stack is empty, throws StackEmpty
|
||
|
bool empty=IsEmpty();
|
||
|
if (empty)
|
||
|
{
|
||
|
throw StackEmpty();
|
||
|
}
|
||
|
|
||
|
int i;
|
||
|
|
||
|
// Initialize maximum element
|
||
|
int max = array[0];
|
||
|
|
||
|
// Traverse array elements
|
||
|
// from second and compare
|
||
|
// every element with current max
|
||
|
for (i = 1; i < top; i++)
|
||
|
{
|
||
|
if (array[i] > max)
|
||
|
max = array[i];
|
||
|
}
|
||
|
return max;
|
||
|
}
|
||
|
|
||
|
int Stack::Min() const // Returns value of smallest integer on stack WITHOUT modifying the stack
|
||
|
{ // If stack is empty, throws StackEmpty
|
||
|
bool empty=IsEmpty();
|
||
|
if (empty)
|
||
|
{
|
||
|
throw StackEmpty();
|
||
|
}
|
||
|
|
||
|
int i;
|
||
|
|
||
|
// Initialize minimum element
|
||
|
int min = array[0];
|
||
|
|
||
|
// Traverse array elements
|
||
|
// from second and compare
|
||
|
// every element with current min
|
||
|
for (i = 1; i < top; i++)
|
||
|
{
|
||
|
if (array[i] < min)
|
||
|
min = array[i];
|
||
|
}
|
||
|
return min;
|
||
|
}
|
||
|
|
||
|
int Stack::Peek(unsigned int n) const // Returns stack value n levels down from top of stack. Peek(0) = Top()
|
||
|
{ // If position n does not exist, throws StackInvalidPeek
|
||
|
|
||
|
if(!array[top-n])
|
||
|
throw StackInvalidPeek();
|
||
|
int st=array[top-n];
|
||
|
return st;
|
||
|
}
|
||
|
|
||
|
int Stack::Capacity() const // Returns total num of elements that maybe stored in stack array
|
||
|
{
|
||
|
return Max_Stack;
|
||
|
}
|