Singly linked lists Doubly linked lists

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Chapter 7. Binary Search Trees
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Linked Lists Course Lecture Slides 23 July 2010 A list is only as strong.
CHP-5 LinkedList.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
(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.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked List by Chapter 5 Linked List by
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Linked Lists (continued). Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle Toronto  (1) Insert node containing (reference to.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
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.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Arrays and Linked Lists
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Problem Understanding
Presentation transcript:

Singly linked lists Doubly linked lists Chapter 2. Linked Lists Singly linked lists Doubly linked lists aka lecture 3

Dynamic data structures Data collections (e.g. stored library records) can vary considerably in size. Arrays not always best solution: Inserting/deleting a new element requires much of array to be rewritten Array size is fixed, must be estimated before use If only few items held, much of array (hence memory) is wasted Solution: dynamic data structures (linked data structures) - don’t need to know how many items to expect - can increase/decrease memory when items added/deleted

Examples Linked lists Trees Binary trees Binary search trees AVL trees B trees Will look at all of these All use objects which are created dynamically, using memory from a special storage area (pool/heap).

Singly Linked Lists (Goodrich § 3.2) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node (or null) First node in list referred to as head next elem node A B C D  head 4

next node For an easy introduction we will have a list of Strings element The thing above is our Node

The Node Class for String objects next node element

The Node Class for String objects Sometimes called “head” next node element

The Node Class for String objects Sometimes called “tail” next node element

The Node Class for String objects constructors next node element

The Node Class for String objects get next node element

The Node Class for String objects set next node element

Over loading next node element

Don’t like

better

The list

head size

head = null size = 0

if head == null return true; else return false; NO!

head  StringList L = size = 3 L.addFront(“pig”) head  StringList L = dog owl cat StringList L = L.addFront(“pig”) head  size = 4 StringList L = dog owl cat pig

Inserting at the Head Allocate a new node

Inserting at the Head Allocate a new node

Inserting at the Head Allocate a new node New node points to old head

Inserting at the Head Allocate a new node New node points to old head Head points to new node

Inserting at the Head Allocate a new node New node points to old head Head points to new node Increase size counter

Removing at the Head Update head to point to next node in the list

Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node

head  StringList L = size = 4 head StringList L = size = 3 YIKES! dog owl cat pig head size = 3 StringList L = dog owl cat pig YIKES!

Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node -Would need to keep a record of which node is the last node and which node points to the last node (even then can’t do it in constant time!)

Java code for a singly linked list of Strings In our implementation instance variables are head (reference to head node) and size (number of nodes currently in list) - Different implementations have different instance variables (some store the head and last/tail, some size (some not)). Will see later, depends what we actually want to do with our list. Our implementation is specific to strings. You will need to adapt it to accept values of other types - we will see a generic linked list later.

We have seen this

Simple Remove and Exception We have seen this But not this

The toString method Iterating over a list

The toString method This is a pointer that traverses the list Iterating over a list

The toString method Iterating over a list

The toString method What’s happening here? Iterating over a list

The toString method What’s happening here? implicit cursor.toString() Iterating over a list

The toString method Iterating over a list

The toString method So we don’t have a list (pig,dog,owl,cat,) Iterating over a list

Iterating over a list … and this is a very nice template (pattern) for iterating over a linked list Iterating over a list

Linear Search: isPresent Checking to see if a node containing a given value is in the list: Use a variable temp of type Node, initially pointing to (node pointed to by) head and progressively pointing to nodes along list until temp points to node containing value of interest, or temp points to null (i.e. end of list): > Searching for “when” return true temp temp temp It’s  easy when you know how head > Searching for “bucket” temp temp temp temp temp temp temp It’s  easy when you know how head ADS2 Lecture 4 return false 45 (Alice’s slide)

Linear Search: isPresent

Linear Search: isPresent Assume we haven’t found what we are looking for

Linear Search: isPresent Our travelling cursor

Linear Search: isPresent Quit if we have hit end-of-list or we found what we are looking for

Linear Search: isPresent Quit if we have hit end-of-list or we found what we are looking for NOTE: we quit if “X or Y” is same as we continue if “¬X and ¬Y” De Morgan’s law!!!!

Linear Search: isPresent Is this what we are looking for?

Linear Search: isPresent Move the cursor down the list

Linear Search: isPresent Did we find it?

Linear Search: isPresent note naming conventions (toString, isPresent, …) note that we do NOT do things like if (x > y) then b = true else b = false please … note, no assumptions about order in data how could we use order if we had it? note, I use “cursor” rather than “temp” (why?)

Linear Search: isPresent NOTE similarity

Linear Search: isPresent Is this better?

Linear Search: isPresent Is this better?

Linear Search: isPresent Is this better?

Linear Search: isPresent Is this better?

Linear Search: isPresent Is this better?

61 ADS2 Lecture 3

Inserting at the Tail Allocate a new node Insert new element Note, need to keep a record of the “last” (or “tail”) node. In Java do this via extra instance variable in list. See lecture 5. Allocate a new node Insert new element Have new node point to null Have old tail node point to new node Update tail to point to new node Exercise for you: Start off with an empty list and add the following Strings, in the order given. This time insert at the tail each time. Starting from the head read off the Strings. “how”, “know”, “you”, “when”, “easy”, “it’s” ADS2 Lecture 3