Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 3 - Linked List. Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today in Q1! ADT List.

Similar presentations


Presentation on theme: "Tutorial 3 - Linked List. Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today in Q1! ADT List."— Presentation transcript:

1 Tutorial 3 - Linked List

2 Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today in Q1! ADT List using Linked List –Basic idea: Slide 17 Linked List Node has 2 parts: –Item/Value/Content  See T03Sup1&2 –Pointers to immediate neighbors Single Linked List –The basic; traversal: head to tail –Usually: insert from head (ok for Stack) –In your lecture notes: BasicLinkList (Slide 26-28), ExtendedLinkedList (Slide 29-36) Generic Java (Slide 37-46) for our custom Basic and Extended LinkedList Linked List Variations: Linked List with Tail Pointer –Can visit the tail very fast –Cannot delete the tail easily…  –In lecture notes (Slide 48-53): TailedLinkedList (revisited in Queue data structure later) Bidirectional Linked List –Two pointers: forward/backward –Can go backwards, Can delete tail! –Extra pointer is overhead  –In lecture notes (Slide 54-58): DoublyLinkedList Circular Linked List –Remember Tail only, Head = Tail.Next –Can visit all node from any node. Generic LinkedList (slide 61-66) for a “bug-free”, “ready-to-use” LL Reserved for next week…

3 Student Presentation T3 Sup1.Ng Hong Geh Sup2.N/A 1.Koh Xianghua, Nicholas (deferred until next week) 2.N/A 3.N/A 4.Seo Jia Wei David T4 Sup1.Chong Tze Koi Sup2.Chong Tze Koi 1.Tan Miang Yeow 2.N/A 3.N/A 4.N/A T5 Sup1.Wang Ruohan Sup2.Zhang Denan, Zhang Yang 1.N/A 2.N/A 3.N/A 4.N/A T6 Sup1.N/A Sup2.N/A 1.Kuganeswari D/O Kuhanesan (deferred until next week) 2.N/A 3.N/A 4.Koh Jye Yiing 3 Hm… not that good… Anyone wants to take Sup1 & Sup2 questions?

4 Question Supplementary 1 ListNode First = new ListNode(“first”); ListNode Last = First; ListNode Temp; for (int j = 1; j < 6; j++) if (j % 2 == 0) { Temp = new ListNode(“j = “ + j); Last.next = Temp; Last = Temp; } else First = new ListNode(“j = “ + j, First); Trace this!

5 Question Supplementary 2 Now, rearrange: J=5  J=3  J=1  first  J=2  J=4 into first  J=1  J=2  J=3  J=4  J=5

6 Question 1 (MovieDataAnalysis) (Prologue) Array Based ADT (Lect 3, Slide 6-15) –Pros? –Cons? Real question: Insert New Item (Movie ADT) to LinkedList –Ensure that you check for duplicate data! 6

7 Question 2 (MovieDataAnalysis) Given time period (yearA – yearB) –Get movies that are produced within that range. Methods –1: Scan and select the appropriate movies –2: Copy the list and delete movies that are out of range What if we use Doubly Linked List? Pro and Cons? 7 2008 2007 2007 2008 2006

8 Question 3 (MovieDataAnalysis) Given two TopList containing movies, perform: –Intersection –Union –Difference Remember that A-B is not the same as B-A! 8 Person A’s preference:Person B’s preference:

9 Question 4 (Guess Output) ExtendedLinkedList list = new ExtendedLinkedList(); list.addHead(“ZZZ”); list.addHead(“YYY”); list.addHead(“XXX”); list.addHead(“WWW”); list.addHead(“VVV”); A: ListNode node = list.getHeadPtr(); printList(node); –VVV, WWW, XXX, YYY, ZZZ B: System.out.println(node.next.next.next); –YYY C: node = node.next(), printList(node); –WWW, XXX, YYY, ZZZ D: node = list.getHeadPtr(); list.deleteAfter(node); printList(node); –VVV, XXX, YYY, ZZZ E: ListNode fNode = new ListNode(“FFF”, node); node.next = fNode; printList(node); –VVV, FFF, VVV, FFF, … (infinite) 9

10 Next Week Revisit the leftover materials from today’s tutorial Revise stack/queue ThisWeek.Next.Next is recess week –Use your recess week carefully! –CS1102 midterm test is just one week after it!


Download ppt "Tutorial 3 - Linked List. Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today in Q1! ADT List."

Similar presentations


Ads by Google