class Stack
{
public: Stack(int
capacity_) :
elements(NULL) ,
top(0) ,
capacity(capacity_) { elements =
new int[capacity]; }
~Stack() { delete[]
elements; } bool
isFull() { return (top
>= capacity); } void
push(int val) {
elements[top] = val;
(top)++; } bool
isEmpty() { return (top
== 0); } int
pop() {
(top)--; return
(elements[top]); } int
size() { return
(top); } int
getCapacity() { return
(capacity); } void
print() { int i =
0; if (top ==
0) {
printf("stack is empty.\n"); } else {
printf("stack contents: "); for (i = 0;
i < top; i++) { printf("%d,
",elements[i]); }
printf("\n"); } }
private: int*
elements; int
top; int
capacity;
};
int main()
{ Stack
st(10); st.isEmpty()
? printf("stack is empty\n") : printf("stack is not
empty\n"); st.isFull()
? printf("stack is full\n") : printf("stack is not full\n"); for (int i =
0; i < 10; i++) {
st.push(i); printf("push
%d\n", i); } st.isEmpty()
? printf("stack is empty\n") : printf("stack is not
empty\n"); st.isFull()
? printf("stack is full\n") : printf("stack is not full\n"); for (int i =
0; i < 5; i++) { printf("pop
%d\n", st.pop()); } st.isEmpty()
? printf("stack is empty\n") : printf("stack is not
empty\n"); st.isFull()
? printf("stack is full\n") : printf("stack is not full\n");
printf("capacity of stack is %d, size of statck is %d\n",
st.getCapacity(), st.size());
st.print(); return
0;
}
============================================================================
引入异常用于检验参数和先验条件
#include <stdio.h>
#include <stdlib.h>
#include <stdexcept>
class Stack
{
public: Stack(int
capacity_)
: elements(NULL)
, top(0)
, capacity(capacity_) { if (capacity
<= 0) { throw
std::invalid_argument("capacity should be larger than 0"); }
elements = new int[capacity]; }
~Stack() {
delete[] elements; } bool
isFull() {
return (top >= capacity); } void
push(int val) { if
(isFull()) { throw
std::overflow_error("stack is full"); }
elements[top] = val;
(top)++; } bool
isEmpty() {
return (top ==
0); } int
pop() { if
(isEmpty()) { throw
std::underflow_error("stack is empty"); }
(top)--;
return (elements[top]); } int
size() {
return (top); } int
getCapacity() {
return (capacity);
}
private: int*
elements; int
top; int
capacity;
};
int main()
{ Stack
st(10); try { for (int i =
0; i < 100; i++) {
st.push(i);
printf("stack push %d\n", i); } }
catch(std::exception& ex) {
printf("excpetion: %s\n",
ex.what()); } try { for (int i =
0; i < 100; i++) {
printf("stack pop %d\n", st.pop()); } }
catch(std::exception& ex) {
printf("excpetion: %s\n",
ex.what());
} return
0;
}
======================================================================================== 使用模板
#include <stdio.h>
#include <stdlib.h>
#include <stdexcept>
template <class ElemType>
class Stack
{
public: Stack(int
capacity_)
: elements(NULL)
, top(0)
, capacity(capacity_) { if (capacity
<= 0) { throw
std::invalid_argument("capacity should be larger than 0"); }
elements = new ElemType[capacity]; }
~Stack() {
delete[] elements; } bool
isFull() {
return (top >= capacity); } void
push(const ElemType& val) { if
(isFull()) { throw
std::overflow_error("stack is full"); }
elements[top] = val;
(top)++; } bool
isEmpty() {
return (top ==
0); } ElemType
pop() { if
(isEmpty()) { throw
std::underflow_error("stack is empty"); }
(top)--;
return (elements[top]);
}
private: ElemType*
elements; int
top; int
capacity;
};
int main()
{
Stack<int> st(10); try { for (int i =
0; i < 100; i++) {
st.push(i);
printf("stack push %d\n", i); } }
catch(std::exception& ex) {
printf("excpetion: %s\n",
ex.what()); } try { for (int i =
0; i < 100; i++) {
printf("stack pop %d\n", st.pop()); } }
catch(std::exception& ex) {
printf("excpetion: %s\n",
ex.what());
} return
0;
}