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

//堆栈的实现,顺序表的实现
//编写一个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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言顺序表插入操作实例如下: 顺序表是一种线性表,其中的元素按照顺序排列并连续存储在内存中。顺序表插入操作是在指定位置插入一个新的元素,并将原位置上的元素按照顺序后移。下面是一个简单的实例: ```C #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 typedef struct { int data[MAX_SIZE]; int length; } SeqList; // 在指定位置插入新元素 void insert(SeqList *list, int pos, int elem) { // 判断插入位置的合法性 if (pos < 0 || pos > list->length || list->length == MAX_SIZE) { printf("插入位置不合法或顺序表已满\n"); return; } // 将指定位置后的元素依次后移 for (int i = list->length - 1; i >= pos; i--) { list->data[i + 1] = list->data[i]; } // 在指定位置插入新元素 list->data[pos] = elem; list->length++; printf("插入成功\n"); } int main() { SeqList list; list.length = 0; // 测试插入操作 insert(&list, 0, 1); // 在第一个位置插入元素1 insert(&list, 1, 2); // 在第二个位置插入元素2 insert(&list, 2, 3); // 在第三个位置插入元素3 // 输出顺序表中的元素 for (int i = 0; i < list.length; i++) { printf("%d ", list.data[i]); } printf("\n"); return 0; } ``` 运行结果: ``` 插入成功 插入成功 插入成功 1 2 3 ``` 以上实例中,首先定义了一个`SeqList`结构体来表示顺序表,其中`data`数组用于存储元素,`length`表示当前顺序表中元素的个数。`insert`函数实现了在指定位置插入新元素的操作,首先判断插入位置的合法性和顺序表是否已满,然后将指定位置后的元素依次后移,最后在指定位置插入新元素并更新顺序表的长度。在`main`函数中,测试了插入操作,并输出了顺序表中的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值