🌍新人小白的博客
⌛️希望大家多多关注
🎃以后会经常更新哒~🙈
⭐️个人主页: 收藏加关注,永远不迷路~ ⭐️


前言

😎编程实现栈的以下基本操作:建栈,取栈顶元素,入栈,出栈。😜
⭐️个人主页: 收藏加关注,永远不迷路~ ⭐️
下面就让我们一起来看看怎么实现吧🐶
在这里插入图片描述


一、代码实现🐛

顺序栈是栈的顺序实现。顺序栈是指利用顺序存储结构实现的栈。采用地址连续的存储空间(数组)依次存储栈中数据元素,由于入栈和出栈运算都是在栈顶进行,而栈底位置是固定不变的,可以将栈底位置设置在数组空间的起始处;栈顶位置是随入栈和出栈操作而变化的,故需用一个整型变量top来记录当前栈顶元素在数组中的位置。采用顺序存储结构的栈称为顺序栈(sequence stack)。设数组data[MAXSIZE]为栈的存储空间,其中MAXSIZE是一个预先设定的常数,为允许进栈结点的最大可能数目,即栈的容量。😎

下面是实现顺序栈的基本操作的代码:🐲

#include <iostream>

using namespace std;

//栈的顺序存储表示

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

typedef int SElemType;
typedef int Status;

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

//初始化
void InitStack( SqStack &S )
{   //构造一个空栈
	S.base =new SElemType[MAXSIZE];

	S.top = S.base;

	S.stacksize = MAXSIZE;

}

//判空
bool StackEmpty( SqStack S )
{
	if(S.top == S.base) return true;
   else return false;
}

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

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

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

//入栈
Status  Push ( SqStack  &S, SElemType  e)
{   //插入元素e为新的栈顶元素
     if ( S.top-S.base==S.stacksize )
           return ERROR;
     *S.top++ = e;
     return OK;
}

//出栈
Status  Pop ( SqStack  &S, SElemType & e)
 {   //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
     if ( S.top == S.base )   return ERROR;
     e=*--S.top;
     return OK;
}

//取栈顶元素
Status GetTop( SqStack S, SElemType &e)
{	// 若栈不空,则用e返回S的栈顶元素

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

//显示栈
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----清空栈"<<endl;
    cout<<"2----判断栈是否为空"<<endl;
    cout<<"3----求栈长度"<<endl;
    cout<<"4----取栈顶元素"<<endl;
    cout<<"5----入栈"<<endl;
    cout<<"6----出栈"<<endl;
    cout<<"7----显示栈"<<endl;
    cout<<"     退出,输入0"<<endl;

}
int main()
{
    char operate_code;
    show_help();
    SqStack S;
    InitStack(S);
    SElemType e;
    int i;
    while(1)
    {
        cout<<"请输入操作代码:";
        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<<"栈顶元素为:"<<endl;
            if(GetTop(S,e) == 1) cout<<e<<endl;
            else cout <<"error"<<endl;
        }
        else if (operate_code=='5')
        {
            cout<<"请输入你想要插入的数:"<<endl;
            cin>>e;
            Push(S,e);
        }
        else if (operate_code=='6')
        {
            cout<<"出栈元素为:"<<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操作码错误!!!"<<endl;
            show_help();
        }
    }
    //调用销毁栈函数
    DestroyStack(S);
    return 0;
}

二、运行结果🐬

1.1
1.2

总结🐶

本文用来介绍数据结构中顺序栈的代码实现过程及运行结果示例。用菜单样式显示顺序栈的初始化、清空、销毁、判空、求长度、取栈顶元素、入栈、出栈以及遍历栈使其显示等操作。🚀🚀🚀

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