#ifndef ARRAY2STACK_H
#define ARRAY2STACK_H
#include <memory.h>
#include <iostream>
class array2stack
{
public:
array2stack();
~array2stack();
int poll();
void push(int val);
int peek();
void printstack();
private:
int buff[4];
int CurSize;
int stackSize;
int curpos;
};
#endif // ARRAY2STACK_H
#include "array2stack.h"
array2stack::array2stack()
{
memset(buff,0,sizeof(buff));
CurSize = 0;
stackSize = 4;
curpos = 0;
}
array2stack::~array2stack()
{
}
int array2stack::poll()
{
if(CurSize == 0)
throw 12;
else
CurSize --;
int temp = curpos;
curpos = curpos == 0 ? (stackSize - 1) : curpos - 1;
return buff[temp];
}
void array2stack::push(int val)
{
if(CurSize == stackSize)
throw 1001;
else
CurSize ++;
buff[curpos] = val;
int temp = curpos;
curpos = curpos == (stackSize - 1) ? 0 : curpos + 1;
}
void array2stack::printstack()
{
int i = CurSize;
int head = curpos;
while(i --)
{
head = head == 0 ? stackSize - 1 : head - 1;
std::cout << buff[head] << std::endl;
}
}
int array2stack::peek()
{
int tmpPos = curpos == 0 ? (stackSize - 1) : curpos - 1;
return buff[tmpPos];
}
int main()
{
array2stack * mystack = new array2stack();
try{
mystack->push(1);
mystack->push(3);
mystack->push(5);
mystack->push(8);
mystack->printstack();
std::cout << "$$$$$$$$$$" << std::endl;
std::cout << mystack->poll() << std::endl;
std::cout << mystack->poll() << std::endl;
std::cout << mystack->poll() << std::endl;
std::cout << mystack->poll() << std::endl;
}
catch(int errcode)
{
std::cout << "err : " << std::endl;
}
return 0;
}