{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Str &amp; Algorithm HW Solutions 26

# Data Str &amp; Algorithm HW Solutions 26 - bool...

This preview shows page 1. Sign up to view the full content.

26 Chap. 4 Lists, Stacks, and Queues 4.10 I assume an int requires 4 bytes, a double requires 8 bytes, and a pointer requires 4 bytes. (a) Since E = 4 and P = 4 , the break-even point occurs when n = 4 D/ 8 = 1 2 D. Thus, the linked list is more space ef fi cient when the array would be less than half full. (b) Since E = 8 and P = 4 , the break-even point occurs when n = 8 D/ 12 = 2 3 D. Thus, the linked list is more space ef fi cient when the array would be less than two thirds full. 4.11 We need only modify push and pop, as follows. bool push(const Elem& item) { // Push ELEM onto stack if (top + length(item) < size) return false; // Full for (int i=0; i<length(item) i++) listArray[top++] = item[i]; listArray[top++] = length(item);
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } bool pop(Elem& it) { // Pop ELEM from top of stack if (top == 0) return false; int length = listarray[top--]; for (int i=1; i<=length; i++) it[length - i] = listarray[top--]; return it; } 4.12 Most member functions get a new parameter to indicate which stack is ac-cessed. // Array-based stack implementation template <class Elem> class AStack2 { private: int size; // Maximum size of stack int top1, top2; // Index for top element (two) Elem *listArray; // Array holding stack elements public: AStack2(int sz =DefaultListSize) // Constructor { size = sz; top = 0; listArray = new Elem[sz]; }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online