#数据结构#Chapter3 栈和队列-1

3.1栈

3.1.1栈的基本概念

栈的基本操作:
InitStack(&S);
StackEmpty(S)
Push(&S,x);
Pop(&S,&x);
GetTop(S,&x);
DestoryStack(&S);

3.1.2 栈的顺序存储结构

定义:
#define Maxsize 50;
typedef struct{
    
    
Elemtype data[MaxSize];
int top;
}sqStack;
栈顶指针:S.top
栈空条件:S.top==-1;
栈满条件:S.top==MaxSize-1;
栈长:S.top+1;
void InitStack(SqStack &S){
    
    
S.top=-1;
}
bool StackEmpty(SqStack &S,ElemType x){
    
    
if(S.top==-1)
	return true;
else
	return false;		
}
bool Push(SqStack &S,ElemType x){
    
    
if(S.top==MaxSize-1)//先判断是否为栈满
	return false;
S.data[++S.top]=x;
return true;
}
bool Pop(Stack &S,ElemType x){
    
    
if(S.top==-1)//先判断栈空
	return false;
x=S.data[S.top--];
return true;	
}
bool GetTop(SqStack S,ElemType &x){
    
    
if(S.top==-1)
	return false;
x=S.data[S.top];
return true;
}

3.1.3 栈的链式存储结构

typedef struct Linknode{
    
    
ElemType data;
struct Linknode *next;
}*LiStack;

Guess you like

Origin blog.csdn.net/weixin_45840087/article/details/105692504