CCI_chapter 3 Stacks and Queues

3.1Describe how you could use a single array to implement three stacks 

for stack 1, we will use [0, n/3)
for stack 2, we will use [n/3, 2n/3)
for stack 3, we will use [2n/3, n)

const int stackSize = 300;

int buffer = new int[stackSize * 3];

int stackPointer[3] = {0,0,0}; //栈顶指针,指向下一可以放元素的位置



bool isEmpty(int stackNum){

    assert(stackNum >=1 && stackNum <= 3);

    return stackPointer[stackNum-1] == 0 ;

} 

bool isFull(int stackNum){

    assert(stackNum >=1 && stackNum <= 3);

    return stackPointer[stackNum-1] == stackSize ;

}

bool push(int stackNum, int value){

    assert(stackNum >=1 && stackNum <= 3);

    if(isFull(stackNum)) return false;

    int index = (stackNum -1) * stackSize + stackPointer[stackNum-1] ;

    buffer[index] = value;

    stackPointer[stackNum-1]++;

    return true;

}

bool pop(int stackNum, int &value){

    assert(stackNum >=1 && stackNum <= 3);

    if(isEmpty(stackNum)) return false;

    int index = (stackNum -1) * stackSize + stackPointer[stackNum-1] ;

    value = buffer[index-1];

    stackPointer[stackNum-1]--;

    return true;

}

bool peek(int stackNum, int &value){

    assert(stackNum >=1 && stackNum <= 3);

    if(isEmpty(stackNum)) return false;

    int index = (stackNum -1) * stackSize + stackPointer[stackNum-1] ;

    value = buffer[index-1];

    return true;

}
View Code

3.2How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time

struct node{

    int value;

    int min;

    node *up;

    node(int data){

        value = data;

        min = data;

        up = NULL;

    }

}

struct stack()

{

    node *top;

    stack(){

        top = NULL ;

    }

}

void push(stack *, node *);

void pop(stack *);

node *peek(stack *);

int min(stack *);

stack *createS()

{

    stack * myS = new stack;

    return myS;

}

void push(stack * myS, node * myn){



    if(NULL == myS || NULL == myn) return ;

    if(NULL == myS->top){

        myS->top = myn;

        return ;

    }else{

        myn->up = myS->top;

        myn->min = myn->value < myS->top->value ? myn->value : myS->top->value;

        myS->top = myn;

    }

}

void pop(stack * myS){

    if(NULL == myS || myS->next == NULL ) return;

    node *tp = myS->top;

    myS->top = tp->up;

    delete tp;

}

node * peek(stack * myS){

    if(myS == NULL|| NULL == myS->top) return NULL;

    return myS->top;

}

int min(stack * myS){

    if(NULL == myS || myS->top== NULL) return INT_MAX ;

    return myS->top->min;

}
View Code

CCI的第二种解法感觉有问题,如果进栈的元素有重复,就有可能有bug

3.3 3.4没什么意思

3.5  Implement a MyQueue class which implements a queue using two stacks

stack<int> s1;

stack<int> s2;



int size(){

        return s1.size();

}

int front(){

    s1.top();

}

int push(int value){

    while(!s1.empty()){

        int temp = s1.top();

        s2.push(temp);

        s1.pop();

    }

    s1.push(value);

    while( !s2.empty() ){

        int temp = s2.top();

        s1.push(temp);

        s2,pop();

    }

}

void pop(){

    s1.pop();

}
View Code

3.6Write a program to sort a stack in ascending order You should not make any assumptions about how the stack is implemented The following are the only functions that should be used to write this program: push | pop | peek | isEmpty

/*

sorting can be performed with one more stack   The idea is to pull an item from the original 

stack and push it on the other stack   If pushing this item would violate the sort order of the 

new stack, we need to remove enough items from it so that it’s possible to push the new 

item   Since the items we removed are on the original stack, we’re back where we started   The 

algorithm is O(N^2) and appears below 

*/

stack<int> sort(stack<int> mys){

    

    if(mys.size() < 2) return mys;

    stack<int> tp;

    while( !mys.empty()){

        int value = mys.top();

        mys.pop();

        while( !tp.empty() && value < tp.top()){

            int temp = tp.top();

            mys.push(temp);

            tp.pop();

        }

        tp.push(value);

    }

    return tp;

    

}
View Code

 

你可能感兴趣的:(Queue)