Linked Lists Topics Understanding Linked Lists Pros and

  • Slides: 16
Download presentation
Linked Lists

Linked Lists

Topics • Understanding Linked Lists • Pros and cons of Linked Lists. • Using

Topics • Understanding Linked Lists • Pros and cons of Linked Lists. • Using the Java Linked. List Class. • Implementing a Linked. List Class. • Adding Linked Nodes • Deleting Linked Nodes

Understanding Linked Lists • What is a linked list and how is it similar

Understanding Linked Lists • What is a linked list and how is it similar to an array or an Array. List? A Linked List is an ordered collection of elements. An array and an Array. List are also ordered collection of elements. • How is a Linked List different from an array or an Array. List? The difference between a Linked List and an array or Array. List is efficiency. A linked list allows efficient addition and removal of elements in the middle of a sequence.

In-depth look at Efficiency of Ordered Elements • To understand the inefficiency of an

In-depth look at Efficiency of Ordered Elements • To understand the inefficiency of an array of Array. List, imagine a program that maintains a sequence of users. • If a user wants to be removed from the list, a gap is created when an element is deleted. • Once a gap appears, it needs to be closed up. • Conversely, when a user needs to be added in the middle of an ordered sequence, all the users following the new user must be moved to the end of the ordered list.

Gaps created when Deleting and Adding Elements in an Array or Array. List Moving

Gaps created when Deleting and Adding Elements in an Array or Array. List Moving a large number of elements in an Array or Array. List can involve a substantial amount of processing time.

What is a Linked List? • A linked list uses a sequence of nodes

What is a Linked List? • A linked list uses a sequence of nodes to create an ordered list. • A node is an object that stores an element and references to the neighboring nodes in the sequence. Example : c 2 through c 8 are nodes in a Linked List

Benefit of a Linked Lists allow speedy insertion and removal. Before: After:

Benefit of a Linked Lists allow speedy insertion and removal. Before: After:

Cons of a Linked List Accessing elements in a Linked List can be slow

Cons of a Linked List Accessing elements in a Linked List can be slow because it is sequential. Example: Accessing c 5 requires that we visit c 2, c 3, then c 5. Or, in reverse we visit c 8, c 7, and then c 5.

Using the Java Linked. List Class • Creating a basic Linked List can be

Using the Java Linked. List Class • Creating a basic Linked List can be done by using the Linked. List class provided by the Java Library. • This class is part of the java. util package. • Creating a Linked List requires a class. Example: Linked. List<Integer> list = new Linked. List<Integer>();

Linked. List Class Methods add. Last() add. First() get. Last() remove. First()

Linked. List Class Methods add. Last() add. First() get. Last() remove. First()

Linked. List Class Inherited Methods The Linked. List class also inherits the methods of

Linked. List Class Inherited Methods The Linked. List class also inherits the methods of the collection interface. size() add() to. String() contains()

Traversing a Linked. List You may use the for each loop structure with any

Traversing a Linked. List You may use the for each loop structure with any collection. Example for (Integer c: list) System. out. println(c);

Iterators for Visiting an element in a Linked. List Provide an iterator for visiting

Iterators for Visiting an element in a Linked. List Provide an iterator for visiting list elements. Example List. Iterator<Integer> itr = list. Iterator(); __________________________ You can use methods of the Iterator and List. Iterator Interfaces next() previous() has. Next() has. Previous() set() add() remove()

Tips for Linked. Lists • Draw before and after diagrams showing relevant references. •

Tips for Linked. Lists • Draw before and after diagrams showing relevant references. • Use before and after diagrams to show elements that need to be altered. • Be sure that no links are left undefined at the conclusion of your code. • Dangling nodes will become garbage and collected by the garbage collector. • Verify that your algorithm works correctly for an empty list and for a list with only one node.

Practice – Create the Linked List Below Additional Tasks: a) Create an iterator and

Practice – Create the Linked List Below Additional Tasks: a) Create an iterator and position it after the third number in the list. Insert the number 4 at this position. b) Remove the number 5 from the list. You will need to reposition the iterator. c) Display the new contents of the linked list.

Implementing a Linked. List Class • Create your own Linked. List Class. • Linked.

Implementing a Linked. List Class • Create your own Linked. List Class. • Linked. List will rely on a Node Class containing a generic object data. • This first Linked. List will be a single linked list. • A pointer will be used to point to the next node. • No pointers will be used to point to a previous node. • Member methods will include add. Node() and delete. Node()