顺序表堆栈的操作实现源码

//堆栈的实现,顺序表的实现
//编写一个C源程序,其中包含顺序表示的空栈的创建、判断栈是否为空、进栈、出栈、取栈顶元素等操作
int main(){
SLNode *phead;
phead=InitiateHead(phead);
cout<<"首地址2.1:"<<phead<<endl;//此为头指针的地址 
menu();
operation(phead);

return 0;

}
//操作实现代码:

//初始化 
int initiate(Stack **s){
       *s=(Stack *)malloc(sizeof(Stack));
      (*s)->top= 0;
    // cout<< s->top;
     return 1;
}
//判断栈是否为空 
int Empty_stack(Stack *s){
     if(s->top<=0){
       cout <<"栈为空!"<<endl;
       return 0;
     }else if(s->top>=MaxSize-1){
         cout<<"栈满了,无法进行压栈操作!"<<endl;
         return 3;
       
     }else{
      cout<<"栈不为空!"<<endl; 
      return 1;
     } 
} 
//创建栈
int Create_Stack(Stack *s){
       int i=0; 
  cout<<"请创建栈:"<<endl;
  cout<<"______________________________top:"<<endl;
  cout<<"期待有人能教我解决这个输入的问题,希望简单明了!"<<endl; 
  for(int i = 0;i < 10;i++){
      if(s->top>=MaxSize){
       break;
     }
    s->stack[s->top]=i+1;
    s->top++;    
      }
       cout<<"________________________________top:"<<endl; 
       /*cout<<"________________________________top:"<<endl;
  for(int i = 0;;i++){
    cin>>i;
      if(s->top<MaxSize&&getchar()=='\n'){
       break;
   }
    s->stack[s->top]=i+1;
    s->top++;    
      }
       cout<<"________________________________top:"<<s->top<<endl; *///这里有个问题希望有人能够指点,不知如何操作。。。求解,谢谢 
      return 1;
} 
//输出栈内元素
 int out_Stack(Stack *s){
  for(int i = 0;i<s->top;i++){
       cout<<s->stack[i]<<endl;
} 
 } 
//压栈 
int Push_Stack(Stack *s,DataType x){
     if(Empty_stack(s)==3){
      return 1;
     }else{
        s->stack[s->top] = x;
        s->top++;
        cout<<x<<endl; 
   return 1;
     }
}
//出栈
int Stack_Pop(Stack *s){
Empty_stack(s);
cout<<"________________________________top:"<<s->top<<endl;
    while(s->top!=NULL){
  s->top--;
  cout<<s->stack[s->top]<<endl;
    }
    cout<<"________________________________top:"<<s->top<<endl;
    return 1;   
} 
//取栈顶元素
int  Stack_Top(Stack *s,DataType *d){
if(s->top<= 0){
     Empty_stack(s);
}else{
*d=s->stack[s->top-1];
}
return 1;
} 
//主函数

int main(){
  Stack *p;
  int x;
  initiate(&p);
  Empty_stack(p);
  Create_Stack(p);
  cout<<"创建好后的栈中的元素:"<<endl; 
  out_Stack(p);
  cout<<"压栈:"<<endl;
  Push_Stack(p,100);
  cout<<"出栈:"<<endl;
       Stack_Pop(p);
       cout<<"取栈顶元素:"<<endl;
       Stack_Top(p,&x);
       cout<<x<<endl;
  return 0;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值