C语言实现顺序栈的基本操作

前言

本程序是数据结构上机实验内容,参考《数据结构(C语言版)》(清华大学出版社)中链表部分的伪代码实现。

题目要求

设计一个抽象数据类型栈的顺序表示和实现的演示程序,其基本操作有初始化栈、判栈空否、入栈、出栈等功能。

伪代码

#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef struct{
    SElemType *base;
    SElemtype *top;
    int stacksize;
}SqStack;
Status InitStack(SqStack &S);
Status DestoyStack(SqStack &S);
Status ClearStack(SqStack &S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack S,SElemType &e); 
Status Push (SqStack &S,SElemType e);
Status Pop (SqStack &S,SElemType &e);
Status StrackTraverse(SqStack S,Status(*visit)());
Status InitStack(SqStack &S){
    S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if(!S.base) exit (OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}
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;
    return OK;
}

实例代码及说明

#include <stdio.h>
#include <stdlib.h>

#define STACK_INIT_SIZE 100     //栈的初始空间大小
#define STACKINCREMENT 10       //每次扩充栈的空间大小

#define OK          1
#define ERROR       0
#define OVERFLOW    -1

typedef int Status ;
typedef int SElemType ;

//栈的结构体
typedef struct SqStack{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

//初始化栈
Status InitStack(SqStack *S){
    S->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if(!S->base) exit(OVERFLOW);
    S->top=S->base;
    S->stacksize=STACK_INIT_SIZE;
    return OK;
}

//获取栈顶数据
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->base=&S->stacksize;
        S->top=&S->stacksize;
        S->stacksize+=STACKINCREMENT;
    }
    *S->top++=e;
    return OK;
}

//出栈
Status Pop(SqStack *S,SElemType *e){
    if (S->top==S->base) return ERROR;
    --S->top;
    *e=*S->top;
    return OK;
}

//获取栈内数据长度
int StackLength(SqStack *S){
    return S->top-S->base;
}

int main(){
    int i,v;

    //定义栈并初始化
    SqStack S;
    InitStack(&S);

    //存入初始数据
    printf("请输入初始存入栈的数据个数:");
    scanf("%d",&i);

    while(i--){
        printf("入栈数据:");
        scanf("%d",&v);
        if(!Push(&S,v)){
            printf("入栈失败.");
        }
    }

    //输出栈的属性
    printf("栈的长度:%d\n",StackLength(&S));
    printf("栈的容量:%d\n",S.stacksize);

    //出栈功能演示
    while(StackLength(&S)){
        if(Pop(&S,&v)){
            printf("出栈数据:%d\n",v);
        }else{
            printf("出栈失败.\n");
        }
    }

    system("pause");
    return 0;
}

运行结果

运行结果

猜你喜欢

转载自blog.csdn.net/lgj123xj/article/details/72179312