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

来源:互联网 发布:大数据能做什么 编辑:程序博客网 时间:2024/06/08 19:56
  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 0xfffftemplate<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()=20mysizeof=12size()=20sizeof=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个字节

原创粉丝点击