C++栈的用法及栈的实现

  1. 首先看一下原c++栈的方法的基本用法: 
    1. push(): 向栈内压入一个成员;
    2. pop(): 从栈顶弹出一个成员;
    3. empty(): 如果栈为空返回true,否则返回false;
    4. top(): 返回栈顶,但不删除成员;
    5. size(): 返回栈内元素的大小;
  2. 代码示例:
#include<iostream>
#include<stack>
using namespace std;

int main()
{
    stack <int>stk;
    //入栈
    for(int i=0;i<50;i++){
        stk.push(i);
    }
    cout<<"栈的大小:"<<stk.size()<<endl;
    while(!stk.empty())
    {
        cout<<stk.top()<<endl;
        stk.pop();
    }
    cout<<"栈的大小:"<<stk.size()<<endl;
    return 0;
}


 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  1. 接下来我们自己写栈,这时就需要用到c++中的模板类(template)
#include<iostream>
#include<stdlib.h>
using namespace std;

#define MAXSIZE 0xffff

template<class type>
class my_stack
{
    int top;
    type* my_s;
    int maxsize;

public:
    my_stack():top(-1),maxsize(MAXSIZE)
    {
        my_s=new type[maxsize];
        if(my_s==NULL)
        {
            cerr<<"动态存储分配失败!"<<endl;
            exit(1);
        }
    }
    my_stack(int size):top(-1),maxsize(size)
    {
        my_s=new type[maxsize];
        if(my_s==NULL)
        {
            cerr<<"动态存储分配失败!"<<endl;
            exit(1);
        }
    }
    ~my_stack()
    {
        delete[] my_s;
    }
    //是否为空
    bool Empty();
    //压栈
    void Push(type tp);
    //返回栈顶元素
    type Top();
    //出栈
    void Pop();
    //栈大小
    int Size();
};

template<class type>
bool my_stack<type>::Empty()
{
    if(top==-1){
        return true;
    }
    else
        return false;
}

template<class type>
type my_stack<type>::Top()
{
    if(top!=-1)
    {
        return my_s[top];
    }
    else
    {
        cout<<"栈空\n";
        exit(1);
    }
}

template<class type>
void my_stack<type>::Push(type tp)
{
    if(top+1<maxsize)
    {
        my_s[++top]=tp;
    }
    else
    {
        cout<<"栈满\n";
        exit(1);
    }
}

template<class type>
void my_stack<type>::Pop()
{
    if(top>=0)
    {
        top--;
    }
    else
    {
        cout<<"栈空\n";
        exit(1);
    }
}

template<class type>
int my_stack<type>::Size()
{
    return top+1;
}


 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  1. 然后就可以在另一个cpp文件中使用它了(记得include):
#include<iostream>
#include "my_stack.cpp"

using namespace std;

int main()
{
    my_stack<int> stk;
    for(int i=0;i<50;i++){
        stk.Push(i);
    }
    cout<<"栈的大小:"<<stk.Size()<<endl;
    while(!stk.Empty())
    {
        cout<<stk.Top()<<endl;
        stk.Pop();
    }
    cout<<"栈的大小:"<<sizeof(stk)<<endl;
    return 0;
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  1. 在编写代码的时候我突然很好奇,size()和sizeof输出的区别,然后我用我写的栈做了尝试:
#include<iostream>
#include<stack>
#include "my_stack.cpp"

using namespace std;

int main()
{
    my_stack<int> stk;
    stack<int> s;
    for(int i=0;i<20;i++){
        stk.Push(i);
        s.push(i);
    }
    cout<<"mysize()="<<stk.Size()<<"\nmysizeof="<<sizeof(stk)<<endl;
    cout<<"size()="<<s.size()<<"\nsizeof="<<sizeof(s)<<endl;
    return 0;
}

输出:
mysize()=20
mysizeof=12
size()=20
sizeof=40
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

并且可以看到我写的栈类的变量只有三个整型(一个template型),刚好12个字节,由此可知c++提供的栈内不止我写的这么简单,光变量就占40个字节

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值