棧的順序存儲

棧的定義ide

    限定僅在表尾進行插入和刪除操做的線性表spa

棧的順序存儲結構指針

 

  
  
  
  
  1. #define MAXSIZE 20 
  2.  
  3. typedef int SElemType; 
  4. typedef struct
  5.     SElemType data[MAXSIZE]; 
  6.     int top;//棧頂指針,棧滿時top爲MAXSIZE-1,棧空時爲-1  
  7. }SqStack;  

入棧blog

 

  
  
  
  
  1. bool push(SqStack *S,SElemType e){ 
  2.     if(S->top == MAXSIZE-1){//判斷是否棧滿  
  3.         return false
  4.     } 
  5.     S->data[++(S->top)] == e;//先上移再賦值  
  6.     return true

出棧get

 

  
  
  
  
  1. bool pop(SqStack *S,SElemType *e){ 
  2.     if(S->top == -1){ 
  3.         return false
  4.     } 
  5.      
  6.     *e = S->data[(S->top)--];//先賦值再下移 
  7.     return true; 
相關文章
相關標籤/搜索