Data Str & Algorithm HW Solutions 26

Data Str & Algorithm HW Solutions 26 - } bool...

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

View Full Document Right Arrow Icon
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 f 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 f cient when the array would be less than two thirds full. 4.11 We need only modify push and pop, as follows. // 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);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } bool pop(Elem&amp; it) { // Pop ELEM from top of stack if (top == 0) return false; int length = listarray[top--]; for (int i=1; i&lt;=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 &lt;class Elem&gt; 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

This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online