当前位置:网站首页>Basic operation of sequential stack

Basic operation of sequential stack

2022-04-23 15:03:00 White folding fan y

Newcomer Xiaobai's blog
️ I hope you will pay more attention
It will be updated frequently in the future ~
️ Personal home page : Collection and attention , Never get lost ~


Preface

Programming to achieve the following basic operations of the stack : Build a stack , Take the top element of the stack , Push , Out of the stack .
️ Personal home page : Collection and attention , Never get lost ~
Now let's take a look at how to realize it
 Insert picture description here


One 、 Code implementation

Sequential stack is the sequential implementation of stack . Sequential stack refers to the stack realized by sequential storage structure . Use continuous address storage space ( Array ) Store the data elements in the stack in turn , Because the operation of entering and leaving the stack is carried out at the top of the stack , The position at the bottom of the stack is fixed , You can set the bottom of the stack at the beginning of the array space ; The position of the top of the stack changes with the operation of entering and exiting the stack , So you need an integer variable top To record the position of the current stack top element in the array . The stack with sequential storage structure is called sequential stack (sequence stack). Set array data[MAXSIZE] For the storage space of the stack , among MAXSIZE Is a preset constant , Is the maximum possible number of nodes allowed to stack , That is, the capacity of the stack .

The following is the code to realize the basic operation of sequential stack :

#include <iostream>

using namespace std;

// The sequential storage representation of stacks 

#define MAXSIZE 100
#define ERROR -1
#define OK 1

typedef int SElemType;
typedef int Status;

typedef struct{
    
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

// initialization 
void InitStack( SqStack &S )
{
       // Construct an empty stack 
	S.base =new SElemType[MAXSIZE];

	S.top = S.base;

	S.stacksize = MAXSIZE;

}

// Sentenced to empty 
bool StackEmpty( SqStack S )
{
    
	if(S.top == S.base) return true;
   else return false;
}

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

// Empty 
void ClearStack( SqStack &S )
{
    
	S.top = S.base;
}

// The destruction 
void DestroyStack( SqStack &S )
{
    
	if( S.base )
	{
    
		delete [] S.base ;
		S.stacksize = 0;
		S.base = S.top = NULL;
	}
}

// Push 
Status  Push ( SqStack  &S, SElemType  e)
{
       // Insert elements e For the new stack top element 
     if ( S.top-S.base==S.stacksize )
           return ERROR;
     *S.top++ = e;
     return OK;
}

// Out of the stack 
Status  Pop ( SqStack  &S, SElemType & e)
 {
       // If the stack is not empty , Delete S Top element of , use e Return its value , And back to OK; Otherwise return to ERROR
     if ( S.top == S.base )   return ERROR;
     e=*--S.top;
     return OK;
}

// Take the top element of the stack 
Status GetTop( SqStack S, SElemType &e)
{
    	//  If the stack is not empty , Then use e return S Top element of 

	if( S.top == S.base )	 return ERROR;
 	e = *( S.top - 1 );
	return OK;
}

// Display stack 
DisplayStack(SqStack &S)
{
    
    SElemType *p;
    if( S.top == S.base )	 return ERROR;
    p=S.top;
    while(p>S.base){
    
        p--;
        cout<<*p<<endl;
    }
    return OK;
}
void show_help()
{
    
    cout<<"******* Data Structure ******"<<endl;
    cout<<"1---- Empty stack "<<endl;
    cout<<"2---- Judge whether the stack is empty "<<endl;
    cout<<"3---- Find stack length "<<endl;
    cout<<"4---- Take the top element of the stack "<<endl;
    cout<<"5---- Push "<<endl;
    cout<<"6---- Out of the stack "<<endl;
    cout<<"7---- Display stack "<<endl;
    cout<<"  sign out , Input 0"<<endl;

}
int main()
{
    
    char operate_code;
    show_help();
    SqStack S;
    InitStack(S);
    SElemType e;
    int i;
    while(1)
    {
    
        cout<<" Please enter the operation code :";
        cin>>operate_code;
        if(operate_code=='1')
        {
    
            cout<<"The stack has been cleared."<<endl;
            ClearStack(S);

        }
        else if (operate_code=='2')
        {
    
            if(StackEmpty(S))
                cout<<"The stack is empty."<<endl;
            else
                cout<<"The stack is not empty."<<endl;

        }
        else if (operate_code=='3')
        {
    
            cout<<"The length of stack is:"<<StackLength(S)<<endl;

        }
        else if (operate_code=='4')
        {
    
            cout<<" The element at the top of the stack is :"<<endl;
            if(GetTop(S,e) == 1) cout<<e<<endl;
            else cout <<"error"<<endl;
        }
        else if (operate_code=='5')
        {
    
            cout<<" Please enter the number you want to insert :"<<endl;
            cin>>e;
            Push(S,e);
        }
        else if (operate_code=='6')
        {
    
            cout<<" The out of stack element is :"<<endl;
            if(Pop(S,e)) cout<<e<<endl;
        }
        else if (operate_code=='7')
        {
    
            cout<<"The contents of the stack are:"<<endl;
            DisplayStack(S);
        }
        else if (operate_code=='0')
        {
    
            break;
        }
        else
        {
    
            cout<<"\n Opcode error !!!"<<endl;
            show_help();
        }
    }
    // Call the destroy stack function 
    DestroyStack(S);
    return 0;
}

Two 、 Running results

1.1
1.2

summary

This article is used to introduce the code implementation process and running result examples of sequential stack in data structure . Display the initialization of sequence stack in menu style 、 Empty 、 The destruction 、 Sentenced to empty 、 Find the length 、 Take the top element of the stack 、 Push 、 Out of the stack and traverse the stack to display .

版权声明
本文为[White folding fan y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231410400498.html