Lecture06 - More Linked List Operations EECS 233-2-...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More Linked List Operations EECS 233-2- Announcement Academic integrity week: February 11-15th Schedule of events, details, etc: http://studentaffairs.case.edu/groups/aiboard/iweek/default.html-3- Arrays versus Linked Lists Two implementations of list ADT Array implementation + Compact + EfFcient random access (using index)- InefFcient insert/delete operations- Need to preallocate maximum size Link list implementation + EfFcient insert/delete + Easy to grow- Random access takes O(N) running time- Waste space for the links Feld Singly-linked lists versus doubly-linked lists-4- Traversing A Linked List Common operation for many tasks. Can be done using recursion or iteration. We make use of a variable (call it trav) that keeps track of where we are in the linked list (a simple linked list here). Template for traversing an entire linked list: trav = lst_head.next; // start with the frst node (or myList.getFirst() i // rom outside while (trav != null) { // usually do something here trav = trav.next; // move trav down one node } lst_head f o x null trav-5- Example: toUpperCase() toUpperCase(str): converting str to all upper-case letters public static void toUpperCase(LLString str) { StringNode trav; if (str.size() > 0) trav = str.getFirst(); while (trav != null) { if (trav.ch >= 'a' && trav.ch <= 'z') trav.ch += ('A' 'a'); trav = trav.next; } } f o x null trav stack storage heap storage lst_head-6- Tracing toUpperCase() After the Frst iteration in the while loop while (trav != null) { if (trav.ch >= 'a' && trav.ch <= 'z') trav.ch += ('A' 'a'); trav = trav.next; } and after the second iteration: lst_head o x null trav x O x null trav x lst_head-7- Tracing toUpperCase() after the third iteration while (trav != null) { if (trav>ch >= 'a' && trav->ch <= 'z') trav->ch += ('A' 'a'); trav = trav->next; } and now trav == null, so we break out of the loop and return from toUpperCase(). The changes are already reFected in the linked list. O X null trav x null lst_head-8- Duplicating A Simple Linked List Helper method: Take the starting StringNode Copy all elements to the end Return the frst element oF the new list Recursive method to copy(str) Base case: iF str is empty, return null Recursion: copy the frst character and then make a recursive call to copy the rest Preliminaries: StringNode constructor...
View Full Document

This note was uploaded on 04/07/2008 for the course EECS 233 taught by Professor Rabinovich during the Spring '08 term at Case Western.

Page1 / 24

Lecture06 - More Linked List Operations EECS 233-2-...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online