H15S-SectionSolutions2

H15S-SectionSolutions2 - CS106B Handout #15S Winter 07-08...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS106B Handout #15S Winter 07-08 January 23, 2008 Section Solutions #2 Problem 1: Vectors a) Vector<eMailMsg> mailVector; b) void RemoveSpam(Vector<eMailMsg> & v) { for (int i = v.size() - 1; i >= 0; i--) { eMailMsg mail = v[i]; if (mail.subject.find("SPAM") == 0) { v.removeAt(i); } } } Note that you could work forwards instead of backwards (i.e., loop from 0 to size - 1 instead of the other way around). However, if you did youd have to make sure to decrement i whenever you removed a message since otherwise youd skip an index. c) We use another Vector, of course! struct eMailMsg { Vector<string> to; string from; string message; string subject; int date; int time; }; Access to the last element of eMailMsg email would be done by: string lastAddress = email.to[email.to.size() 1]; Problem 2: Queues /** * The client version of reverse queue. In order * to change the order of elements in the queue, * we use an external stack */ void ReverseQueue(Queue<int> & queue) { Stack<int> stack; while (!queue.isEmpty()) { stack.push(queue.dequeue()); } while (!stack.isEmpty()) { queue.enqueue(stack.pop()); } } Problem 3: Using the Scanner and Stack classes #include "stack.h" #include "scanner.h" bool ProcessOpenTag(Scanner& scanner, Stack<string>& tagStack) { string tag = scanner.nextToken(); tagStack.push(tag); return true; } bool ProcessCloseTag(Scanner& scanner, Stack<string>& tagStack)...
View Full Document

Page1 / 5

H15S-SectionSolutions2 - CS106B Handout #15S Winter 07-08...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online