顺序栈

Posted sgawscd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顺序栈相关的知识,希望对你有一定的参考价值。

 1 //Stack.h
 2 
 3 #ifndef STACK_H
 4 #define STACK_H
 5 
 6 #define OK 1
 7 #define ERROR 0
 8 typedef int Status;
 9 typedef int SElemType;
10 #define STACK_INIT_SIZE 100
11 #define STACKINCREMENT 10
12 typedef struct {
13     SElemType *base;//栈底指针
14     SElemType *top; //栈顶指针
15     int stacksize;  //当前已分配的存储空间
16 }SqStack;
17 Status InitStack(SqStack &S);
18 Status DestoryStack(SqStack &S);
19 Status ClearStack(SqStack &S);
20 Status StackEmpty(SqStack &S);
21 int StackLength(SqStack S);
22 Status GetTop(SqStack S, SElemType &e);
23 Status Push(SqStack &S, SElemType e);
24 Status Pop(SqStack &S, SElemType &e);
25 Status StackTraverse(SqStack S);
26 
27 #endif
//Stack.cpp

#include"Stack.h"
#include<iostream>
#include<cstdlib>
using namespace std;

Status InitStack(SqStack &S)
{
    S.base = (SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
    if (!S.base)
        exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}
Status DestoryStack(SqStack &S)
{
    S.top = S.base;
    free(S.base);
    S.stacksize = 0;
    return OK;
}
Status ClearStack(SqStack &S)
{
    S.top = S.base;
    return OK;
}
Status StackEmpty(SqStack &S)
{
    if (S.top == S.base)
        return OK;
    return ERROR;
}
int StackLength(SqStack S)
{
    return (S.top - S.base)/sizeof(SElemType);
}
Status GetTop(SqStack S, SElemType &e)
{
    if (S.top == S.base)
        return ERROR;
    e = *(S.top - 1);
    return OK;
}
Status Push(SqStack &S, SElemType e)
{
    if (S.top - S.base >= S.stacksize)//追加栈的存储空间
    {
        S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
        if (!S.base)
            exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return OK;
}
Status Pop(SqStack &S, SElemType &e)
{
    if (S.top == S.base)
        return ERROR;
    e = *--S.top;      //*S.top并没有指向元素,S.top指向栈顶元素的下一个位置
    return OK;
}
Status StackTraverse(SqStack S)
{
    if (S.top == S.base)
    {
        cout << "栈为空"<<endl;
        return ERROR;
    }
    SElemType *p = S.base;
    cout << "栈底 |";
    for (p = S.base; p < S.top; p++)
        cout << *p<<" ";
    cout << endl;
    return OK;
}
//Main.cpp

#include"Stack.h"
#include<iostream>
using namespace std;

int main()
{
    SqStack S;

    SElemType temp = 0;
    int choose = 10;
    
    while (choose != 0)
    {
        switch (choose)
        {
        case 1:
            InitStack(S);
            if(S.base)
                cout << "构造栈成功" << endl;
            break;
        case 2:
            DestoryStack(S);
            cout << "销毁栈成功" << endl;
            break;
        case 3:
            ClearStack(S);
            cout << "清空栈成功" << endl;
            break;
        case 4:
            if (StackEmpty(S))
                cout << "栈为空"<<endl;
            else
                cout << "栈不为空"<<endl;
            break;
        case 5:
            cout<<StackLength(S);
            break;
        case 6:
            GetTop(S,temp);
            cout << "栈顶元素为:"<<temp<<endl;
            break;
        case 7:
            cout << "输入要插入的元素: ";
            cin >> temp;
            Push(S, temp);
            break;
        case 8:
            if (S.base == S.top)
                cout << "栈已空,无法弹出元素" << endl;
            else
            {
                Pop(S, temp);
                cout <<"弹出 "<< temp<<endl;
            }
            break;
        case 9:
            StackTraverse(S);
            break;
        default:
            cout << "|**顺序栈**|" << endl;
            cout << "|1.构造栈 *|" << endl;
            cout << "|2.销毁栈 *|" << endl;
            cout << "|3.置空栈 *|" << endl;
            cout << "|4.栈空否 *|" << endl;
            cout << "|5.栈长度 *|" << endl;
            cout << "|6.栈顶值 *|" << endl;
            cout << "|7.插入栈 *|" << endl;
            cout << "|8.弹出栈 *|" << endl;
            cout << "|9.遍历栈 *|" << endl;
            cout << "|0.退出    |" << endl;
            break;
        }    
        cout << "input 0~9" << endl;
        cin >> choose;
    }
    system("pause");
    return 0;
}

 

以上是关于顺序栈的主要内容,如果未能解决你的问题,请参考以下文章

html 将以编程方式附加外部脚本文件的javascript代码片段,并按顺序排列。用于响应式网站,其中ma

考研数据结构模板:顺序表链表栈队列

C++ class实现顺序栈(完整代码)

数据结构--栈(Stack)

数据结构--栈(Stack)

数据结构_顺序栈的代码实践