顺序栈接口

/*
 * MyStack.h
 * Created on: 2009-12-22
 * Programming Language: C++
 * Operating system: Window XP
 * Environment: DEV-C++ and Eclipse
 * Author: http://blog.csdn.net/programs
 */
#ifndef MYSTACK_H_
#define MYSTACK_H_
typedef int DataType;
class MyStack
{
public:
     MyStack(int max_size);
     ~MyStack();
     bool MyPush(DataType max_size);
     bool MyPop();
     bool IsEmpty();
     bool IsFull();
     DataType GetTopItem();
     int GetTopNum();
     int GetSize();
     void MyDisplay();
private:
     DataType *my_array;
     int max_size;
     int size;
     int top;
};
#endif /* MYSTACK_H_ */
/* ********************************* */

/*
 * MyStack.cpp
 * Created on: 2009-12-22
 * Programming Language: C++
 * Operating system: Window XP
 * Environment: DEV-C++ and Eclipse
 * Author: http://blog.csdn.net/programs
 */
#include "MyStack.h"
#include <iostream>
using namespace std;
MyStack::MyStack(int max_size)
{
     my_array = new DataType[max_size];
     this->max_size = max_size;
     this->size = 0;
     this->top = -1;
}
MyStack::~MyStack()
{
     delete my_array;
}
bool MyStack::MyPush(DataType value)
{
     if (IsFull())
     {
          cout << "The stack is full, do not push any item in any more." << endl;
          exit(-1);
     }
     ++top;
     my_array[top] = value;
     ++size;
     return true;
}
bool MyStack::MyPop()
{
     if (IsEmpty())
     {
          cout << "The stack is empty, there is no item to pop up" << endl;
          return false;
     }
     top--;
     (this->size)--;
     return true;
}
bool MyStack::IsEmpty()
{
     return (0 == this->size);
}
bool MyStack::IsFull()
{
     return ((max_size - 1) == top);
}
DataType MyStack::GetTopItem()
{
     if (IsEmpty())
     {
          cout << "The stack is empty, there is no top item." << endl;
          exit(-1);
     }
     return (my_array[top]);
}
int MyStack::GetSize()
{
     return (this->size);
}
void MyStack::MyDisplay()
{
     if (IsEmpty())
     {
          cout << "The stack is empty, there is no item the display." << endl;
          exit(-1);
     }
     int i = 0;
     for (i = (this->size - 1); i >= 0 ; --i)
     {
          cout << "|" << my_array[i] << "|" << endl;
     }
}
int MyStack::GetTopNum()
{
     return top;
}
/* ********************************* */

/*
 * TestMain.cpp
 * Created on: 2009-12-22
 * Programming Language: C++
 * Operating system: Window XP
 * Environment: DEV-C++ and Eclipse
 * Author: http://blog.csdn.net/programs
 */
#include "MyStack.h"
#include <iostream>
using namespace std;
int main(void)
{
     MyStack st(5);
     st.MyPush(66);
     st.MyPush(55);
     st.MyPush(33);
     st.MyPush(22);
     st.MyPush(11);
     st.MyDisplay();
     cout << "The top item is: " << st.GetTopItem() << endl;
     int i = 0;
     for (i = 0; i < 4; ++i)
     {
          st.MyPop();
     }
     cout << "----------------" << endl;
     st.MyDisplay();
     cout << "The top item is: " << st.GetTopItem() << endl;
     return 0;
}
 

你可能感兴趣的:(职场,休闲,栈接口)