棧:只允許一端進行插入或刪除的線性表。(操作受限的線性表)
特點:“先進後出”。
一、棧邏輯結構聲明:
typedef struct{
ElemType data[MAXSIZE];
int top;
}sqStack;
二、棧初始化、入棧、出棧、判斷棧是否爲空的方法:
初始化:
void initStack(sqStack &s){//棧初始化
s.top=-1;
}
入棧:
bool push(sqStack &s,ElemType e){//入棧操作
if(s.top==MAXSIZE-1) return false;
s.data[++s.top]=e;
cout<<"棧頂指針:"<<s.top<<endl;
return true;
}
出棧:
bool pop(sqStack &s){
if(s.top==-1) return false;
s.data[s.top];
s.top--;
return true;
}
判斷棧是否爲空:
bool stackEmpty(sqStack s){//判斷棧是否爲空
if(s.top==-1) return true;
return false;
}