A round of stack sequential storage for postgraduate entrance exams is fully implemented and tested (Wang Zhuo version)

This blog post is derived from the data structure of Mr. Wang Zhuo at station B. The sequential storage of the stack is relatively simple to implement, so this test is published in csdn.

0, the storage structure representation of the stack

Stored in a linear table, not chained!

typedef struct{
    
    
    SElemType *base; //基地址
    SElemType *top; // 栈顶指针
    int stacksize; //栈的大小
}SqStack;

1. Create a stack

  • Allocate space for the base address, if ERROR returns in time.
  • The top of stack pointer points to the base address
  • Initialize stack size
  • return OK
Status InitStack(SqStack &S){
    
    
    S.base = new SElemType[MAXSIZE];
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = MAXSIZE;
    return OK;
}

2. Destroy the stack

  • Check if the stack is empty, if not, perform the following operations
  • free base address
  • clear stack size
  • Both the base address and the stack top pointer point to NULL
  • Finally return OK
Status DestroyStack(SqStack &S){
    
    
    if(S.base){
    
    
        delete S.base;
        S.stacksize = 0;
        S.base = S.top = NULL;
    }
    return OK;
}

3. The length of the stack

Directly return the value of the top of the stack pointer - the base address pointer.

int StackLength(SqStack S){
    
    
    return S.top - S.base;
}

4, into the stack

To check if the stack is full? Return if full, otherwise do the following

  • The top of the stack pointer is assigned the value e
  • top of stack++
  • Finally return OK
Status Push(SqStack &S,SElemType e){
    
    
    if(S.top - S.base == S.stacksize){
    
     //栈满
        return ERROR;
    }
    *S.top++ = e;
    return OK;
}

5. Pop out

To check whether the stack is empty or not, perform the following operations if it is not empty

  • The top of the stack pointer is assigned the value e
  • top of stack –
  • Finally return OK
Status Pop(SqStack &S,SElemType &e){
    
    
    if(StackEmpty(S)){
    
    
        return ERROR;
    }
    e = *--S.top;//或者--S.top;e=*S.top;
    return OK;
}

6. Empty the stack

First determine whether the base address pointer is empty, not empty, the stack top pointer points to the base address pointer, and return OK

Status  ClearStack(SqStack &S){
    
    
    if(S.base) S.top = S.base;
    return OK;
}

7. Determined to be empty

It is to determine whether the base address pointer of the top pointer field of the stack is equal

Status StackEmpty(SqStack S){
    
    
    if(S.top == S.base){
    
    
        return TRUE;
    }else
        return FALSE;
}

Test effect

insert image description here

full code

#include<iostream>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 100
using namespace std;
typedef int Status;
typedef int SElemType;
typedef struct{
    
    
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S){
    
    
    S.base = new SElemType[MAXSIZE];
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = MAXSIZE;
    return OK;
}

Status StackEmpty(SqStack S){
    
    
    if(S.top == S.base){
    
    
        return TRUE;
    }else
        return FALSE;
}

int StackLength(SqStack S){
    
    
    return S.top - S.base;
}

//清空顺序栈
Status  ClearStack(SqStack &S){
    
    
    if(S.base) S.top = S.base;
    return OK;
}

//销毁栈
Status DestroyStack(SqStack &S){
    
    
    if(S.base){
    
    
        delete S.base;
        S.stacksize = 0;
        S.base = S.top = NULL;
    }
    return OK;
}
/*顺序栈入栈
    (1) 判断是否栈满,若满则出错
    (2) 元素e压入栈顶
    (3) 栈顶指针加1;
 */
Status Push(SqStack &S,SElemType e){
    
    
    if(S.top - S.base == S.stacksize){
    
     //栈满
        return ERROR;
    }
    *S.top++ = e;
    return OK;
}
/* 顺序栈的出栈
 *  (1)判断是否栈空,若空则出错
 *  (2) 获取栈顶元素e
 *  (3) 栈顶指针减1
 * */
Status Pop(SqStack &S,SElemType &e){
    
    
    if(StackEmpty(S)){
    
    
        return ERROR;
    }
    e = *--S.top;//或者--S.top;e=*S.top;
    return OK;
}
int main(){
    
    
    SqStack  s;
    InitStack(s);
    cout << "IsEmpty? 1 represent true:" << StackEmpty(s) << endl;
    cout << "length? 0 represent empty:" << StackLength(s) << endl;
    cout << "ClearStack 1 represent success:" << ClearStack(s) << endl;
    DestroyStack(s);
    cout << "DestroyStack 0 represent that stacksize is 0:" << s.stacksize << endl;
    InitStack(s);
    for(int i=0;i<5;i++){
    
    
        Push(s,i+1);
    }
    cout << "after pushed five value ,stacksize:" << StackLength(s) << endl;
    SElemType  e;
    Pop(s,e);
    cout << "Stack top value :" << e << endl;
    return 0;

}

Guess you like

Origin blog.csdn.net/m0_37149062/article/details/123439540