- Write the pseudo-code of an algorithm which evaluates a fully parenthesized mathematical expression using stack data structure. (calculator).
- For the queue class given in Appendix 1 (cf. end of this assignment), implement the copy constructor.
Note that the class uses a dynamic array. Also please note that you should not use the copy function (copy only the valid entries of one array to the new array).
- template <class Item>
- queue<Item>::queue (const queue <Item>& source)
- queue class given in Appendix 1 (cf. end of this assignment), implement the following function, which increases the size of the dynamic array used to store items. Please note that you should not use the copy function (copy only the valid entries of one array to the new array).
- deque class given in Appendix 2 (cf. end of this assignment), implement the following constructor. The constructor allocates an array of block pointers and initializes all of its entries with NULL. The initial size of the array is init_bp_array_size.
- template < class Item >
- deque<Item>::deque( int init_bp_array_size, int init_block_size )
- {
- bp_array_size = init_bp_array_size;
- block_size = init_block_size;
- deque class given in Appendix 2 (cf. end of this assignment), write the full implementation of the following function.
- template <class Item>
- void deque <Item>::pop_front()
- // Precondition: There is at least one entry in the deque
- // Postcondition: Removes an item from the front of the deque
- {
- assert(!isEmpty());
- What are the outputs of the following programs?
1. #include < iostream >2. using namespace std;3. |
4. class Base { |
5. private:6. int i, j; |
7. public:8. Base (int _i = 0, int _j = 0): i(_i), j(_j) {} |
9. }; 10. |
11. class Derived: public Base { |
12. public:13. void show() { cout << i = << i << j = << j; } |
14. }; 15. |
16. int main(void) { |
17. Derived d; |
18. d.show(); |
19. return 0; |
20. } |
1. #include < iostream >2. using namespace std; 3. |
4. class P { |
5. public:6. void print() { |
7. cout << Inside P; |
8. } |
9. }; 10. |
11. class Q: public P { |
12. public:13. void print() { |
14. cout << Inside Q; |
15. } |
16. }; 17. |
18. class R: public Q {}; 19. |
20. int main(void) { |
21. R r; |
22. r.print(); |
23. return 0; |
24. } |
1. #include < iostream >2. using namespace std; 3. |
4. class Base {}; 5. |
6. class Derived: public Base {}; 7. |
8. int main() { |
9. Base * bp = new Derived; |
10. Derived * dp = new Base; |
11. } |
Appendix 1: queue class declaration:
- template < class Item >
Appendix 2: deque class declaration:
- template < class Item > 2. class deque {
- public:
- // TYPEDEF
- static const size_t BLOCK_SIZE = 5; // Number of data items per block
- // Number of entries in the block of array pointers. The minimum acceptable value is 2 8. static const size_t BLOCKPOINTER_ARRAY_SIZE = 5; 9.
- typedef std::size_t size_type; 11. typedef Item value_type;
- deque(int init_bp_array_size = BLOCKPOINTER_ARRAY_SIZE,
- int initi_block_size = BLOCK_SIZE);
- deque(const deque & source); 17. ~deque();
- // CONST MEMBER FUNCTIONS
- bool isEmpty();
- value_type front(); value_type back();
- // MODIFICATION MEMBER FUNCTIONS
- void operator = (const deque & source);
- void clear();
- void reserve();
- void push_front(const value_type & data);
- void push_back(const value_type & data);
- void pop_back(); void pop_front();
- private:
- // A pointer to the dynamic array of block pointers value_type** block_pointers;
- // A pointer to the final entry in the array of block pointers 38. value_type** block_pointers_end;
- // A pointer to the first block pointer thats now being used 41. value_type** first_bp;
- // A pointer to the last block pointer thats now being used 44. value_type** last_bp;
- value_type* front_ptr; // A pointer to the front element of the whole deque 47. value_type* back_ptr; // A pointer to the back element of the whole deque
- size_type bp_array_size; // Number of entries in the array of block pointers 50. size_type block_size; // Number of entries in each block of items
- };
Reviews
There are no reviews yet.