栈(C++)

简介:

限定仅在表尾进行插入或删除操作的线性表
表尾端称为栈顶(top),表头端称为栈底(bottom)

特点:

在栈中,后入栈的元素先出栈

C语言版本

用于测试的文件,以及测试结果可以去作者GitHub上查看

 (下面代码只是最初实现,后续如果有修改,由于太麻烦,不会再更改下文,仅仅更新Github上的代码文件)

具体实现:

#define StackDataType int
#define InitStackData 12345

class StackNode {
public:
    StackNode() {
        data = InitStackData;
        next = nullptr;
    }

    StackNode(StackDataType ele) : data(ele), next(nullptr) {

    }

    StackDataType data;
    StackNode *next;
};

// 栈(链式,长度)
class Stack {
public:
    Stack();
    Stack(int len);    // 创建一个有着len个栈元素的栈
    ~Stack();

    // 栈的初始化
    void StackInit();

    // 销毁栈
    void StackDestroy();

    // 将栈清空
    void StackClear();

    // 判断是否为空栈
    bool StackEmpty();

    // 获取栈长度,即栈中元素个数
    int StackLength();

    // 获取栈顶元素保存的数据值
    bool StackGetTop(StackDataType &value);

    // 压栈,插入新的栈顶元素
    bool StackPush(StackDataType value);
    
    // 出栈,删除栈顶元素
    bool StackPop();
    bool StackPop(StackDataType &value);    // 出栈并返回其值

    // 遍历栈,从栈底到栈顶
    void StackTraverse();

private:
    StackNode *top;    // 表尾 栈顶
    StackNode *bottom; // 表头 栈底
};
Stack::Stack()
{
    StackInit();
}

Stack::Stack(int len)
{
    StackInit();
    StackDataType tempdata = InitStackData;

    for (int i = 0; i < len; i++) {
        std::cin >> tempdata;
        StackPush(tempdata);
    }
}

Stack::~Stack()
{
    StackClear();
    StackDestroy();
}

void Stack::StackInit()
{
    StackNode *temp = new(std::nothrow) StackNode;

    if (temp == 0) {
        std::exit(1);
    }

    top = temp;
    bottom = temp;
}

void Stack::StackDestroy()
{
    delete(bottom);
}

void Stack::StackClear()
{
    if (bottom == top) {    // 已经是空栈
        return;
    }

    StackNode *cur = bottom->next;
    StackNode *next = cur->next;

    while (cur != nullptr) {    // 释放栈元素结点空间
        delete(cur);

        if (next == nullptr) {
            break;
        }

        cur = next;
        next = next->next;
    }

    top = bottom;
}

bool Stack::StackEmpty()
{
    return (bottom == top) ? true : false;
}

int Stack::StackLength()
{
    int cnt = 0;
    StackNode *cur = bottom;

    while (cur != top) {
        cnt++;
        cur = cur->next;
    }

    return cnt;
}

bool Stack::StackGetTop(StackDataType &value)
{
    if (bottom == top)
        return false;    // 空栈无栈顶元素

    value = top->data;

    return true;
}

bool Stack::StackPush(StackDataType value)
{
    StackNode *temp = new(std::nothrow) StackNode(value);

    if (temp == 0) {
        return false;
    }

    top->next = temp;
    top = temp;

    return true;
}

bool Stack::StackPop()
{
    if (top == bottom)    // 空栈无栈顶元素,无法pop
        return false;

    StackNode *prior = bottom;

    while (prior->next != top) {    //  找到栈顶第二个元素
        prior = prior->next;
    }

    delete(top);
    top = prior;
    top->next = nullptr;  // 注意此处

    return true;
}

bool Stack::StackPop(StackDataType &value)
{
    if (top == bottom)    // 空栈无栈顶元素,无法pop
        return false;

    StackNode *prior = bottom;

    while (prior->next != top) {    //  找到栈顶第二个元素
        prior = prior->next;
    }

    value = top->data;
    delete(top);
    top = prior;
    top->next = nullptr;  // 注意此处

    return true;
}

void Stack::StackTraverse()
{
    StackNode *cur = bottom->next;

    while (cur != top) {    
        std::cout << cur->data << std::endl;
        cur = cur->next;
    }

    std::cout << top->data << std::endl;
}

猜你喜欢

转载自www.cnblogs.com/lnlin/p/10887091.html