//InitStack(S);
//StackEmpty(S);
//Push(S,x);
//Pop(S,x);
//GetTop(S,x);
//ClearStack(S);
//栈顶指针:S.top=-1;栈顶元素:S.data[S.top]
//进栈操作:指针+1,再送值到栈顶元素
//出栈操作:先取栈顶元素值,再将栈顶指针减1
//栈空条件:S.top==-1,栈满条件:S.top=MaxSize-1,栈长:S.top+1
#include<iostream>
using namespace std;
#include"stdbool.h"
#define MaxSize 50 //栈的容量
typedef struct
{
int data[MaxSize];
int top;
}SqStack;//栈的名字
void InitStack(SqStack *s){
cout << "init Now s = " << s << endl;
(*s).top=-1;
}
bool StackEmpty(SqStack s){
if(s.top==-1){
return true;
}else
{
return false;
}
}
bool Push(SqStack *s,int x){
cout << "push Now s = " << s << endl;
if((*s).top==MaxSize-1){
return false;
}else
{
(*s).data[++(*s).top]=x;
return true;
}
}
int Pop(SqStack *s,int &x){
cout << "pop Now s = " << s << endl;
if((*s).top==-1){
return 0;
}else
{
x=(*s).data[(*s).top--];
return x;
}
}
int GetTop(SqStack s,int x){
if(s.top==-1){
return -999;
}else
{
x=s.data[s.top];
return x;
}
}
int Getlength(SqStack s){
return s.top+1;
}
int main(){
SqStack s;
cout << "main Now s = " << &s << endl; //查看一下s的地址
int x=4;
InitStack(&s); //传入s的地址
Push(&s,x);
int o=8;
Push(&s,o);
int m;
cout << "Now pop = " << Pop(&s,m) << endl;
int n;
int p=GetTop(s,n);
cout << "Now p = " << p << endl;
cout << "Now empty = " << StackEmpty(s) << endl;
}
栈的相关操作 C/C++
最新推荐文章于 2023-11-01 23:22:51 发布