Docsity

Linked List Methods - Representing Structure and Behavior | CS 1316, Exams of Computer Science

Toggle sidebar
bg1
SUMMER 2007 CS1316 PRACTICE FINAL
LINKEDLIST METHODS
A. TRACING THE CODE
Consider the following code:
1. What are the requirements to enter the if-statement? What occurs in the if-statement? Why is
this if-statement even necessary?
2. What is occurring in the line directly below the comment? Why is this necessary?
3. What are the conditions to enter the while-loop and under what conditions will the while-loop
exit?
4. What are the requirements to enter the if-statement? What occurs in the if-statement?
5. What is occurring in the line directly below the comment? Why is this necessary?
public void remove(PositionedSceneElement node){
//Question 1
if (node==this){
System.out.println("I can't remove the first node from the list.");
return;
}
//Question 2
PositionedSceneElement current = this;
//Question 3
while (current.getNext() != null){
//Question 4
if (current.getNext() == node){
current.setNext(node.getNext());
node.setNext(null);
return;
}
//Question 5
current = current.getNext();
}
}
pf2

Partial preview of the text

Download Linked List Methods - Representing Structure and Behavior | CS 1316 and more Exams Computer Science in PDF only on Docsity!

SUMMER 2007 CS1316 PRACTICE FINAL

LINKEDLIST METHODS

A. TRACING THE CODE

Consider the following code:

1. What are the requirements to enter the if-statement? What occurs in the if-statement? Why is

this if-statement even necessary?

2. What is occurring in the line directly below the comment? Why is this necessary?

3. What are the conditions to enter the while-loop and under what conditions will the while-loop

exit?

4. What are the requirements to enter the if-statement? What occurs in the if-statement?

5. What is occurring in the line directly below the comment? Why is this necessary?

public void remove(PositionedSceneElement node){ //Question 1 if (node==this){ System.out.println("I can't remove the first node from the list."); return; } //Question 2 PositionedSceneElement current = this; //Question 3 while (current.getNext() != null){ //Question 4 if (current.getNext() == node){ current.setNext(node.getNext()); node.setNext(null); return; } //Question 5 current = current.getNext(); } }

SUMMER 2007 CS1316 PRACTICE FINAL

B. WRITING A METHOD

Consider the following code:

Rewrite the length() method so that when called it will print out the correct number of elements

in the list. Remember that you do not know how many elements will be in the list so you cannot use

a for-loop. You also cannot alter the linked list structure in any way.

public int length()

public class Element { private Element next; public Element getNext() {return this.next;} public void setNext(Element next) {this.next = next;} public int length() {return 0;} public static void main(String[]args){ Element node1 = new Element(); Element node2 = new Element(); Element node3 = new Element(); Element node4 = new Element(); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node1); System.out.println(node1.length()); } }