栈的链式存储结构

来源:互联网 发布:360手机 知乎 编辑:程序博客网 时间:2024/05/01 17:49

栈的链式表示,即链栈。由于栈的操作是线性表操作的特例,因此链栈可以看成运算受限的单链表。其特点如下:

①链栈无栈满问题,空间可扩充;
②插入和删除仅在栈顶处执行;
③链式栈的栈顶在链头;
④适合于多栈操作。

stackList.h

#include<iostream>
class ListInt
{
public:
    ListInt() { data = 0;};
    int data;
    ListInt *link;
};
 
class StackList
{
public:
    StackList() { top=0; };
    ~StackList();
 
    bool IsEmpty() const {return top==0;}  //栈空返回1,否则返回0
    void GetTop(ListInt& x);               //获得栈顶元素
    bool Push(const ListInt& x);           //元素x入栈
    bool Pop(ListInt& x);                  //元素出栈,并保存在x中
    bool Pop();                            //元素出栈
    void ClearStack();                     //清除栈中所有元素
    void Output();                         //输出栈元素
private:
    ListInt *top;                         //指向栈顶的结点
};
.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}.codearea pre.alt{ background-color:#f7f7ff !important}.codearea .lnum{color:#4f81bd;line-height:18px}

stackList.cpp

#include <iostream>
#include "stackList.h"
using namespace std;
 
StackList::~StackList()
{
    ListInt *next;
    while(top)
    {
        next=top->link;
        delete top;
        top = next;
    }
}
 
void StackList::GetTop(ListInt& x)
{
    if(!top)
    {
        cout<<"The stack is empty!"<<endl;
        return;
    }
 
    x = *top->link;
}
 
bool StackList::Push(const ListInt &x)
{
    ListInt *listTemp = new ListInt;
    *listTemp = x;
    listTemp->link = top;
    top = listTemp;
    return true;
}
 
bool StackList::Pop(ListInt &x)
{
    if(IsEmpty())
    {
        cout<<"栈为空!"<<endl;
        return false;
    }
 
    x = *top;
    top = x.link;
    return true;
}
 
bool StackList::Pop()
{
    if(IsEmpty())
    {
        cout<<"栈为空!"<<endl;
        return false;
    }
 
    ListInt *x;
    x = top;
 
    top = x->link;
 
    delete x;
    return true;
}
 
void StackList::Output()
{
    ListInt *next;
    next = top;
    if(IsEmpty())
    {
        cout<<"栈为空!"<<endl;
        return;
    }
    while(next)
    {
        cout<<next->data<<"  ";
        next = next->link;        
    }
    cout<<endl;
}
 
void StackList::ClearStack()
{
    ListInt *next;
    while(top)
    {
        next=top->link;
        delete top;
        top = next;
    }
    top = 0;
}
.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}.codearea pre.alt{ background-color:#f7f7ff !important}.codearea .lnum{color:#4f81bd;line-height:18px}

 

.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}.codearea pre.alt{ background-color:#f7f7ff !important}.codearea .lnum{color:#4f81bd;line-height:18px}

测试文件:stack.app 

#include <iostream>
#include "stackList.h"
using namespace std;
 
int main()
{
    StackList *stackList = new StackList();
 
    ListInt x ;
    x.data = 5;
 
    stackList->Push(x);
    x.data = 6;
    stackList->Push(x);
    x.data = 8;
    stackList->Push(x);
    x.data = 62;
    stackList->Push(x);
    x.data = 12;
 
    cout<<"当前栈中元素为:"<<endl;
    stackList->Output();
 
    stackList->Pop(x);
    cout<<"出栈元素:"<<x.data<<endl;
    stackList->GetTop(x);
    cout<<"栈顶元素:"<<x.data<<endl;
    cout<<"当前栈中元素为:"<<endl;
    stackList->Output();
 
    return 0;
}
.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}.codearea pre.alt{ background-color:#f7f7ff !important}.codearea .lnum{color:#4f81bd;line-height:18px}

运行结果:

原创粉丝点击