DOC PREVIEW
Princeton COS 126 - Lecture 15

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COS126: General Computer Science • http://www .cs.Princeton.EDU/~cos126Lecture 15: Linked StructuresLewis CarollThrough the Looking Glass"The name of the song is called 'Haddocks' Eyes.' ""Oh, that's the name of the song, is it?" Alice said,trying to feel interested."No, you don't understand," the Knight said, looking alittle vexed. "That's what the name is called. Thename really is 'The Aged Aged Man.' ""Then I ought to have said 'That's what the song iscalled' ?" Alice corrected herself."No, you oughtn't: that's quite another thing! Thesong is called 'Ways and Means,' but that is only whatit's called, you know!""Well, what is the song, then?" said Alice, who was bythis time completely bewildered."I was coming to that," the Knight said. "The songreally is 'A-sitting On A Gate,' and the tune's my owninvention."2Linked vs. Sequential AllocationGoal: process a collection of objects.Sequential allocation: put object one after another.! TOY: consecutive memory cells.! Java: array of objects.Linked allocation: include in each object a link to the next one.! TOY: link is memory address of next object.! Java: link is reference to next object.Key distinctions.! Sequential allocation: random access, fixed size.! Linked allocation: sequential access, variable size.3Linked list of strings.! A recursive data structure.! A string plus a pointer to another linked list (or empty list).! Unwind recursion: linked list is a sequence of strings.Linked lists in Java.! Easy to define as a Java class.! A reference to a String.! A reference to another List.! Use special value null to terminate list.Linked Listspublic class List { private String name; private List next;}AlicenullBobCarolList x4Linked List DemoList a = new List();a.name = "Alice";a.next = null;List b = new List();b.name = "Bob";b.next = a;List c = new List();c.name = "Carol";c.next = b;"Bob"C9C0CA0CB0C50C60C70C8"Alice"C0nullC10C2"Carol"C3C9C4valueaddrmain memoryC0aregistersListC9bListC3cListAlicenullBobCarolc b a5Traversing a ListParadigm for traversing a null-terminated linked list.for (List x = c; x != null; x = x.next) { System.out.println(x.name);}AlicenullBobCarol% java Listx12 Stack and Queue ADTsFundamental data type.! Set of operations (add, remove, test if empty) on generic data.! Intent is clear when we insert.! Which object do we remove?Stack.! Remove the object most recently added. ("last in first out")! Analogy: cafeteria trays, surfing Web.Queue.! Remove the object least recently added. ("first in first out")! Analogy: Registrar's line.Multiset.! Remove any object.! Law professor calls on arbitrary student.13QueueQueue operations.! enqeuue Insert a new object onto queue.! dequeue Delete and return the object least recently added.! isEmpty Is the queue empty?public static void main(String[] args) { Queue q = new Queue(); q.enqueue("Vertigo"); q.enqueue("Just Lose It"); q.enqueue("Pieces of Me"); q.enqueue("Pieces of Me"); System.out.println(q.dequeue()); q.enqueue("Drop It Like It's Hot"); while(!q.isEmpty()) System.out.println(q.dequeue());}A simple queue client14More Applications of QueuesReal world applications.! iTunes playlist.! Echo filter to store last ten waves.! Dispensing requests on a shared resource (printer, processor).! Asynchronous data transfer (file IO, pipes, sockets).! Data buffers (iPod, TiVo).! Graph processing (stay tuned).Simulations of the real world.! Traffic analysis of Lincoln tunnel.! Waiting times of customers in McDonalds .! Determining number of cashiers to have at a supermarket.15 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.firstnow is the timelast16 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.Insert.firstnow is the timelastList x = new List();x.item = "for";last.next = x;last = x;x17 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.Insert.firstnow is the timelastforList x = new List();x.item = "for";last.next = x;last = x;x18 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.Insert.firstnow is the timelastforList x = new List();x.item = "for";last.next = x;last = x;x19 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.Insert.firstnow is the timelastforList x = new List();x.item = "for";last.next = x;last = x;20 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.Delete.firstnow is the timelastforString val = first.item;first = first.next;return val;nowval21 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.Delete.firstnow is the timelastforString val = first.item;first = first.next;return val;nowval22 Queue: Linked List ImplementationLinked list implementation.! Maintain linked list of elements.! Let first be reference to first node on list.! Let last be reference last node on list.Delete.firstnow is the timelastforString val = first.item;first = first.next;return val;nowval23 Queue: Linked List Implementationpublic class Queue { private List first; private List last; private class List { String item; List next; } public boolean isEmpty() { return (first == null); } public void enqueue(String anItem) { List x = new List(); x.item = anItem; x.next = null; if (isEmpty()) { first = x; last = x; } else { last.next = x; last = x; } } public String dequeue() { String val = first.item; first = first.next; return val; }} reference to first element in queuereference to last element in queuedelete first elementnested class24Binary tree.! Organize homogeneous collection of values (all same type).! Associate two pointers with each value.! Use pointers to access each branch of the


View Full Document

Princeton COS 126 - Lecture 15

Download Lecture 15
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 15 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 15 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?