Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class stack{
- private:
- int size, *stack_, count;
- public:
- stack(int);
- ~stack(){
- };
- stack push(const int&n);
- bool empty();
- bool full();
- stack pop();
- void print();
- void clear();
- };
- stack::stack(int n = 0){
- size = n;
- stack_ = new int[size];
- }
- stack stack::push(const int&n){
- if (full())
- {
- cout << "stack is full, can not push" << endl;
- }
- else
- stack_[count++] = n;
- return *this;
- }
- bool stack::empty(){
- if (count < 0){
- return true;
- }
- else return false;
- }
- bool stack::full(){
- if (count >= size){
- return true;
- }
- else return false;
- }
- stack stack::pop(){
- if (empty())
- {
- count = 0;
- cout << "stack is empty, can not pop" << endl;
- }
- else
- count--;
- return *this;
- }
- void stack::print(){
- cout << "---------" << endl;
- for (int i = 0; i < count; i++){
- cout << stack_[i] << endl;
- }
- cout << "---------" << endl;
- }
- void stack::clear(){
- delete stack_;
- }
- int main(){
- stack p(5);
- p.push(1);
- p.push(1);
- p.push(1);
- p.push(1);
- p.push(1);
- p.push(1);
- p.push(1);
- p.print();
- p.pop();
- p.print();
- p.clear();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement