[Cracking the Coding Interview] Chapter 3

来源:互联网 发布:ubuntu 16.10安装lamp 编辑:程序博客网 时间:2024/06/04 00:40
3.1 Describe how you could use a single array to implement three stacks.

/* solution for question 3.1 */template<class T>class TriStack{private:T *data;int size;int begin[3], end[3];public:/* constructor */TriStack(int maxSize = 100){maxSize = maxSize < 10 ? 100 : maxSize;data = new T[maxSize];begin[0] = end[0] = 0;begin[1] = end[1] = maxSize / 3;begin[2] = end[2] = maxSize / 3 * 2;size = maxSize;}/* destructor */~TriStack(){if(data != NULL){delete[] data;}}/* push */void push(T &x, int stackId){if(stackId < 0 || stackId > 2){cerr << "Invalid stack id!" << endl;return;}// pushif( (stackId < 2 && end[stackId] < begin[stackId+1]) || (stackId == 2 && end[stackId] < size) ){data[end[stackId]++] = x;}else{cerr << "Out of stack!" << endl;}}/* pop */void pop(int stackId){if(stackId < 0 || stackId > 2){cerr << "Invalid stack id!" << endl;return;}// popif(end[stackId] > begin[stackId]){end[stackId]--;}else{cerr << "Empty stack!" << endl;}}/* top */T top(int stackId){if(stackId < 0 || stackId > 2){cerr << "Invalid stack id!" << endl;return T(NULL);}// popif(end[stackId] > begin[stackId]){return data[end[stackId]-1];}else{cerr << "Empty stack!" << endl;return T(NULL);}}/* output */void output(){for(int i = 0; i < size; ++i){cout << data[i] << " ";}cout << endl;}};


3.2 How 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.

/* solution for question 3.2 */template <class T>class MinStack{private:stack<T> data;stack<T> minData;public:/* constructor */MinStack(){}/* push */void push(T &x){if(data.empty()){minData.push(x);}else{minData.push( minData.top() < x ? minData.top() : x );}data.push(x);}/* pop */void pop(){data.pop();minData.pop();}/* top */T top(){if(data.empty()){cerr << "Empty stack!" << endl;return T(NULL);}else{return data.top();}}/* min */T min(){if(data.empty()){cerr << "Empty stack!" << endl;return T(NULL);}else{return minData.top();}}};


3.3 Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks, and should create a new stack once the previous one exceeds capacity. SetOfStacks.push() and SetOfStacks.pop() should behave identically to a single stack (that is, pop() should return the same values as it woould if there were just a single stack).
FOLLOW UP
Implement a function popAt(int index) which performs a pop operation on a specific sub-stack.

/* solution for question 3.3 */template <class T>class SetOfStacks{private:vector<vector<T> > data;// stores dataint singleSize;// size of each stackint size;// current size of SetOfStacksint num;// number of stackspublic:/* constructor */SetOfStacks(int _singleSize = 100) : singleSize(_singleSize), size(0), num(0){}/* push */void push(T &x){if(size + 1 > num * singleSize){vector<T> row;row.push_back(x);data.push_back(row);size++;num++;}else{data[num-1].push_back(x);size++;}}/* pop */void pop(){if(0 == size){cerr << "Empty stack." << endl;}else{data[num-1].erase(data[num-1].end()-1);size--;if(size == (num - 1) * singleSize){data.erase(data.end()-1);num--;}}}/* pop at */void popAt(int index){if(index < 0 || index >= size){cerr << "Out of stack." << endl;}else{if(index == size-1){pop();}else{// copy the remained datavector<T> tmp;for(int i = 0; i < data.size(); ++i){for(int j = 0; j < data[i].size(); ++j){if(i*singleSize + j != index){tmp.push_back(data[i][j]);}}}// cleardata.clear();size = 0;// remind to reset size and numnum = 0;// resetfor(int i = 0; i < tmp.size(); ++i){push(tmp[i]);}}}}/* top */T top(){if(0 == size){cerr << "Empty stack." << endl;return T(NULL);}else{return data[num-1][size-(num-1)*singleSize-1];}}/* output */void output(){for(int i = 0; i < data.size(); ++i){for(int j = 0; j < data[i].size(); ++j){cout << data[i][j] << " ";}cout << endl;}}};


3.4 In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one). You have the following constraints:
(A) Only one disk can be moved at a time.
(B) A disk is slid off the top of one rod onto the next rod.
(C) A disk can only be placed on top of a larger disk.
Write a program to move the disks from the first rod to the last using Stacks.

/* solution for question 3.4 */void hanoi(stack<int> &from, int n, stack<int> &to, stack<int> &by, string fromName, string toName, string byName){if(n <= 0)return;hanoi(from, n-1, by, to, fromName, byName, toName);if(!from.empty()){cout << "move 1 plate from " << fromName << "(" << from.top() << ")" << " to " << toName << "(" << (to.empty() ? -1 : to.top()) << ")" << endl;to.push(from.top());from.pop();hanoi(by, n-1, to, from, byName, toName, fromName);}}


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

/* solution for question 3.5 */template <class T>class MyQueue{private:stack<T> first;stack<T> second;public:/* constructor */MyQueue(){//first.clear();//second.clear();}/* push */void push(T &x){first.push(x);}/* pop */void pop(){if(first.empty() && second.empty()){cerr << "Empty queue." << endl;return;}// move data from first to secondif(second.empty()){while(!first.empty()){T x = first.top();second.push(x);first.pop();}}second.pop();}/* front */T front(){if(first.empty() && second.empty()){cerr << "Empty queue." << endl;return T(NULL);}// move data from first to secondif(second.empty()){while(!first.empty()){T x = first.top();second.push(x);first.pop();}}return second.top();}/* back */T back(){if(first.empty() && second.empty()){cerr << "Empty queue." << endl;return T(NULL);}// move data from first to secondif(first.empty()){while(!second.empty()){T x = second.top();first.push(x);second.pop();}}return first.top();}};


3.6 Write 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.
/* solution for question 3.6 */template <class T>void stack_sort(stack<T> &myStack){stack<T> tmpStack;while(!myStack.empty()){if(tmpStack.empty()){tmpStack.push(myStack.top());myStack.pop();}else{// swap the top of myStack and the top of tmpStackif(myStack.top() > tmpStack.top()){T tmp = myStack.top();myStack.pop();myStack.push(tmpStack.top());tmpStack.pop();myStack.push(tmp);}else{tmpStack.push(myStack.top());myStack.pop();}}}// reset myStackwhile(!tmpStack.empty()){myStack.push(tmpStack.top());tmpStack.pop();}}

说明:版权所有,转载请注明出处。Coder007的博客