C 언어 데이터 구조 창고 와 대기 열 순서 창고 (창고 의 순서 저장 구조)

//C  --    --         (        )


#include 
#include 
#include 
#include 
#include 
#include 


#define STACK_INIT_SIZE      100 //       
#define STACK_INCREMENT_SIZE 10 //      


typedef  int  ElemType;


typedef struct{
ElemType  *base;    //    (       )
ElemType  *top;     //    
int       stacksize;//         
}OrderStack;


int  InitStack(OrderStack &s);//    (      )
int  Pop(OrderStack &s);//  (      )
void Traverse(OrderStack &s);//   
int  GetTop(OrderStack s);//         
int  Push(OrderStack &s,ElemType &e);//  


//    (      )
int InitStack(OrderStack &s){
s.base=(ElemType*)malloc(10);
if (!s.base)
exit(0);//        
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return 1;
}


//         
int GetTop(OrderStack s){
ElemType e;
if (s.top==s.base){
return FALSE;
}else{
e=*(s.top-1);
}
return e;
}


//  
int Push(OrderStack &s,ElemType &e){
if ((s.top-s.base)>=s.stacksize){
s.base=(ElemType *)realloc(s.base,(s.stacksize+STACK_INCREMENT_SIZE)*sizeof(ElemType));
if (!s.base){
return false;
}
s.top=s.base+s.stacksize;
s.stacksize=s.stacksize+STACK_INCREMENT_SIZE;
printf("%-6       !!!      --> %d
",s.stacksize); } *s.top=e; s.top++; return 1; } // ( )    int Pop(OrderStack &s){ ElemType e; if (s.top==s.base){ return 0; }else{ s.top--; e=*s.top; return e; } } // void Traverse(OrderStack &s){ if (s.top==s.base){ } else{ ElemType *traverse=s.top; while (traverse>s.base){ traverse--; printf("%d
",*traverse); } } }   int main(){ OrderStack s; InitStack(s); for (int i=1;i<=10;i++){ //*s.top=i; //++s.top; Push(s,i);// printf(" %d
",GetTop(s)); } printf("--------------- ---------------
"); int k=100; int m=200; Push(s,k);// printf(" %d
",GetTop(s)); Push(s,m);// printf(" %d
",GetTop(s)); printf("--------------- ---------------
"); printf(" %d
",Pop(s)); printf(" %d
",Pop(s)); printf(" %d
",Pop(s)); printf(" %d
",Pop(s)); printf("--------------- ---------------
"); Traverse(s); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기