C++的Stack实现

    初始版本,未引入异常,未将声明与实现分开
#include <stdio.h>
#include <stdlib.h>

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值