Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Generic LinkedList<> Collection class

Similar presentations


Presentation on theme: "The Generic LinkedList<> Collection class"— Presentation transcript:

1 The Generic LinkedList<> Collection class
Modern Collections Classes

2 Table Of Contents

3 Generic LinkedList<> Overview

4 LinkedList<> stores data in a ‘chain’ of nodes
The Stack Main() numList static void Main(string[] args) { LinkedList<int> numList = new LinkedList<int>(); numList.AddFirst(10); numList.AddLast(20); numList.AddLast(30); foreach( int num in numList) Console.WriteLine(num); } LinkedList<int> object Internal reference to first and last nodes in the list 20 10 30 null null

5 LinkedList<int>
Overview The Stack Main() numList LinkedList<> class allows you to create, add, remove, and move nodes within the list This saves you time by not having to implement (write) it yourself You can use this to access items one at a time (as you walk along the chain). You can get the individual nodes themselves You can quickly add / remove individual items (as they spliced into / out of the chain) You CANNOT jump to a location quickly (in contrast to arrays and the basic List) First Last LinkedList<int> 2 1

6 Warm Up Code

7 Let’s start by looking at code that doesn’t interact with the nodes themselves

8 AddFirst() method, foreach loop
The Stack Main() numList // Part 1 LinkedList<int> numList = new LinkedList<int>(); numList.AddFirst(1); numList.AddFirst(2); foreach (int num in numList) { Console.WriteLine(num); } Create a LinkedList of int’s Then add a couple ints Foreach can be used to access every element in the list First Last LinkedList<int> null null OUTPUT: 2 1 2 1

9 LinkedList<int>
AddLast() method The Stack Main() numList OUTPUT: List contents: 2, 1, 3, 4, 5 // Part 2 numList.AddLast(3); numList.AddLast(4); numList.AddLast(5); PrintList(numList); First Last LinkedList<int> 2 1 3 4 5

10 RemoveFirst(), RemoveLast(), Remove()
The Stack Main() numList OUTPUT: List contents: 1, 3, 4 1, 4 // Part 3 numList.RemoveFirst(); numList.RemoveLast(); PrintList(numList); numList.Remove(3); // NOTE: Removing via the VALUE // Need to find value – O(N) First Last LinkedList<int> 2 3 5 1 4

11 Now let’s look at code that chooses to be uses the nodes directly

12 .First, .Next, .Previous instance variables
OUTPUT: First node’s value: 1 Next node’s value: 4 Previous node’s value: null // Part 4 LinkedListNode<int> refToNode; // no node created yet refToNode = numList.First; Console.WriteLine(“First node’s value: {0}", refToNode.Value); if (refToNode.Next != null) Console.WriteLine("Next node’s value: {0}", refToNode.Next.Value); else Console.WriteLine("Next node’s value: null"); if (refToNode.Previous != null ) Console.WriteLine("Previous node’s value: {0}", refToNode.Previous.Value); Console.WriteLine("Previous node’s value: null"); refToNode First Last LinkedList<int> 1 4

13 AddAfter(), AddBefore()
OUTPUT: List contents: 1, 6, 4 7, 1, 6, 4 // Part 5 numList.AddAfter(refToNode, 6); PrintList(numList); numList.AddBefore(refToNode, 7); First Last LinkedList<int> refToNode 7 6 1 4

14 LinkedList<int>
Remove(node) OUTPUT: refToNode points to 1 refToNode STILL points to 1 First list: List contents: 7, 6, 4 // Part 6 Console.WriteLine("refToNode points to {0}", refToNode.Value); numList.Remove(refToNode); // NOTE: Removing via the NODE // Don’t have to find, so it’s O(1) Console.WriteLine("refToNode STILL points to {0}", refToNode.Value); Console.Write("First list: "); PrintList(numList); First Last LinkedList<int> 1 refToNode 4 7 6

15 Moving a node to a different list is fine
OUTPUT: Second list: List contents: 1 // Part 7 LinkedList<int> secondList = new LinkedList<int>(); secondList.AddFirst(refToNode); Console.Write("Second list: "); PrintList(secondList); First Last LinkedList<int> First Last LinkedList<int> null 1 refToNode null 7 6 4

