手工数据结构系列-C语言模拟队列和栈 hdu1702

#include <stdio.h>
#include <stdlib.h>
//================= DATA STRUCTURE =========================
//================= Stack ==================================
#define __init_size 1000
typedef struct{
    int size,__size,head,tail,*seq;
}Stack;

typedef Stack* S_P;
void __init(S_P s){
    s->head=s->tail=s->size=0;
    s->__size=__init_size;s->seq=(int*)malloc(__init_size*sizeof(Stack));//少了__init_size
}
int __empty(S_P s){
    if(s->head==s->tail) return 1;
    return 0;
}
void __push(S_P s,int ele){
    if(s->tail+1>=s->__size){
        s->seq=(int*)realloc(s->seq,sizeof(Stack)*(s->__size+=__init_size));
    }
    s->seq[s->tail++]=ele;s->size++;
}
void __pop(S_P s){
    if(!__empty(s)) s->tail--,s->size--;
}
int __top(S_P s){
    if(!__empty(s)) return s->seq[s->tail-1];
}
void __test(){
    S_P s=(S_P)malloc(sizeof(S_P));__init(s);
    int i;for(i=0;i<10;++i)  __push(s,i);
    while(!__empty(s)) {printf("%d ",__top(s));__pop(s);}
    printf("\n");
}
//======================= QUEUE ============================
#define init_size 1000
typedef struct {
    int head,tail,size,__size,*seq;
}Queue;
typedef Queue* Q_P;
void init(Q_P q){
    q->head=q->tail=0;q->__size=init_size;q->size=0;
    q->seq=(int*)malloc(init_size*sizeof(Queue));
}
void push(Q_P q,int ele){
    if(q->tail+1>=q->__size){
        q->seq=(int*)realloc(q->seq,sizeof(int)*(q->__size+=init_size));
    }
    q->seq[q->tail++]=ele;
    q->size++;
    //debug
    // printf("PUSH %d SIZE:%d\n",q->seq[q->tail-1],q->size);
}
int empty(Q_P q){
    if(q->head==q->tail) return 1;
    return 0;
}
void pop(Q_P q){
    if(!empty(q)) q->head++,q->size--;
    if(empty(q)) q->head=q->tail=0;
    // printf("POP SIZE:%d\n",q->size);
}
int front(Q_P q){
    if(!empty(q)) return q->seq[q->head];
}
void print(Q_P q){
    printf("%d",front(q));pop(q);
    while(q->size!=0) {printf(" %d",front(q));pop(q);}
    printf("\n");
}
void test(){
    int i;
    Q_P q=(Q_P)malloc(sizeof(Queue));
    init(q);
    for(i=0;i<10;++i) {push(q,i);}
    print(q);    
}
//=================== SOLVE FUNCTION ========================
void solve(){
    int i,x,n,T;scanf("%d",&T);
    char type[8],op[4];
    S_P s=(S_P)malloc(sizeof(Stack));__init(s);
    Q_P q=(Q_P)malloc(sizeof(Queue));init(q);
    while(T--){
        scanf("%d%s",&n,type);
        if(type[2]=='F') {while(!empty(q)) pop(q);}else {while(!__empty(s)) __pop(s);}
        for(i=0;i<n;++i){
            scanf("%s",op);
            if(op[0]=='I') scanf("%d",&x);
            if(type[2]=='F'){
                //FIFO QUEUE
                if(op[0]=='I'){
                    push(q,x);
                }
                else{
                    if(empty(q)) printf("None\n");
                    else {printf("%d\n",front(q));pop(q);}
                }
            }
            else{
                if(op[0]=='I'){
                    __push(s,x);
                }
                else{
                    if(__empty(s)) printf("None\n");
                    else {printf("%d\n",__top(s));__pop(s);}
                }
            }
        }
    }
}
int main(){
    solve();
    return 0;
}

没了__init_size ,然后肯定越界访问了啊,然后本地还没报错。。结果还对的。。一定要小心这个错误

转载于:https://www.cnblogs.com/linkzijun/p/6498915.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值