关于栈

  栈是一种后进先出的数据结构,栈中允许进行插入、删除操作的一段位栈顶,另一端则称为栈底。栈中没有元素时为空栈。栈的插入删除操作分别称为进栈出栈。因此每次进栈都会放在原栈顶元素之上并成为新的栈顶,而每次出栈都会拿出当前栈顶元素,并更新栈顶。

  我认为这是一种比较重要的数据结构,因为操作系统中许多操作都是在栈中进行的。因此有必要认真看一看。

栈的操作

下面是栈操作的图示

入栈:每一次入栈,栈顶指针都必须跟随变化

 

 出栈:栈空后,栈顶指针应为-1

 

 

  这是最主要的两个操作。其余的操作还有:

    • initStack(S)     //初始化栈
    • clearStack(s)  //清空栈
    • isEmpty(S)           //看是否为空站
    • isFull(s)                //看栈是否已满
    • getTop(S, x)         //取出顶部元素,但是不改变栈

  以下是栈的所有操作

bool pop(Stack* stack, int* top);
bool push(Stack* stack, int *pushElement);
void initStack(Stack*);
void clearStack(Stack* stack);
bool isEmpty(Stack* stack);
bool isFull(Stack* stack);
bool getTop(Stack* stack, int* top);

  栈的结构

class Stack
{
private:
    int stack[Stack_Size];        //Stack_Size为栈的大小,可根据需求修改
    int topPointer;

public:
    bool pop(int *top);
    bool push(int *pushElement);
    void initStack();
    void clearStack();
    bool isEmpty();
    bool isFull();
    bool getTop(int *top);
};

栈操作的实现

//出栈
bool Stack::pop(int *top)
{
    if (this->isEmpty())
    {
        return false;
    }
    *top = this->stack[this->topPointer];
    this->topPointer--;
    return true;
}
//初始化
void Stack::initStack()
{
    this->topPointer = -1;
}
//清空
void Stack::clearStack()
{
    this->topPointer = -1;
}
//判空
bool Stack::isEmpty()
{
    if (this->topPointer < 0)
    {
        return true;
    }
    return false;
}
//入栈
bool Stack::push(int *top)
{
    if (this->isFull())
    {
        return false;
    }
    this->topPointer++;
    this->stack[topPointer] = *top;
    return true;
}
//判满
bool Stack::isFull()
{
    if (this->topPointer == Stack_Size - 1)
    {
        return true;
    }
    return false;
}
//获取栈顶元素
bool Stack::getTop(int *top)
{
    if (this->isEmpty()){

        return false;
    }
    *top = this->stack[this->topPointer];
    return true;
}

这就是栈的全部了

此外,还准备了模板栈类——主要为了强化自己面向对象能力

#include <iostream>
#include <string>
#define Stack_Size 50 //栈中个数为50
template <typename T>
class Stack
{
private:
    T stack[Stack_Size];
    int topPointer;

public:
    bool pop(T *top);
    bool push(T *pushElement);
    void clearStack();
    bool isEmpty();
    bool isFull();
    bool getTop(T *top);

    //构造函数担任初始化任务
    Stack();
    //不允许赋值
    Stack(const Stack &) = delete;
    Stack operator=(const Stack &rhs) = delete;
};
template <typename T>
bool Stack<T>::pop(T *top)
{
    if (this->isEmpty())
    {
        return false;
    }
    *top = this->stack[this->topPointer];
    this->topPointer--;
    return true;
}

template <typename T>
bool Stack<T>::push(T *pushElement)
{
    if (this->isFull())
    {
        return false;
    }
    this->topPointer++;
    this->stack[topPointer] = *pushElement;
    return true;
}

template <typename T>
void Stack<T>::clearStack()
{
    this->topPointer = -1;
}

template <typename T>
bool Stack<T>::isEmpty()
{
    if (this->topPointer < 0)
    {
        return true;
    }
    return false;
}

template <typename T>
bool Stack<T>::isFull()
{
    if (this->topPointer == Stack_Size - 1)
    {
        return true;
    }
    return false;
}

template <typename T>
bool Stack<T>::getTop(T *top)
{
    if (this->isEmpty())
    {

        return false;
    }
    *top = this->stack[this->topPointer];
    return true;
}

template <typename T>
Stack<T>::Stack() : topPointer(-1) {}

int main()
{
    Stack<std::string> aStack;
    std::string  a = "this is a test";
    aStack.push(&a);
}
posted @ 2022-03-10 21:53  帝皇の惊  阅读(403)  评论(0)    收藏  举报