{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture06

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

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

More Linked List Operations EECS 233

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

View Full Document
-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 + Efficient random access (using index) - Inefficient insert/delete operations - Need to preallocate maximum size Link list implementation + Efficient insert/delete + Easy to grow - Random access takes O(N) running time - Waste space for the links field Singly-linked lists versus doubly-linked lists

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

View Full Document
-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 first node (or myList.getFirst() if // from 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

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

View Full Document
-6- Tracing toUpperCase() After the first 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 F o x null trav x F 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 reflected in the linked list. F O X null trav x null lst_head

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

View Full Document
-8- Duplicating A Simple Linked List Helper method: Take the starting StringNode Copy all elements to the end Return the first element of the new list Recursive method to copy(str) Base case: if str is empty, return null Recursion: copy the first character and then make a recursive call to copy the rest Preliminaries: StringNode constructor public Class StringNode { private char ch; private StringNode next; public StringNode(char myCh, StringNode nextNode){ ch = myCh; next = nextNode; } } S O X null str
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}