10-1-2:
The two stacks start from both ends of the array and expand to the middle;
10-1-5:
This deque code has previously been implemented (C ++ STL) and will not be written here. The head and tail point to the center of the array at the beginning;
10-1-6:
Stack is first-in-first-out, queue is first-in-first-out,
To implement a queue by two stacks, add the element to stack 1 at the beginning according to the negative positive principle. When the element is deleted, the element is first deleted to stack 2, then, stack 2 is used to exit the stack. Then, wait until all the elements in stack 2 are deleted, and then all the elements in stack 1 are cleared to stack 2.
The Stack class is used to construct the queue class. For details, refer to the stack and queue code,
The change to the stack code is to add the friend Declaration;
Template <typename T, typename alloc = alloc> class queue {PRIVATE: Stack <t, alloc> _ stack1; stack <t, alloc> _ stack2; int capacity; public: queue (INT queuecapacity = 10); // construct a queue. The default capacity is 10. bool empty () const {return _ stack1.empty () & _ stack2.empty ();} T & Front (); // return the element T & Rear () in the queue header; // return the element void push (const T & X) at the end of the queue; // Insert the element, you can only insert void POP (); // Delete the element at the end of the queue. You can only delete the element at the queue header };//--------------------------------------------------- Invalid template <typename T, typename alloc> queue <t, alloc>: Queue (INT queuecapacity): Capacity (queuecapacity) {If (capacity <1) throw "queue capacity must> 0"; _ stack1.capacity = queuecapacity; _ stack1.allocate _ and_fill (10, T ();} // define template <typename t, typename alloc> T & queue <t, alloc >:: Front () {If (empty () throw "queue is empty"; if (_ stack2.empty () {// stack 2 is empty at this time, the elements we need to obtain must be the top element of stack 2, so we need to first clear all the elements in stack 1 to stack 2, t tmp; while (! _ Stack1.empty () {TMP = _ stack1.top (); _ stack2.push (TMP); _ stack1.pop () ;}} return _ stack2.top ();} // define template <typename T, typename alloc> T & queue <t, alloc >:: rear () {If (empty () throw "queue is empty "; if (_ stack1.empty () {return _ stack2. _ stack [0];} else {return _ stack1.top ();}}//-------------------------------------- ------------------------------------------------- Template <typename T, typename alloc> inline void queue <t, alloc >:: push (const T & X) {_ stack1.push (x );} // queue ---- template <typename T, typename alloc> void queue <t, alloc>: Pop () {If (empty () throw "queue is empty "; if (_ stack2.empty () {// at this time, stack 2 is empty. The elements we need to obtain must be the top of stack 2. So you need to first clear all the elements in // stack 1 to stack 2, while (! _ Stack1.empty () {_ stack2.push (_ stack1.top (); _ stack1.pop () ;}}_ stack2.pop ();}
10-1-7:
This is exactly what we cannot bear.
The implementation here is not very skillful. I have read other people's solutions and it is a thought. When the stack is to be launched, all the first n-1 elements in one queue will be dumped into another queue, then the last element of the queue goes out of the stack;
Or from the beginning, one of the two queues is an empty queue and the value is entered into this empty queue, then, the values of another non-empty queue are retrieved and added to the queue in sequence;
10-2-8:
Here refer
10-4-x:
// Configure template <typename T> struct _ tree_node {t data; _ tree_node <t> * left_child; _ tree_node <t> * right_child; _ tree_node (t a = T (), _ tree_node <t> * B = NULL, _ tree_node <t> * c = NULL): Data (), left_child (B), right_child (c) {}}; template <typename T, typename alloc = alloc> class tree {PRIVATE: typedef _ tree_node <t> tree_node; typedef simple_alloc <tree_node, alloc> node_allocator; // The main consideration here is the tree traversal problem public: void inorder (tree_node * current_node ); // In-order traversal void iterative_inorder (tree_node * current_node); // In-iteration traversal void preorder (tree_node * current_node); // first-order traversal void postorder (tree_node * current_node ); // post-order traversal void level_order (tree_node * current_node); // hierarchical traversal PRIVATE: tree_node * root; private: void visit (tree_node * current_node ); // Indicates access to a node}; // your template <typename T, typename alloc> void tree <t, alloc >:: inorder (tree_node * current_node) {If (current_node) {inorder (current_node-> left_child); visit (current_node); inorder (current_node-> right_child) ;}// your template <typename T, typename alloc> void tree <t, alloc >:: preorder (tree_node * current_node) {If (current_node) {visit (current_node); preorder (current_node-> left_child); preorder (current_node-> right_child );}} // define template <typename T, typename alloc> void tree <t, alloc>: postorder (tree_node * current_node) {If (current_node) {postorder (current_node-> left_child ); postorder (current_node-> right_child); visit (current_node) ;}// else // fully simulate the execution process of recursive algorithms, converted from an explicit program call stack to our own stack template <typename T, typename alloc> void tree <t, alloc >:: iterative_inorder (tree_node * current_node) {stack <tree_node *> S; tree_node * _ current_node = current_node; while (1) {While (_ current_node) {S. push (_ current_node); _ current_node = _ current_node-> left_child;} If (S. empty () return; _ current_node = S. top (); S. pop (); visit (_ current_node); _ current_node = _ current_node-> right_child;} // ------------------------------------------------------------- // access the node, here we print the node value to couttemplate <typename T, typename alloc> void tree <t, alloc >:: visit (tree_node * current_node) {STD :: cout <current_node-> data <'';} // uses the queue idea template <typename T, typename alloc> void tree <t, alloc> :: level_order (tree_node * current_node) {queue <tree_node *> q; tree_node * _ current_node = current_node; while (_ current_node) {visit (_ current_node ); if (_ current_node-> left_child) Q. push (_ current_node-> left_child); If (_ current_node-> right_child) Q. push (_ current_node-> right_child); If (Q. empty () return; _ current_node = Q. front (); q. pop () ;}// response ();}}//-------------------------------------------------------------------------------------
10-4-5:
The requirement here is to use a fixed amount of extra space. One method is to increase the parent domain, but this method will cause a huge space cost, which is far less cost-effective than using stacks;
Using this method, we still simulate the execution process of sequential traversal, but here we do not use the stack, but adopt the parent index;
The second method is to use the clue tree: Because the left_child and right_child fields of the leaf node are not used, we can make full use of these spaces:
If the right_child of node P is empty, set right_child to point to the successor node of P in the middle order;
If left_child of node P is empty, set left_child to point to the frontend node of P in the middle order;
At the same time, two domains are added to the node: left_thread and right_thread of the bool type. When the value is true, the corresponding child node is a clue;
10-4-6:
Clue tree