Lecture 9 Single Linked List Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1
» What is Linked List » Iteration, Inserting, Removing » Implementation » Linked List in C# » Linked List VS List » Performance Comparison with List » When use List, and Linked List » Exercise 2 © Sekolah Tinggi Teknik Surabaya
3
» A data structure consisting of a group of nodes which together represent a sequence. » Example: » Linked List with Header: 4 © Sekolah Tinggi Teknik Surabaya Next Property Node Class Element Value Property
» Store its element in Node Class » Each Node have next reference to hold next element » Linked List Class have header » Last element is Nothing/Null » If header is null then the linked list is empty 5 © Sekolah Tinggi Teknik Surabaya
6
» Start with header, trace its next » Example: Finding Bacon 7 © Sekolah Tinggi Teknik Surabaya It is Bacon ?
» Example: Inserting Cookies After Eggs » Create Node Cookies » Find Node Eggs, get its Next (Bacon) » Set Cookies’s Next to Bacon » Set Eggs’s Next to Cookies 8 © Sekolah Tinggi Teknik Surabaya
» Example: Removing Bacon » Find Bacon » Get Bacon’s Previous Node (Cookies) » Get Bacon’s Next Node (Nothing) » Set Cookies’s Next to Nothing 9 © Sekolah Tinggi Teknik Surabaya
10
» Generic Class » Store Element » Have Link to store its Next 11 © Sekolah Tinggi Teknik Surabaya
» Constructor and Property 12 © Sekolah Tinggi Teknik Surabaya
» Find and Insert Method 13 © Sekolah Tinggi Teknik Surabaya
» Find Previous and Remove Method 14 © Sekolah Tinggi Teknik Surabaya
» PrintList Method (Iteration) 15 © Sekolah Tinggi Teknik Surabaya
16
» A generic collection class System.Collections.Generic.LinkedList » Node Class: LinkedListNode 17 © Sekolah Tinggi Teknik Surabaya
18
DifferenceListLinked List Storing ElementArray basedNode reference based Access ElementBy its indexFind the node, from header Insertion Method1.Add 2.AddRange 3.Insert 4.InsertRange 1.AddAfter 2.AddBefore 3.AddFirst 4.AddLast Remove1.Remove 2.RemoveAll 3.RemoveAt 4.RemoveRange 1.Remove 2.RemoveFirst 3.RemoveLast Select1.List[index] 2.List.ElementAt 1.Link.Find 2.Link.FindLast 3.Link.ElementAt 19 © Sekolah Tinggi Teknik Surabaya
20
» Adding, Iterate, Insert, and Deleting element » Use Stopwatch Class to measure the time 21 © Sekolah Tinggi Teknik Surabaya
22 © Sekolah Tinggi Teknik Surabaya
23 © Sekolah Tinggi Teknik Surabaya
24 © Sekolah Tinggi Teknik Surabaya
25 © Sekolah Tinggi Teknik Surabaya
26 © Sekolah Tinggi Teknik Surabaya
27 © Sekolah Tinggi Teknik Surabaya
28 © Sekolah Tinggi Teknik Surabaya
29 © Sekolah Tinggi Teknik Surabaya
30
» Fast Adding » Fast Iteration 31 © Sekolah Tinggi Teknik Surabaya
» Fast Insertion » Fast Deletion 32 © Sekolah Tinggi Teknik Surabaya
33
Write a program that can read an indefinite number of lines of VB.NET code and store reserved words in one linked list and identifiers and literals in another linked list. When the program has finished reading input, display the contents of each linked list. 34 © Sekolah Tinggi Teknik Surabaya
» Andrew Troelsen, Pro C# and The.Net 4.5 Framework (Sixth Edition), Apress, 2012 » Linked List MSDN, us/library/he2s3bh7(v%3Dvs.110).aspx us/library/he2s3bh7(v%3Dvs.110).aspx 35 © Sekolah Tinggi Teknik Surabaya