当前位置:网站首页>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
边栏推荐
- Introduction to dirty reading, unrepeatable reading and phantom reading
- epoll 的EPOLLONESHOT 事件———实例程序
- Little red book timestamp2 (2022 / 04 / 22)
- OPPO数据湖统一存储技术实践
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- epoll 的 ET,LT工作模式———实例程序
- Thread synchronization, life cycle
- On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
- Leetcode162 - find peak - dichotomy - array
- How to write the keywords in the cover and title? As we media, why is there no video playback
猜你喜欢
编程哲学——自动加载、依赖注入与控制反转
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
eolink 如何助力远程办公
1n5408-asemi rectifier diode
LeetCode151-颠倒字符串中的单词-字符串-模拟
Thread synchronization, life cycle
How to upload large files quickly?
Nuxt project: Global get process Env information
What is the role of the full connection layer?
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
随机推荐
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
LeetCode149-直线上最多的点数-数学-哈希表
LeetCode167-两数之和II-双指针-二分-数组-查找
We reference My97DatePicker to realize the use of time plug-in
Programming philosophy - automatic loading, dependency injection and control inversion
Epoll's et, lt working mode -- example program
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
你還不知道責任鏈模式的使用場景嗎?
Epolloneshot event of epoll -- instance program
Sqlserver transaction and lock problem
Comment eolink facilite le télétravail
Unity_ Code mode add binding button click event
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
Vscode Chinese plug-in doesn't work. Problem solving
冰冰学习笔记:一步一步带你实现顺序表
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
Chapter 7 of JVM series -- bytecode execution engine
OPPO数据湖统一存储技术实践
Select receives both normal data and out of band data