Top Banner
Tutorial 3 - Linked List
10

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.

Jan 21, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Tutorial 3 - Linked List

Page 2: 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.

Linked List: Revision• The concept of ADT List• ADT List using Array

– Pro & cons Discussed in T02Q3and 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<E> (slide 61-66) for a “bug-free”, “ready-to-use” LL

Reserved for next week…

Page 3: 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.

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

• T4Sup1. Chong Tze Koi

Sup2. Chong Tze Koi

1. Tan Miang Yeow

2. N/A

3. N/A

4. N/A

• T5Sup1. Wang Ruohan

Sup2. Zhang Denan, Zhang Yang

1. N/A

2. N/A

3. N/A

4. N/A

• T6Sup1. N/A

Sup2. N/A

1. Kuganeswari D/O Kuhanesan (deferred until next week)

2. N/A

3. N/A

4. Koh Jye Yiing

3Hm… not that good…

Anyone wants to take Sup1 & Sup2 questions?

Page 4: 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.

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!

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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

20082008 2007 2007 2007 2007 2008 2008 2006 2006

Page 8: 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.

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:

Page 9: 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.

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

Page 10: 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.

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!