1.数组实现栈
#include<iostream>
using namespace std;
class arr_to_stack
{
private:
int size;
int initsize;
int *arr;
public:
void stack(int _initsize)
{
if (_initsize < 0)
{
cout << "array initsize is zone" << endl;
return;
}
arr = new int[_initsize];
size = 0;
initsize = _initsize;
}
void push(int obj)
{
if (size == initsize)
{
cout << "stack is pull,not push" << endl;
return;
}
arr[size++] = obj;
}
int pop()
{
if (size == 0)
{
cout << "the stack is empty,not pop" << endl;
return -1;
}
return arr[--size];
}
int top()
{
if (size == 0)
{
cout << "the stack is empty,not pop" << endl;
return -1;
}
return arr[size-1];
}
void _Print()
{
for (int i = 0; i < size; i++)
cout << arr[i] << ' ';
cout << endl;
}
};
int main()
{
arr_to_stack b;
b.stack(5);
b.push(7);
b.push(7);
b.push(6);
b._Print();
cout << b.pop() << endl;
system("pause");
return 0;
}