 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
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.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Stacks, Queues, and Deques
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
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.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List by Chapter 5 Linked List by
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Week 4 - Monday CS221.
Marcus Biel, Software Craftsman
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
CSCI-255 LinkedList.
Linked Lists.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Introduction to Data Structure
Linked Lists.
Presentation transcript:

 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman

 2015, Marcus Biel, Terminology First of all, let’s have a look at the term “Linked List”. Why is Linked List actually called Linked List?

 2015, Marcus Biel, Terminology Link For the term “Link” - as an analogy - think of a weblink – when you click on it, it brings you from one website to the next.

 2015, Marcus Biel, Terminology List A list is a collection of related objects. Think of a shopping list - it contains every item that you plan to buy.

 2015, Marcus Biel, Terminology Linked List So a Linked List is a collection of related objects, where a link will bring you from one item to the next item.

 2015, Marcus Biel, Terminology Linked List Technically, in our Linked List, an item or entry of the list…

 2015, Marcus Biel, Node … is stored in a so called “Node”.

 2015, Marcus Biel, 23 Node In our simple example, this entry is the number twenty three.

 2015, Marcus Biel, Node 23 Each node has a link or pointer to the next element of the list, the next node.

 2015, Marcus Biel, Singly Linked List 23 For every item that we want to add to the Linked List, we create a node and store the item in it.

 2015, Marcus Biel, Singly Linked List 23 The first item of the list is usually called the head, the last item of the list the tail of the list.

 2015, Marcus Biel, Singly Linked List 233 For every new item, we create a new node…

 2015, Marcus Biel, Singly Linked List 2393 … and link to it from the last element of the list.

 2015, Marcus Biel, Singly Linked List So a Linked List is a dynamic data structure, that can hold any number of elements, as long as enough memory is available.

 2015, Marcus Biel, Singly Linked List next ( ) After the list is created, to navigate through the list we call a function like next() which uses the link to go from one item to the next.

 2015, Marcus Biel, Singly Linked List This type of a Linked List is called a Singly Linked List because the links only go in one direction -

 2015, Marcus Biel, Singly Linked List from the head of the list -

 2015, Marcus Biel, Singly Linked List to the tail.

 2015, Marcus Biel, Singly Linked List So to navigate to the previous element, for example from the element 42 -

 2015, Marcus Biel, Singly Linked List to the element 9

 2015, Marcus Biel, Singly Linked List you have to go back to the head of the list -

 2015, Marcus Biel, Singly Linked List next ( ) and you have to call the function next() -

 2015, Marcus Biel, Singly Linked List on every single element -

 2015, Marcus Biel, Singly Linked List next ( ) until you reach the element 9.

 2015, Marcus Biel, Singly Linked List If the list contains a lot of elements, this may take some time.

 2015, Marcus Biel, Singly Linked List Inserting an element after the current element is relatively easy with a Singly Linked List.

 2015, Marcus Biel, Singly Linked List You create a new Node containing the new element.

 2015, Marcus Biel, Singly Linked List You link to the new element „17“ from the current element „9”.

 2015, Marcus Biel, Singly Linked List You add a link pointing from the new “17” element to the existing “42” element.

 2015, Marcus Biel, Singly Linked List And you’r done.

 2015, Marcus Biel, Singly Linked List Inserting the same element BEFORE the current element is possible in a Singly Linked List, but usually not very efficient.

 2015, Marcus Biel, Singly Linked List It will require to navigate to the previous element, starting from the beginning of the list, as I showed you before.

 2015, Marcus Biel, Singly Linked List Removing an element from a Singly Linked List has the same issue – it is possible, but generally not very efficient.

 2015, Marcus Biel, Node 23 These operations get much easier, when you add a second link to each node, pointing to the previous element.

 2015, Marcus Biel, Node 23 This way you can navigate in both directions of the list.

 2015, Marcus Biel, Node 23 However the extra link comes at a cost, of extra memory, as well as time to build the more complex structure.

 2015, Marcus Biel, Node 23 If this overhead is justified, I cannot generally answer, as it will differ for each use case. If performance is an issue, I advise you to test different options.

 2015, Marcus Biel, Doubly Linked List 23 so a Linked List that contains nodes that provide a link to the next and the previous node is called a “Doubly Linked List”.

 2015, Marcus Biel, Doubly Linked List 233 For every element that you add to a Doubly Linked List, you need to create two links, so creating a Doubly Linked List is somewhat more complicated.

 2015, Marcus Biel, Doubly Linked List 2339 Navigation in both directions in a Doubly Linked List is easier, but at the cost of a more complex structure.

 2015, Marcus Biel, Doubly Linked List Based on the two way link structure…

 2015, Marcus Biel, Doubly Linked List adding or removing an element before or after the current element is relatively easy.

 2015, Marcus Biel, Doubly Linked List Here we will add the element “17” before the current element “9”.

 2015, Marcus Biel, Doubly Linked List The back link from element “9” to element “3” is removed…

 2015, Marcus Biel, Doubly Linked List and replaced by a back link to the new element “17”.

 2015, Marcus Biel, Doubly Linked List From the new element “17” we link to the next element “9”.

 2015, Marcus Biel, Doubly Linked List …next, we place a back link from 17 to 3…

 2015, Marcus Biel, Doubly Linked List The old link from element 3 to 9 is replaced by a link from 3 to 17.

 2015, Marcus Biel, Doubly Linked List Removing an element from a Doubly Linked List has the same steps as inserting an element to the list, just in reverse order.

 2015, Marcus Biel, public class Node { private E item; private Node next; public Node(E element, Node next) { this.item = element; this.next = next; } public E item() { return item; } public Node next() { return next; } […] } Node Let‘s look at a code excerpt of a Node in a Singly Linked List. So you have a method to retrieve the item of a Node, and a method to go to the next Node.

 2015, Marcus Biel, Node public class Node { private E item; private Node previous; private Node next; public Node(Node previous, E element, Node next) { this.item = element; this.next = next; this.previous = previous; } public E item() { return item; } public Node previous() { return previous; } public Node next() { return next; } […] } The Node of a Doubly Linked List is very similar, but a bit more complex. Additionally, you have a reference to the previous Node.

 2015, Marcus Biel, public class LinkedList { private Node currentNode; private Node head; public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…} […] } LinkedList A LinkedList usually contains a link to the head of the List. The methods of the Node class will be used to navigate through the list.

 2015, Marcus Biel, public class LinkedList { private Node currentNode; private Node head; public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…} […] } LinkedList The concrete implementation of methods to get, add or remove an element from the list, depends on the type of the node, but such an implementation detail is usually hidden from a user of the list.

 2015, Marcus Biel, public class LinkedList { private Node currentNode; private Node head; private Node tail; public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…} […] } LinkedList Besides the direct link to the current node of the list and the head of the list, a Linked List may also provide a direct link to the tail of the list. This is common for a Doubly Linked List, but may also be beneficial for a Singly Linked List.

 2015, Marcus Biel, Application Scenarios List Queue Stack Double Ended Queue (Deque) Let’s have a look at different application scenarios for a Linked List. A Linked List can be used to implement data structures like List, Queue, Stack or Double Ended Queue.

 2015, Marcus Biel, Application Scenarios List Queue Stack Double Ended Queue (Deque) When comparing implementations based on a Singly- or a Doubly Linked List, there is no overall winner.

 2015, Marcus Biel, Application Scenarios List Queue Stack Double Ended Queue (Deque) Both have advantages and disadvantages that make them useful for different application scenarios.

 2015, Marcus Biel, Application Scenarios List Queue Stack Double Ended Queue (Deque) Using the example of List, Queue, Stack and Double Ended Queue we will in each case decide for a Singly- or a Doubly Linked based implementation.

 2015, Marcus Biel, Application Scenarios List Queue Stack Double Ended Queue (Deque) An implementation with other data structures, for example an array, is also possible, but that’s a different story.

 2015, Marcus Biel, List Queue Stack Double Ended Queue (Deque) Application Scenarios A list usually requires random access to any element of the list, so I would recommend an implementation based on a Doubly Linked List.

 2015, Marcus Biel, List Queue Stack Double Ended Queue (Deque) Application Scenarios In a so called “first in first out” Queue, new elements are inserted at the tail of the queue and removed from the head of the queue.

 2015, Marcus Biel, List Queue Stack Double Ended Queue (Deque) Application Scenarios Random access is not required, so I would recommend an implementation based on a Singly Linked List.

 2015, Marcus Biel, List Queue Stack Double Ended Queue (Deque) Application Scenarios A Stack provides a so called “Last in First out” order. Elements are added and removed from the head of the Stack, which makes it even more simple then a Queue.

 2015, Marcus Biel, List Queue Stack Double Ended Queue (Deque) Application Scenarios Therefore a Singly Linked List is usually sufficient.

 2015, Marcus Biel, List Queue Stack Double Ended Queue (Deque) Application Scenarios A Double Ended Queue or Deque is a very dynamic data structure. It allows to access, add or remove elements from both ends. Therefore I would use a Doubly Linked List to implement a Deque.