정적 스 택

가장 범람 하 는 것 도 아마도 데이터 구조 에서 가장 간단 한 것 일 것 이다.할 말 없어.코드 를 직접 보 세 요.
/*  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;
}

좋은 웹페이지 즐겨찾기