手工数据结构系列-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 ,然后肯定越界访问了啊,然后本地还没报错。。结果还对的。。一定要小心这个错误

posted @ 2017-03-03 23:58  狡啮之仰  阅读(434)  评论(0编辑  收藏  举报