The Generic LinkedList<> Collection class

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Data Structure Introduction.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
List Interface and Linked List Mrs. Furman March 25, 2010.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Introduction toData structures and Algorithms
CMSC 202 ArrayList Aug 9, 2007.
Using the Java Collection Libraries COMP 103 # T2
Lecture 10 Collections Richard Gesick.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
The List ADT.
Week 4 - Monday CS221.
Understanding Algorithms and Data Structures
Single-Linked Lists.
Chapter 12 – Data Structures
Compsci 201 Midterm 1 Review
4. Linked Lists.
COMP 53 – Week Eleven Hashtables.
Chapter 5 Ordered List.
Search Lesson Outline Searching Lesson Outline
Sequences and Iterators
Data Structures and Database Applications Linked Lists
Chapter 17 Object-Oriented Data Structures
Top Ten Words that Almost Rhyme with “Peas”
CS313D: Advanced Programming Language
Linked Lists.
Circular Buffers, Linked Lists
Chapter 8: Collections: Arrays
Chapter 5 Ordered List.
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Lecture 20 Linked Lists Richard Gesick.
Arrays and Collections
CMSC 202 ArrayList Aug 9, 2007.
Linked Lists.
Object Oriented Programming in java
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
CMSC 202 ArrayList Aug 9, 2007.
Introduction to C++ Linear Linked Lists
Revision of C++.
CSC 143 Java Linked Lists.
The Generic List<> Collection class
Data Structures & Algorithms
Fundaments of Game Design
Amortized Analysis and Heaps Intro
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Marty Stepp
Building Java Programs
TCSS 143, Autumn 2004 Lecture Notes
slides created by Marty Stepp
Presentation transcript:

The Generic LinkedList<> Collection class Modern Collections Classes

Table Of Contents

Generic LinkedList<> Overview

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

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

Warm Up Code

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

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

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

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

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

.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

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

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

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

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

Methods You Must Memorize

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<>)

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) ☺

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

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

Method Running Times in Big Oh notation

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)

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)

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.”

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

When to use LinkedList<>

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