【算法和数据结构】1.5--数据结构之栈

来源:互联网 发布:莎莎源码 解压密码 编辑:程序博客网 时间:2024/05/22 00:38

      在前面和大家分享了两种线性表(List)结构:链表和队列。今天和大家分享List的最后一种结构—栈(stack)。

      首先,给出栈的一个通常定义:一个运算受限的线性表(A restricted form of list): Insert and remove only at front of list. 其具有重要特征:LIFO,Last in,first out.

      我们在栈上定义两种操作:

  • 插入:PUSH,向栈插入元素
  • 删除:POP,删除栈中指定的元素

      我们将栈中当前可操作元素称之为Top元素。

      栈由于其LIFO性质有很多实际应用(或者我们可以说栈是根据实际应用而设计具有LIFO性质的线性表),这里不再一一列举,有兴趣的读者可以查找相关资料。

      下面给出栈的C++代码实现:

//Array based stack: First in, last outclass AStack{private:    int* stackArray;    int maxSize;    int top;  //the position to insert a new elementpublic:    AStack(int size)    {        maxSize = size;        top = 0;        stackArray = new int[maxSize];    }    ~AStack()    {        clear();        delete[] stackArray;    }    void clear()    {        top = 0;    }    int getElementNum()    {        return top;    }    bool push(const int& valuePush)    {        if (top == maxSize)            return false;        //a full stack        else        {            stackArray[top++] = valuePush;            return true;        }    }    //Push an element on the top of the stack    bool pop(int& valuePop)    {        if (top == 0)            return false;        //an empty stack        else        {            valuePop = stackArray[--top];            return true;        }    }    //Remove the top element of the stack and put it in "valuePop"    bool topValue(int& valueTop)    {        if (top == 0)            return false;        else        {            valueTop = stackArray[top - 1];            return true;        }    }    void print()    {        if (top == 0)            cout << "This is an empty stack" << endl;        else        {            for (int i = 0;i < top;i++)                cout << stackArray[i] << " ";            cout << endl;        }    }};//Linked stackclass StackNode{public:    int element;  //Value of this node    StackNode* next;  //Pointer to next node    StackNode(const int & elementValue, StackNode* nextNode)    {        element = elementValue;        next = nextNode;    }};class LStack{private:    StackNode* top;    int size;  //Count of elementspublic:    LStack()    {        top = NULL;        size = 0;    }    ~LStack()    {        clear();    }    void clear()    {        while (top != NULL)        {            StackNode* temp = top;            top = top->next;            delete temp;        }        size = 0;    }    int getSize()    {        return size;    }    bool push(const int& valuePush)    {        top = new StackNode(valuePush, top);        size++;        return true;    }    bool pop(int& valuePop)    {        if (size == 0)            return false;        valuePop = top->element;        StackNode* temp = top->next;        delete top;        top = temp;        size--;        return true;    }    bool topValue(int& valueTop) const    {        if (size == 0)            return false;        valueTop = top->element;        return true;    }    void print()    {        if (size == 0)            cout << "This is an empty stack" << endl;        else        {            StackNode* temp = top;            while (temp != NULL)            {                cout << temp->element << " ";                temp = temp->next;            }            cout << endl;        }    }};

以上为本人关于栈的一些学习心得,欢迎广大读者朋友批评指正。

1 0
原创粉丝点击