数据结构1-栈

  数据结构一直是我最薄弱的地方,所以要好好学习一下。

  首先是栈。

  栈有,判断栈是否为空,判断栈是否为满,出栈,入栈,取栈顶元素,这5个功能,用类实现,就是5个方法,5个成员函数。

  为方便起见,栈能容纳元素的最大值设定为固定值。元素为int型。用C++实现如下:

  

 1 //实现一个栈
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 const int Max=100;
 7 
 8 class stack
 9 {
10 private:
11     int data[Max];      //定义一个数组来存放数据
12     int top;            //指向栈顶
13 public:
14     stack();            //构造函数
15     ~stack();            //析构函数
16     bool empty()const;    
17     bool full()const;
18     void push(int);
19     void pop();
20     int GetTop();
21 };
22 
23 stack::stack()
24 {
25     top=-1;
26 }
27 
28 stack::~stack()
29 {
30 }
31 
32 bool stack::empty()const
33 {
34     return top==-1;
35 }
36 
37 bool stack::full()const
38 {
39     return top==Max-1;
40 }
41 
42 void stack::push(int x)
43 {
44     if(full())
45     {
46         cout<<"栈已满!"<<endl;
47         //exit (1);
48     }
49     else
50     {
51         top++;
52         data[top]=x;
53     }
54 }
55 
56 void stack::pop()
57 {
58     if(empty())
59     {
60         cout<<"栈为空!"<<endl;
61         //exit (1);
62     }
63     else
64     {
65         top--;
66     }    
67 }
68 
69 int stack::GetTop()
70 {
71     if(empty())
72     {
73         cout<<"栈为空!"<<endl;
74         //exit (1);
75     }
76     else
77     {
78         return data[top];
79     }
80 }
81 
82 int main()
83 {
84     stack s1;
85     s1.pop();
86     s1.push(1);
87     s1.push(2);
88     s1.push(3);
89     s1.push(4);
90     s1.push(5);
91     for(int i=0;i<5;i++)
92     {
93         cout<<s1.GetTop()<<endl;
94         s1.pop();
95     }
96     s1.pop();
97     return 0;
98 }

  在编写该栈的时候,学到了一个自己一直不知道的小细节,就是exit的用法,在60行处,如果栈为空,exit会直接退出整个程序,后面的代码将不再执行。

  上面写的栈存储的数据时int型,如果我们要存储其他类型的数据,比较麻烦,因此我用模板把栈重新写了一下。

//实现一个栈
#include<iostream>

using namespace std;

const int Max=100;

template<class T>
class stack
{
private:
    T data[Max];      //定义一个数组来存放数据
    int top;            //指向栈顶
public:
    stack();            //构造函数
    ~stack();            //析构函数
    bool empty()const;    
    bool full()const;
    void push(T);
    void pop();
    T GetTop();
};

template<class T>
stack<T>::stack()
{
    top=-1;
}

template<class T>
stack<T>::~stack()
{
}

template<class T>
bool stack<T>::empty()const
{
    return top==-1;
}

template<class T>
bool stack<T>::full()const
{
    return top==Max-1;
}

template<class T>
void stack<T>::push(T x)
{
    if(full())
    {
        cout<<"栈已满!"<<endl;
        //exit (1);
    }
    else
    {
        top++;
        data[top]=x;
    }
}

template<class T>
void stack<T>::pop()
{
    if(empty())
    {
        cout<<"栈为空!"<<endl;
        //exit (1);
    }
    else
    {
        top--;
    }    
}

template<class T>
T stack<T>::GetTop()
{
    if(empty())
    {
        cout<<"栈为空!"<<endl;
        //exit (1);
    }
    else
    {
        return data[top];
    }
}

int main()
{
    stack<char> s1;
    s1.pop();
    s1.push('a');
    s1.push('b');
    s1.push('c');
    s1.push('d');
    s1.push('e');
    for(int i=0;i<5;i++)
    {
        cout<<s1.GetTop()<<endl;
        s1.pop();
    }
    s1.pop();
    return 0;
}

  用了模板之后,stack就可以存储各种不同类型的数据。而不需要重新编写该类,非常方便。

posted on 2014-06-29 09:01  再见猪小华  阅读(184)  评论(0编辑  收藏  举报

导航