首页 > 代码库 > 数据结构--顺序栈的实现

数据结构--顺序栈的实现

最近在看严蔚敏的数据结构,以下是参照 

http://blog.csdn.net/WLxinliang/article/details/52894338

手写的顺序栈的实现代码:

1.头文件定义了常数项

//constant.h
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int ElemType;
typedef int Status;

2.九种基本操作

//header.h
#include "constant.h"
typedef struct{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;


//构造一个空栈
Status InitStack(SqStack &S){
    S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
    if(!S.base)
        exit(OVERFLOW);
        S.top=S.base;
        S.stacksize=STACK_INIT_SIZE;
        return OK;

}

//销毁栈
Status DestroyStack(SqStack &S){
    S.top=NULL;
    S.stacksize=0;
    free(S.base);
    return OK;
}

//请空栈
Status ClearStack(SqStack &S){
    S.top=S.base;
    return OK;
}

//判断是否为空栈
Status StackEmpty(SqStack S){
    if(S.base==S.top)
        return ERROR;
    else
        return TRUE;
}

//求栈的长度
Status StackLength(SqStack S){
    if(S.top==S.base)
        return FALSE;
    else
        return (S.top-S.base);
}

//求栈顶元素
Status GetTop(SqStack S,ElemType &e){
    if(S.top==S.base)
        return FALSE;
    else
        e=*(S.top-1);
    return e;
}

//栈顶插入元素
Status Push(SqStack&S,ElemType &e){
    //如果长度超出,增加分配面积
    if(S.top-S.base>=STACK_INIT_SIZE){
        S.base=(ElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
        if(!S.base)
            return false;
        S.top=S.base+STACK_INIT_SIZE;//栈的地址可能发生改变,重新定位栈顶元素
        S.stacksize=S.stacksize+STACKINCREMENT;
    }
    *S.top=e;
    S.top++;
    return OK;
}

//删除栈顶元素
Status Pop(SqStack &S,ElemType &e){
    if(S.top==S.base)
        return ERROR;
    else{
        S.top--;
        e=*S.top;
        return e;
    }
}

//遍历栈
Status StackTraverse(SqStack S){
    if(S.base==NULL)
        return ERROR;
    if(S.top==S.base)
        cout<<"栈中没有元素"<<endl;
    ElemType *p;
    p=S.top;
    while(p>S.base){
        p--;
        cout<<*p<<" ";
    }
    return OK;
}

3.操作例子

//main.cpp
#include <iostream>
#include "header.h"
using namespace std;

int main()
{
    SqStack S;
    cout<<"构造一个空栈......"<<endl;
    InitStack(S);
    int i,n;
    cout<<"输入栈的长度:    "<<endl;
    cin>>n;
    for(i=1;i<=n;i++){
        cout<<"输入栈的第"<<i<<"个元素"<<endl;
        ++S.top;
        cin>>*(S.top-1);
    }
    cout << "……本栈是空栈吗??……" << endl;
    if (StackEmpty(S) == 1)
        cout << "NO !!!" << endl;
    else
        cout << "YES !!!" << endl;
         cout << "……求出栈的长度……" << endl;
    int m;
    m = StackLength(S);
    cout << "栈的长度是:" << endl;
    cout << m << endl;
    cout << "遍历输出栈中的所有元素:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……输出栈顶元素……" << endl;
    int e;
    e = GetTop(S, e);
    cout << "栈顶元素是:" << endl;
    cout << e << endl;
    cout << "……栈顶插入元素……" << endl;
    cout << "请输入要插入的元素的数值:" << endl;
    cin >> e;
    Push(S,e);
    cout << "现在栈中的元素是:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……栈顶删除元素……" << endl;
    e = Pop(S,e);
    cout << "被删除的元素是:" << endl;
    cout << e << endl;
    cout << "现在栈中的元素是:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……清空栈……" << endl;
    ClearStack(S);
    cout << "现在栈中的元素是:" << endl;
    StackTraverse(S);
    cout << "……销毁栈……" << endl;
    if(DestroyStack(S)==1)
        cout << "销毁栈成功" << endl;
    else
        cout << "销毁栈失败" << endl;
    cout << "恭喜您成功完成所有的功能,毕竟您那么帅!!!" << endl;
    return 0;

}

 4.案例--数制转换

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

//对于输入的一个非负十进制整数,打印输出与其等值的八进制数
int main()
{
    SqStack S;
    InitStack(S);
    int n;
    cout<<"输入一个非负十进制整数"<<endl;
    cin>>n;
    while(n){
        int m=n%8;
        Push(S,m);
        n=n/8;
    }
    cout<<"转化后的八进制为:"<<endl;
    StackTraverse(S);
    return 0;

}

 

数据结构--顺序栈的实现