hw2solns

# hw2solns - Suppose we use a stack named S 1 If the...

This preview shows pages 1–2. Sign up to view the full content.

University of California, Santa Barbara CS20, Spring 2008 Homework II solutions Question 1. (i) 24. (ii) The factorial function. Question 2. (i) ab + cd - / (ii) abc * - de * f * g + / (iii) ab/cde - + * (iv) ab ^ c * d - e ^ f g ^ h ^ + Question 3. public Object belowTop() { if(isEmpty()) return null. if(top.getlink() == null) return null; return (top.getlink().getInfo()); } Question 4. (a) 3 5 4 5 16 5 1 0 (b) 5 5 5 5 5 4 5 Question 5. No, ﬁrst note that we can always push and then pop, but we can only pop and then push if the stack is not empty. Note also that by ﬁrst pushing and then popping, you always push what you popped and the stack remains unchanged; but when you ﬁrst pop, you can push something diﬀerent and therefore the stack can change. Question 6. (a) False (b) True (c) True (d) True Question 7.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Suppose we use a stack named S: 1- If the container is not empty, pop the ﬁrst one if it is yellow, it eat otherwise push it in S go to step 1; else go to step 2; 2- If S is not empty 1 pop the topmost element. push in container. go to step 2. else Halt. Question 8. (i) We will get a compile error since the newNode is being used before being deﬁned. (ii) newNode will point to intself and we will lose the access to the stack. Question 9. On a seperate document. Question 10. public Object inspector(int index) { if(isEmpty()) return null; LLObjectNode node = top; for(int i=1; i < index; i++) { if(node.getLink()== null) return null; node = node.getLink(); } return node.getInfo(); } 2...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

hw2solns - Suppose we use a stack named S 1 If the...

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

View Full Document
Ask a homework question - tutors are online