当前位置:网站首页>Stack-------

Stack-------

2022-07-01 12:23:00 Between the steps

Order of the stack , use Sequential storage Implementation stack , Its basic operation : Additions and deletions , Sentenced to empty , Full sentence operation

1. The first is initialization :

#include<stdio.h>
#define Maxsize 10 // The maximum number of elements in the stack 
typedef struct{
    
ElemType data[Maxsize];  // Stack elements in static arrays 
int top;                 // Top pointer of stack 

}SqStack;

void InitStack(SqStack &S){
    
    S.top=-1;    // Initialize the stack top pointer 

}

bool StackEmpty(SqStack S){
    
    if(S.top==-1)
        return true;     // The stack is empty 
    else
        return false;
}

void testStack{
    
    SqStack S;  // Declare a sequential stack , Allocate space for it 
}

int main(){
    
    SqStack S;
    InitStack(S);
    // Subsequent addition, deletion, modification and query 
}

 Insert picture description here

2. Stack in operation
First judge whether the stack is full , If you are dissatisfied, let the pointer at the top of the stack add 1, Then put new elements on the stack

bool push(SqStack &S,ElemType x){
    
    if(S.top==Maxsize-1)   // Stack full , Report errors 
        return false;
        S.top=S.top+1;  // The pointer first adds 1
        S.data[S.top]=x;   // Put new elements on the stack 
        return true;
}

 Insert picture description here

3. The stack,

bool Pop(SqStack &S,ElemType &x){
    
    if(S.top==-1)      
        return false;   // The stack is empty ,, Report errors 
    x=S.data[S.top];  // The top element of the stack goes out of the stack first 
    S.top=S.top-1;  // Pointer minus 1
    return true;

}

 Insert picture description here
4. Read the top element of the stack

bool GetTop(SqStack S,ElemType &x){
    
    if(S.top==-1)
        return false;
    x=S.data[S.top];
    return true;
}

 Insert picture description here

Shared stack

Shared stack

The shared stack has pointers at the bottom and top of the stack

#include<stdio.h>
#define Maxsize 10 // The maximum number of elements in the stack 
typedef struct{
    
ElemType data[Maxsize];  // Stack elements in static arrays 
int top0;                 //0 No. stack top pointer 
int top1;                 //1 No. stack top pointer 

}SqStack;

void InitStack(SqStack &S){
    
    S.top0=-1;    // initialization top0 Top pointer of stack 
    S.top1=Maxsize;  // initialization top1 Top pointer of stack 

}

 Insert picture description here

Conditions for stack full :top0+1==top1 ?

Chain stack

Chain stack

The chain stack is similar to the header insertion of a single chain table , The one inserted after is on the right Such as
 Insert picture description here
So we just need to stack at the head of the chain
 Insert picture description here

typedef struct{
    
ElemType data;    
struct LinkNode *next;              

}LinkNode,*LiStack;

Like a single chain watch

原网站

版权声明
本文为[Between the steps]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011204421806.html