当前位置:网站首页>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
边栏推荐
- PCIe X1 插槽的主要用途是什么?
- Is asemi ultrafast recovery diode interchangeable with Schottky diode
- On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
- 8.3 language model and data set
- [NLP] HMM hidden Markov + Viterbi word segmentation
- What is the main purpose of PCIe X1 slot?
- async void 导致程序崩溃
- LeetCode149-直线上最多的点数-数学-哈希表
- eolink 如何助力遠程辦公
- LeetCode167-两数之和II-双指针-二分-数组-查找
猜你喜欢
8.5 concise implementation of cyclic neural network
PCIe X1 插槽的主要用途是什么?
The art of automation
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
Is asemi ultrafast recovery diode interchangeable with Schottky diode
we引用My97DatePicker 实现时间插件使用
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
Introduction to distributed transaction Seata
Leetcode165 compare version number double pointer string
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
随机推荐
Realization of four data flow modes of grpc based on Multilingual Communication
Leetcode151 - invert words in string - String - simulation
thinkphp5+数据大屏展示效果
Swift protocol Association object resource name management multithreading GCD delay once
Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
When splicing HQL, the new field does not appear in the construction method
分布式事务Seata介绍
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
LeetCode149-直线上最多的点数-数学-哈希表
Unity_ Code mode add binding button click event
解决computed属性与input的blur事件冲突问题
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
JUC learning record (2022.4.22)
go基础 反射
每日一题-LeetCode396-旋转函数-递推
如何设计一个良好的API接口?
epoll 的EPOLLONESHOT 事件———实例程序
Explain TCP's three handshakes in detail