1. stack.h //------------------------------------------------------------------------------ // Copyright (c) 2009 eryar All rights reserved. // // File : stack.h // Author : eryar@163.com // Date : 2009-8-22 // Version : 1.0v // // Description : stack declaration file // //============================================================================== #ifndef _STACK_H_ #define _STACK_H_ #include <iostream> using namespace std; typedef int ElemType; typedef struct Node{ ElemType data; struct Node *pointer; }Node; class Stack{ public: Stack(); // initialize stack Stack(const Stack& s); // user defined copy constructor bool Push(ElemType elem); // push : insert an element to stack top bool Pop(ElemType& elem); // pop : delete an element on stack top void Clear(); // reset the stack int Length(); // get stack element numbers bool Traverse(); // visit stack elements from bottom to top ~Stack(); // destroy stack private: Node *top; // stack top node Node *base; // stack base node }; #endif // _STACK_H_ 2. stack.cpp //------------------------------------------------------------------------------ // Copyright (c) 2009 eryar All rights reserved. // // File : stack.cpp // Author : eryar@163.com // Date : 2009-8-22 // Version : 1.0v // // Description : class stack definition // //============================================================================== #include "Stack.h" /* Parameter : none Return : none Description : class default constructor */ Stack::Stack() { cout<<"class stack default constructor ..."<<endl; base = new Node; base->pointer = NULL; base->data = 0; top = base; } // class default constructor /* Parameter : const Stack& Return : none Description : user defined constructor */ Stack::Stack(const Stack& s) { cout<<"user defined copy constructor ..."<<endl; base = new Node; base->pointer = s.base->pointer; base->data = s.base->data; top = s.top; } // user defined copy constructor /* Parameter : ElemType Return : bool Description : insert an element on top of the stack */ bool Stack::Push(ElemType elem) { Node *node = new Node; node->data = elem; top->pointer = node; node->pointer = top; top = node; base->data++; return true; } // Push /* Parameter : ElemType& Return : bool Description : kick out an element on the top of the stack */ bool Stack::Pop(ElemType& elem) { elem = top->data; top = top->pointer; base->data--; return true; } // Pop /* Parameter : none Return : int Description : get stack length */ int Stack::Length() { return base->data; } // Length /* Parameter : none Return : none Description : traverse the stack from bottom to top */ bool Stack::Traverse() { cout<<"================== stack traverse ================="<<endl; Node *bottom = base->pointer; Node *peak = top; int times = 0; for (int i = 0; i < Length(); ++i) { cout<<i<<" element is : "<<bottom->data<<endl; bottom = bottom->pointer; } return true; } // Traverse /* Parameter : none Return : none Description : class default destructor */ Stack::~Stack() { cout<<"class default destructor ... "<<endl; delete base; } // class default destructor 3. main.cpp 测试程序 //------------------------------------------------------------------------------ // Copyright (c) 2009 eryar All rights reserved. // // File : main.cpp // Author : eryar@163.com // Date : 2009-8-22 // Version : 1.0v // // Description : a stack demo promgram // //============================================================================== #include "stack.h" int main(int argc, char *argv[]) { Stack one; ElemType data = 1; cout<<"stack's length : "<<one.Length()<<endl; for (int i = 0; i < 10; ++i) { one.Push(i); } cout<<"after insert several elemnts ... "<<endl; cout<<"stack's length : "<<one.Length()<<endl; one.Traverse(); for (i = 0; i < 2; ++i) { one.Pop(data); cout<<"pop out : "<<data<<endl; } cout<<"after pop several elements ..."<<endl; cout<<"stack's length : "<<one.Length()<<endl; one.Traverse(); Stack two(one); two.Traverse(); two.Push(9); two.Traverse(); return 0; }