データ構造 - C ++の実装スタックのシーケンス

スタックCのパッケージング配列++

スタックの基本的な操作を達成するためのシンプルなパッケージのためのC ++でのスタックの順番、
カプセル化方法を:ポップ()、トップ() 、サイズ()、空()、プッシュ()

コードはテストされています

#pragma once
#include <iostream>
#include <algorithm>
using namespace std;
template<class T> class Stack {
public:
        Stack();       //构造函数
   void pop();         //弹出头元素
   void push(T value); //入栈
   bool empty();       //判断是否为空栈
   int  size();        //返回栈的大小
     T  top();         //获取首元素

private:
    T arr[100];
    int head;
    int tail;
    int len;
};

template<class T>
inline Stack<T>::Stack()
{
    this->head = 0;
    this->tail = 0;
    this->len = 0;
}

template<class T>
inline void Stack<T>::pop()
{
    this->head--;
    this->len--;
}

template<class T>
inline void Stack<T>::push(T value)
{
    head++;
    arr[head] = value;
    this->len++;
}

template<class T>
inline bool Stack<T>::empty()
{
    if(this->len == 0)
    return true;
    else return false;
}

template<class T>
inline int Stack<T>::size()
{
    return this->len;
}

template<class T>
inline T Stack<T>::top()
{
    return T(arr[head]);
}

あなたが私に置かれた任意の質問がある場合は、歓迎の首長が問題を指摘し、ああQQを追加することができます。
あなたがあなたの助けを感じる場合は、私に賞賛のポイントを与え、次回は私が^ _ ^それを書くのパワーを点灯しました!

おすすめ

転載: www.cnblogs.com/wlw-x/p/11609672.html