当前位置:网站首页>Basic operation of sequential stack
Basic operation of sequential stack
2022-04-23 15:03:00 【White folding fan y】
Basic operation of sequence stack
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
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
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
边栏推荐
- Sqlserver transaction and lock problem
- We reference My97DatePicker to realize the use of time plug-in
- 分享3个使用工具,在家剪辑5个作品挣了400多
- Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
- Brute force of DVWA low -- > High
- Leetcode151 - invert words in string - String - simulation
- 分享 20 个不容错过的 ES6 的技巧
- 大文件如何快速上传?
- 三、梯度下降求解最小θ
- [detailed explanation of factory mode] factory method mode
猜你喜欢
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
Daily question - leetcode396 - rotation function - recursion
每日一题-LeetCode396-旋转函数-递推
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
你還不知道責任鏈模式的使用場景嗎?
The win10 taskbar notification area icon is missing
LeetCode149-直线上最多的点数-数学-哈希表
UML learning_ Day2
eolink 如何助力远程办公
QT Detailed explanation of pro file
随机推荐
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
Tencent has written a few words, Ali has written them all for a month
Svn detailed use tutorial
你還不知道責任鏈模式的使用場景嗎?
Unity_ Code mode add binding button click event
8.3 language model and data set
Go basic reflection
Introduction to dirty reading, unrepeatable reading and phantom reading
select 同时接收普通数据 和 带外数据
How to design a good API interface?
如何设计一个良好的API接口?
LeetCode151-颠倒字符串中的单词-字符串-模拟
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
买卖股票的最佳时机系列问题
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
Epolloneshot event of epoll -- instance program
We reference My97DatePicker to realize the use of time plug-in
How to upload large files quickly?
epoll 的 ET,LT工作模式———实例程序