Pregunta de la entrevista 03.01 Tres en uno

Pregunta de la entrevista 03.01 Tres en uno

Idea: registrar el tamaño de la pila

Abra la memoria para las matrices int * a; a = new int [1000];

class TripleInOne {
public:
    int *stack;
    int top[3];
    int stackSize;
    TripleInOne(int stackSize):stackSize(stackSize) {
        stack = new int[stackSize*3];//此处创建新内存卡了1小时
        top[0]=top[1]=top[2]=0;
    }
    
    void push(int stackNum, int value) {
        if(top[stackNum]<stackSize)
            stack[stackSize*stackNum + top[stackNum]++]=value;
    }
    
    int pop(int stackNum) {
        if(top[stackNum] <= 0) 
            return -1;
        else 
            return stack[stackSize*stackNum + (--top[stackNum])];
    }
    
    int peek(int stackNum) {
        if(top[stackNum] <=0 )
            return -1;  
        else 
            return stack[stackSize*stackNum + (top[stackNum]-1)];
    }
    
    bool isEmpty(int stackNum) {
        return top[stackNum]==0;
    }
};

 

248 artículos originales publicados · Me gusta 29 · Visitas 30,000+

Supongo que te gusta

Origin blog.csdn.net/weixin_38603360/article/details/105146985
Recomendado
Clasificación