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

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

View Full Document Right Arrow Icon
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, first 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 first pushing and then popping, you always push what you popped and the stack remains unchanged; but when you first pop, you can push something different and therefore the stack can change. Question 6. (a) False (b) True (c) True (d) True Question 7.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
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 dened. (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

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 Right Arrow Icon
Ask a homework question - tutors are online