スタティックスタック


最も氾濫しているのもおそらくデータ構造の中で一番簡単です.何も言いません.コードを自分で見ましょう
/*  Author : Moyiii
 *  Mail:  [email protected]
 *        
 *        ,         ,    。
 *      BUG,        ,      
*/

#include<iostream>
using namespace std;

const int maxSize = 50;


//      ,    ……,      
class SeqStack
{
public:
    SeqStack();
    void push(const int x);
    void pop();
    void getTop(int &x);
    void clear();
    bool isEmpty();
    int getLength();
    bool isFull();
    void print();

private:
    int elems[maxSize];
    int top;
};

SeqStack :: SeqStack()
{
    top = -1;
}

void SeqStack :: push(int x)
{
    if(top == maxSize - 1)
    {
        cout << "Space is Full!" << endl;
        return;
    }

    elems[++top] = x;
    return;
}

void SeqStack :: getTop(int &x)
{
    if(top == -1)
    {
        cout << "No elems in the stack!" << endl;
        return;
    }

    x = elems[top];
    return;
}

void SeqStack :: pop()
{
    if(top == -1)
    {
        cout << "No elems in the stack!" << endl;
        return;
    }
    top--;
}

bool SeqStack :: isEmpty()
{
    return (top == -1);
}

bool SeqStack :: isFull()
{
    return (top == maxSize - 1);
}

int SeqStack :: getLength()
{
    return (top + 1);
}

void SeqStack :: clear()
{
    top = -1;
}

void SeqStack :: print()
{
    cout << "The Stack is:" << endl;
    for(int i = top; i >=0; --i)
    {
        cout << "| " << elems[i] << " |";
        if(i == top)
        {
            cout << "←TOP";
        }
        cout << endl;
    }
}

int main()
{
    SeqStack s;
    for(int i = 1; i <= 10; ++i)
    {
        s.push(i);
    }
    s.print();
    for(int i = 1; i <= 5; ++i)
    {
        s.pop();
    }
    s.print();
    return 0;
}