{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Stacks and Queues Answers

Stacks and Queues Answers - x2 X1(with xN at the front...

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

View Full Document Right Arrow Icon
Answers to Self-Study Questions Test Yourself #1 public Stack() { items = new E[INITSIZE]; numItems = 0; } Test Yourself #2 public E pop() throws EmptyStackException { if (numItems == 0) throw new EmptyStackException(); else { numItems--; return items[numItems]; } } Test Yourself #3 Test Yourself #4 public void push(E ob) { items = new Listnode(ob, items); numItems++; } Test Yourself #5 Test Yourself #6 public static void reverseQ(Queue<E> q) { // precondition: q contains x1 x2 . .. xN (with x1 at the front) // postcondition: q contains xN .
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: .. x2 X1 (with xN at the front) Stack<E> s = new Stack<E>(); while (!q.empty()) s.push(q.dequeue()); while (!s.empty()) q.enqueue(s.pop()); } OPERATION WORST-CASE TIME AVERAGE-CASE TIME constructor O(1) O(1) empty O(1) O(1) size O(1) O(1) push O(N) O(1) pop O(1) O(1) peek O(1) O(1) OPERATION WORST-CASE TIME constructor O(1) empty O(1) size O(1) push O(1) pop O(1) peek O(1) Page 1 of 1 Stacks and Queues Answers 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Stacks-and-Queues/answers.html...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online