Assignment_W2 Final

Assignment_W2 Final - EECS 233 Written Assignment #2...

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

View Full Document Right Arrow Icon
EECS 233 Written Assignment #2 Akshaya Annavajhala Due Feb 21, 2008 (midnight, 23:59:59) 1. (a) Let Node p refer to an element in a list. Write a helper method that swaps p and the subsequent element by swapping only the links (and not the data). public class MyList { private Node head ; …. private boolean swap(Node p) { // Swaps p and subsequent element. // Returns true if success, false otherwise. Node previous = null ; Node current = head ; Node pprevious = null ; Node ccurrent = head.next ; for (int i = 0 ;i<MyList.size() ;i++){ if (previous == p) break ; if (current.next != null){ pprevious = previous ; previous = current ; current = ccurrent; ccurrent = ccurrent.next ; } else return false ; } pprevious.next = current ; current.next = previous ; previous.next = ccurrent ; return true ; Node previous = null ; Node pprevious = null ; Node current = head ; Node ccurrent = head.next ; for (int i = 0 ; i < MyList.size() ; i++){ if (previous ==p) break ; if (current.next !=null ;){ pprevious = previous ; previous= current ; current = ccurrent ; ccurrent = ccurrent.next ; } else return false ; } pprevious.next = current ; previous.next = ccurrent ; current.next = previous ; previous.prev = current ; current.prev=pprevious ;
Background image of page 1

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

View Full DocumentRight Arrow Icon
ccurrent.previous=previous ;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Assignment_W2 Final - EECS 233 Written Assignment #2...

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