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.

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.
Singly linked lists Doubly linked lists
Lists CS 3358.
DATA STRUCTURES USING C++ Chapter 5
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.
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.
Data Structures & Algorithms
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.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
© 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
CSC 212 – Data Structures Lecture 13: Linked Lists.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Chapter 17 Linked List.
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.
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Linked Structures, LinkedStack
CS2006- Data Structures I Chapter 5 Linked Lists III.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,
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.
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,
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Data Structure & Algorithms
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Linked Lists and Generics Written by J.J. Shepherd.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Unit – I Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
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.
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,
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Lecture - 6 On Data Structures
Doubly Linked List Review - We are writing this code
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.
Doubly Linked Lists or Two-way Linked Lists
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
Linked Lists & Iterators
Problem Understanding
Presentation transcript:

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 Lists/Green Coding ADS2 Lecture 5

Sorting a singly linked list nullhead Two options (of some) - keep it sorted (insert in order) - we could use a bubble sort with 2 cursors

Singly linked lists with head and last/tail nodes 3 Makes insertion from end easier Sounds like a great idea … Baltimore head Rome Seattle Toronto  last ADS2 Lecture 5

Adding a new node at the “last/tail” 4 Add node containing the string “Zurich” to tail of list head Rome Seattle Toronto  last Zurich create new node containing (reference to) string “Zurich”, with next =null head Rome Seattle Toronto  last Zurich Redirect last.next to new node head Rome Seattle Toronto  last Zurich Reallocate last to new node ADS2 Lecture 5

Adding a new node at the “last/tail” Psuedo code snippet Node node = new Node(e,null) tail.setNext(node) tail = node size++

Singly linked lists with head and last/tail nodes 6 Makes insertion from end easier Sounds like a great idea … Baltimore head Rome Seattle Toronto  last ADS2 Lecture 5 But what happens if we delete the last node 

Doubly linked lists 7 Removing an element from the tail of a singly linked list is not easy Whether we have just a head, or a head and a last node, need to always traverse the whole list to remove the end. Why? In general it is hard to remove any node other than the head We don’t have a quick way of working out which is the node in front of the one we want to remove. For applications where we want quick access to the predecessor node of any node, we use a doubly linked list. A list in which we can go in both directions. ADS2 Lecture 5

8 Doubly Linked Lists (Goodrich § 3.3) A doubly linked list is a concrete data structure consisting of a sequence of nodes Each node stores –element –link to the next node –link to previous node ADS2 Lecture 5 next elem node prev AB C  first  last

9 ADS2 Lecture 5 DNode

10 ADS2 Lecture 5 DNode

11 ADS2 Lecture 5 DNode

12 ADS2 Lecture 5 DNode

13 ADS2 Lecture 5 DNode

14 ADS2 Lecture 5 DNode

15 ADS2 Lecture 5 DNode

16 ADS2 Lecture 5 DList

17 ADS2 Lecture 5 DList

18 ADS2 Lecture 5 DList

19 ADS2 Lecture 5 DList

20 ADS2 Lecture 5 DList

21 ADS2 Lecture 5 DList

22 ADS2 Lecture 5 DList

iteration refresher

Iteration

Declare loop variable and initialise

Iteration Continuing condition

Iteration Action at end of loop on each iteration

Iteration

Declare loop variable and initialise

Iteration Continuing condition

Iteration Action at end of loop on each iteration

Iteration

Declare loop variable and initialise

Iteration Continuing condition

Iteration Action at end of loop on each iteration

36 ADS2 Lecture 5 DList This is an example of really tight coding Could we make it tighter? (maybe remove found?) Any comments?

37 ADS2 Lecture 5 DList As tight as it gets?

end of iteration refresher

39 ADS2 Lecture 5 Insertion into the middle of a doubly linked list insert here node d node a node b node c node anode d node b node c make node d’s prev link point to node a make node d’s next link point to node b make node b’s prev link point to node d make node a’s next link point to node d

DList insert

NOTE: case analysis. This can make coding a bit less complicated

DList insert

A1B3 C2  first  A0

DList insert A1B3 C2  first  A0

DList insert A1B3 C2  first A0

DList insert A1B3 C2  first  A0 

DList insert A1B3 C2  first  A0 

DList insert

A1B3 C2  first  last D4

DList insert A1B3 C2 first  last D4

DList insert A1B3 C2 first  last D4

DList insert A1B3 C2 first  last D4 

DList insert A1B3 C2 first  last D4 

DList insert

Find insertion point

DList insert Insert new node

DList insert Insert new node

DList insert A1B3 C2  first  B0 x y z

DList insert A1B3 C2  first  B0 x y z

DList insert A1B3 C2  first  B0 x y z

DList insert A1B3 C2  first  B0 x y z

DList insert A1B3 C2  first  B0 x y z

DList insert Done!

Irrelevant fact … my 1 st passport was paid for by Burroughs Machines Corporation. It came with an indefinite US visa and had as my “Profession” … Passports no longer have “Profession”  Random Fact #1 Computer Programmer

Irrelevant fact … my 1 st passport was paid for by Burroughs Machines Corporation. It came with an indefinite US visa and had as my “Profession” … Passports no longer have “Profession”  Random Fact #1 Still Learning!!!!

Random Fact #2 What computers looked like when I was a young man working at Burroughs

The dark side of Burroughs (William S.) Random Fact #3

Random Fact #4

end of random facts #1 to #4

ADS2 Lecture 575 Removal from the middle of a doubly linked list node anode d node b node c remove this node make node a’s next link point to node d.next make node b’s prev link point to node d.prev node a node b node c

DList delete But again, do you see the case analysis?

DList delete

Private … only used here, not by user

DList delete

node A  head  last

DList delete garbage A  head  last

DList delete node AB C  first  last

DList delete node AB C  first  last

DList delete node AB C  first  last 

DList delete garbage AB C  first  last 

DList delete node AB C  first  last

DList delete node AB C  first  last

DList delete node AB C  first  last 

DList delete garbage AB C  first  last 

DList delete node AB C  first  last

DList delete node AB C  first  last

DList delete node AB C  first  last

DList delete garbage AB C  first  last

DList Test

Generic linked lists ADS2 Lecture 597 Rather than use a linked list that can only store objects of a certain type, can use a generic linked list (either generic singly linked list or generic doubly linked list). Need a generic node to implement the list

ADS2 Lecture 598 Java code for a node of a generic singly linked list: GList.java

ADS2 Lecture 599 Java code for a node of a generic singly linked list: GList.java

ADS2 Lecture 5100 Java code for a node of a generic singly linked list: GList.java

ADS2 Lecture 5101 Java code for a node of a generic singly linked list: GList.java

ADS2 Lecture 5102 Java code for a node of a generic singly linked list: GList.java

ADS2 Lecture 5103 Java code for a node of a generic singly linked list: GList.java

ADS2 Lecture 5106

Comparing: array, linked list, doubly linked list insert an item in order delete an item get the ith item

“green” coding? When we delete an element of the list it becomes garbage. Could we recycle deleted nodes?

ADS2 Lecture 5112