16 LinkedList<int>
Find() OUTPUT: Found 4 Did NOT find 8 // Part 8 LinkedListNode<int> findIt = numList.Find(4); if (findIt != null) Console.WriteLine("Found 4"); else Console.WriteLine("Did NOT find 4"); LinkedListNode<int> findIt = numList.Find(8); Console.WriteLine("Found 8"); Console.WriteLine("Did NOT find 8"); First Last LinkedList<int> findIt 7 6 4 null

17 Methods You Must Memorize

18 Useful Methods And Properties
The Count property (how many items are in the list) AddFirst(), AddLast(): Adding new values, or a LinkedListNode, to either end of the list RemoveFirst(), RemoveLast(), Remove(T) foreach loop .First, .Last properties on the List .Next, .Previous, .Value properties on the LinkedListNode Find() AddAfter(), AddBefore() Add value, or LLNode, to the list Remove(LinkedListNode<>)

19 More Useful Methods Contains: Find an element by doing a linear search
LinkedList doesn’t have a Sort method LinkedList doesn’t have a BinarySearch Because you can’t jump to an arbitrary spot Specifically, it’s O(N) to jump to the middle So even if the list was sorted it would take O(N•lgN) to execute …Or you could do a linear search for O(N) ☺

20 How is List<> implemented?
How can we figure this out on our own?

21 Search The Web This particular data structure is very public about how it’s structured. Interesting to note that you’re not allowed to manipulate the internal structures yourself You can call AddBefore to add a node before an existing node HOWEVER, you can’t change First/Last/Next/Previous yourself

22 Method Running Times in Big Oh notation

23 Running times for list-based Add methods
The running time for AddFirst( valueToAdd ) : “This method is an O(1) operation.”, according to the docs on MSDN Because you’re adding the new element to the very start of the list, and because the LinkedList class keeps track of First, it takes a constant amount of time add the new node in In other words, you’re adding a single node from a known location, as demonstrated previously The running time for AddLast( valueToAdd ): “This method is an O(1) operation.” Similar to AddFirst, you’re adding the new node to the end of the list, and the list tracks the last node (as you’ve seen in previous pictures)

24 Running times for node-based Add methods
The running time for AddAfter(LinkedListNode<>, valueToAdd): “This method is an O(1) operation.”, according to the docs on MSDN Because you’re adding the new element to the very start of the list, and because the LinkedList class keeps track of First, it takes a constant amount of time add the new node in In other words, you’re adding a single node from a known location, as demonstrated previously The running time for AddBefore(LinkedListNode<>, valueToAdd): “This method is an O(1) operation.” Similar to AddFirst, you’re adding the new node to the end of the list, and the list tracks the last node (as you’ve seen in previous pictures)

25 Running times for Removes:
The running time for RemoveFirst() and for RemoveLast() is O(1) For both, the ONLY remark is: “This method is an O(1) operation.” ☺ You’re removing a single node from a known location, as demonstrated previously Remove(T) is O(N), because it has to walk through the list to find the thing to remove Example: “numList.Remove(3);” “This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.” You need to search through the list to find the thing to remove, as demonstrated previously Remove(LinkedListNode<T>) is O(1) Example: “numList.Remove(refToNode);” “This method is an O(1) operation.”

26 Running times: What the docs say
Find(T) is O(N), because it has to walk through the list to find the thing to return “This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.” You need to search through the list to find the thing to remove, as demonstrated previously

27 When to use LinkedList<>

28 Why /why not use a LinkedList<>?
It’s good when you want to access the values sequentially (i.e., walk through the values in order) and you want to add new items into the middle as you walk through the list Fast (constant-time) insertion or removal But only at the start/end of the list, or near a node that you previously found In contrast, the array-based List<> can only add things to the end O(1) for adding to the end O(N) to shuffle everything down when adding to the middle) Slower (linear-time) access to the elements The best you can do for finding an element is O(N) / linear time Each <thing> we store requires a node, so it’s bad for storing a lot of small things For example, storing a lot of individual characters would be very inefficient


Download ppt "The Generic LinkedList<> Collection class"

Similar presentations


Ads by Google