Sequential storage representation of stack

Sequential stack, that is, the sequential storage structure of the stack, uses a group of storage units with continuous addresses to store data elements from the bottom of the stack to the top of the stack in turn, while the pointer top is attached to indicate the location of the top of the stack elements in the sequential stack. The common practice is to use top=0 for empty stacks. Generally speaking, the maximum capacity of the stack should not be limited when initializing the empty stack. A more reasonable approach: first allocate a basic capacity for the stack, and then expand when the stack space is insufficient in the application process. The representation of empty stack is S.top == S.base.

///Sequential storage representation of stack
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
using namespace std;
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
typedef int SElemType;
typedef struct
{
    SElemType *base;//base is NULL before stack construction and after destruction
    SElemType *top;//Top pointer of stack
    int stacksize;//Currently allocated storage space in elements
} SqStack;

//Construct an empty stack S
Status InitStack(SqStack &S);
//Destroy stack S, stack S no longer exists
Status DestroyStack(SqStack &S);
//Set S as empty stack
Status ClearStack(SqStack &S);
//Returns true if S is an empty stack, FALSE otherwise
Status StackEmpty(SqStack S);
//Returns the number of elements of S and the length of the stack
int StackLength(SqStack S);
/*Get the top element of the stack,
If the stack is not empty, e is used to return the top element of S, and OK is returned,
Otherwise, ERROR is returned*/
Status GetTop(SqStack S, SElemType &e);
//Insert element e as a new stack top element
Status Push(SqStack &S, SElemType e);
//If the stack is not empty, delete the stack top element of S, return its value with e, and return OK; otherwise, return ERROR
Status Pop(SqStack &S, SElemType &e);
//Read element
Status vi(SElemType e);
//From the bottom of the stack to the top of the stack, call the function visit() for each element in the stack. Once the visit() fails, the operation fails
Status StackTraverse(SqStack S, Status(*visit)(SElemType));
int main()
{
    SqStack S;
    InitStack(S);
    if(StackEmpty(S))
    {
        printf("After creating the stack, The stack is empty.\n");
    }
    for(int i = 0; i < 6; i++)
    {
        Push(S, i);
    }
    if(!StackEmpty(S))
    {
        printf("After adding elements, Stack is not empty.\n");
    }
    StackTraverse(S, vi);//Traversal of elements in the stack
    int length;//Length of stack
    length = StackLength(S);
    printf("Length of stack: %d\n", length);
    SElemType e;
    GetTop(S, e);
    printf("Top element of stack: %d\n", e);
    Pop(S, e);
    printf("Deleted top of stack element: %d\n", e);
    StackTraverse(S, vi);//Traversal of elements in the stack
    ClearStack(S);
    if(StackEmpty(S))
    {
        printf("After clearing the stack, The stack is empty.\n");
    }
    DestroyStack(S);
    return 0;
}
//Construct an empty stack S
Status InitStack(SqStack &S)
{
    S.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base)
    {
        printf("Storage allocation failed!\n");
        return ERROR;
    }
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}
//Destroy stack S, stack S no longer exists
Status DestroyStack(SqStack &S)
{
    S.base = NULL;
    free(S.base);//Release S.base
    S.top = NULL;//Leave the element pointing to the top of the stack empty
    S.stacksize = 0;//Stack destroyed, space freed, currently allocated storage empty
    return OK;
}
//Set S as empty stack
Status ClearStack(SqStack &S)
{
    S.top = S.base;
    return OK;
}
//Returns true if S is an empty stack, FALSE otherwise
Status StackEmpty(SqStack S)
{
    if(S.top == S.base)
    {
        return TRUE;
    }
    return FALSE;
}
//Returns the number of elements of S and the length of the stack
int StackLength(SqStack S)
{
    return S.top - S.base;
}
/*Get the top element of the stack,
If the stack is not empty, e is used to return the top element of S, and OK is returned,
Otherwise, ERROR is returned*/
Status GetTop(SqStack S, SElemType &e)
{
    if(S.base == S.top)
    {
        return ERROR;
    }
    e = *(S.top - 1);
    return OK;
}
//Insert element e as a new stack top element
Status Push(SqStack &S, SElemType e)
{
    if(S.top - S.base >= S.stacksize)//Stack full, additional storage space
    {
        S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(SElemType));
    }
    if(!S.base)
    {
        return ERROR;//Storage allocation failed
    }
    *S.top = e;
    S.top++;
    return OK;
}
//If the stack is not empty, delete the stack top element of S, return its value with e, and return OK; otherwise, return ERROR
Status Pop(SqStack &S, SElemType &e)
{
    if(S.top == S.base)
    {
        return ERROR;
    }
    S.top--;
    e = *S.top;
    return OK;
}
//Read element
Status vi(SElemType e)
{
    printf("%d ", e);
    return OK;
}
//From the bottom of the stack to the top of the stack, call the function visit() for each element in the stack. Once the visit() fails, the operation fails
Status StackTraverse(SqStack S, Status(*visit)(SElemType))
{
    SElemType e;
    printf("Traversal of elements in the stack: ");
    while(S.top != S.base)
    {
        S.top--;
        e = *S.top;
        if(!(*visit)(e))
        {
            return ERROR;
        }
    }
    printf("\n");
    return OK;
}

Posted by jackdcrack on Wed, 22 Jan 2020 08:05:25 -0800