Linked Lists Dr. Tim Margush University of Akron © 2009.

Slides:



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

Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
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 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)
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.
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
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.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
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.
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.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 5 Linked Lists II
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
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.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
List Interface and Linked List Mrs. Furman March 25, 2010.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
1 CS162: Introduction to Computer Science II Abstract Data Types.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
An Array-Based Implementation of the ADT List
Linked List ADT used to store information in a list
Tirgul 12 Data Structures (2) 1.
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Linked Lists.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Header and Trailer Sentinels
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Presentation transcript:

Linked Lists Dr. Tim Margush University of Akron © 2009

Goals Describe a linked data structure Perform basic operations on a linked data structure using a Node class Implement the List ADT using a linked data structure Compare the advantages of linked to array-based implementations of a list © 2009 Dr. Tim Margush

Backing Store Every List implementation requires a backing store – A data structure containing elements of the List An array is one possible backing store – Arrays store data references physically contiguous memory locations A linked structure is another – References to data in linked structures are scattered throughout memory © 2009 Dr. Tim Margush

Array vs. Linked Structure References to List elements can be stored – In physical sequential order (array) – In random locations (linked) Must be able to locate the first and next items – list[0], list[1], … – head, current.next The first element is special Each element "knows" where the next is © 2009 Dr. Tim Margush

Links Links specify the location of something Links can be integers, or references – Object t; t is a link (reference) to an Object – int loc; loc is an array subscript used to locate an Object (or primitive value) © 2009 Dr. Tim Margush

Nodes Nodes support linked structures by bundling data with one or more links to other elements of the collection A linked list uses Nodes with a data value and one or more links (references) to other Nodes © 2009 Dr. Tim Margush class Node { private E data; private Node next; }

Inner Classes An inner class is defined inside another class A private inner class is only accessible by the outer (enclosing) class © 2009 Dr. Tim Margush public class LList implements ListInterface { private class Node{ private E data; private Node next; } private Node head;

Inner Classes Methods in both the inner and outer class have complete access to all members of each other, including private members © 2009 Dr. Tim Margush public class LList implements ListInterface { private class Node{ private Node(){ //access private outer member if (head == null) … } } //access private constructor private Node head = new Node();

Visualization of a Linked List © 2009 Dr. Tim Margush null Node head;

Inner Class: Node © 2009 Dr. Tim Margush public class LList implements ListInterface { private class Node{ private E data; private Node next; private Node(E newEntry){ this(newEntry, null); } private Node(E newEntry, Node n){ data = newEntry; next = n; } } } …

Linked List Implementation public class LList implements ListInterface { private Node head; private int size; //insert inner class for Node here public LList(){ clear(); } public final void clear(){ head = null; size = 0; } … } © 2009 Dr. Tim Margush

Linked List add public boolean add(int newPosition, E newEntry){ if (newPosition size) return false; Node prev = getNodeAt(newPosition-1); if (prev == null){ head = }else{ prev.next = } size++; return true; } © 2009 Dr. Tim Margush

getNodeAt, add private Node getNodeAt(int loc){ Node temp = head; if (loc =size) return null; while(loc-- > 1) temp = return temp; } public boolean add(E newEntry){ return } © 2009 Dr. Tim Margush

remove public E remove(int position){ if (position -size) return null; Node todelete, prev = getNodeAt(position-1); if (prev == null){ todelete = head; head = todelete.next; }else{ } size--; return todelete.data; } © 2009 Dr. Tim Margush

Array vs. Linked List Backing Store Array Wastes memory in unused positions Resizing requires moving many references Random access is efficient Insert in middle requires shifting Add to end is fast Add to beginning is costly Linked list Uses only the memory needed for the data actually in the list Resizing is never necessary Random access is inefficient Insert in middle is efficient after locating previous Add to end is costly Add to beginning is fast © 2009 Dr. Tim Margush

Variations on Node class Node should be accessible only to LList (or other collection-type clients) – Private inner class Add public get/set methods – Package access Access can be package or public Set/get methods are optional as free access is available inside package (unless declared private) © 2009 Dr. Tim Margush

Package Model © 2009 Dr. Tim Margush package ListPackage; //Everything in this class is package access Class Node { E data; Node next; Node(E newEntry){ this(newEntry, null); } Node(E newEntry, Node n){ data = newEntry; next = n; }

Package Model - add © 2009 Dr. Tim Margush package ListPackage; public class LList { Node head; int size; public boolean add(E newEntry){ if (head==null) head = new Node (newEntry); else{ //note package access requirement getNodeAt(size).next = new Node (newEntry); size++; return true; } … }

Package Model - getEntry © 2009 Dr. Tim Margush package ListPackage; public class LList { //illustrates package access to member public boolean getEntry(int position){ if (position>=0 && position<size) return getNodeAt(position).data; else return null; }

Circular Lists Head and Tail Access List keeps reference to last Node – Facilitates add to end since we always know where the last node is Last Node points to first node – Otherwise, how would we know where the first Node was located? This can also be done with a standard list maintaining a head and tail pointer © 2009 Dr. Tim Margush

Small Lists © 2009 Dr. Tim Margush 01 2 a b a

Circular Traverse © 2009 Dr. Tim Margush public void display(){ if (last==null) return; Node current = last; do{ current = current.next; System.out.println(current.data); while(current!=last); } 3 bac

getNodeAt © 2009 Dr. Tim Margush private Node getNodeAt(int position){ //node at 0 or size is the last node (or null if empty) if (position size) return last; Node current = last.next; while(position-- > 0) current = current.next; return current; } 3 bac

Circular Add Add to end is to add between the last and first – Insert between last and first last = last.next = new Node(x, last.next); If empty, last.next is illegal – New node is last and points to self last.next = last = new Node(x); Adding at the front is even easier – Assuming not empty last.next = new Node(x, last.next); © 2009 Dr. Tim Margush

Circular - add © 2009 Dr. Tim Margush public boolean add(E newEntry){ if (last==null) last.next = last = new Node(newEntry); else last = last.next = new Node(newEntry, last.next); size++; } public boolean add(int newLoc, E newEntry){ if (newLoc==size) //add to end case return add(newEntry); //adding somewhere other than at the end Node prev = getNodeAt(newLoc-1); prev.next = new Node(newEntry, prev.next); size++; }

Circular - remove © 2009 Dr. Tim Margush public E remove(int loc){ if (loc =size) return null; Node prev = getNodeAt(loc-1); Node toRemove = prev.next; prev.next = toRemove.next; if (loc==size-1) last = prev; size--; if (size==0) last = null; return toRemove.data; }

Dummy Header Node First Node always exists as a placeholder, but is not used to store data – Node head = new Node(null); Eliminates special case of no previous node – getNodeAt(-1) returns the header Node. © 2009 Dr. Tim Margush

Doubly-Linked Each Node has a forward and backward link Usually combined with circular list structure with dummy header © 2009 Dr. Tim Margush private Node { private E data; private Node prev, next; private Node(E newEntry){ data = newEntry; prev = next = this; //circular } private Node(E newEntry, Node p, Node n){ data = newEntry; prev = p; next = n; } }

Doubly-Linked - add © 2009 Dr. Tim Margush class DLCList implements ListInterface { private Node head = new Node(null); //size and private Node class here public boolean add(newEntry){ Node last = head.prev; last.next = head.prev = new Node(newEntry,last,head); size++; return true; } public boolean add(int newLoc, E newEntry){ Node prev = getNodeAt(newLoc-1); Node after = prev.next; prev.next = after.prev = new Node(newEntry,prev,after); size++; return true; }

Doubly-Linked - remove No special cases! © 2009 Dr. Tim Margush public E remove(int loc){ Node toRemove = getNodeAt(loc); toRemove.prev.next = toRemove.next; toRemove.next.prev = toRemove.prev; size--; return toRemove.data; }

Doubly-Linked - getNodeAt Efficiently scan for location © 2009 Dr. Tim Margush private Node getNodeAt(int loc){ if(loc =size) return null; Node current = head; //location 0 or size+1 if(loc<size/2){ //scan forward int pos=0; while(pos++ < loc) current = current.next; }else{ //scan in reverse int pos = size+1; do current = current.prev; while(--pos > loc); } return current; }

Linked List Doubly-Linked List – Provides for forward or backward iteration – No linked list supports random access getNodeAt is a non-trivial method Dummy header and circularity – Eliminates special cases and all null references at the expense of one extra Node © 2009 Dr. Tim Margush

java.util.LinkedList Implements List, Cloneable, Serializable Uses a doubly-linked list backing store Includes special methods that take advantage of linked list structure – addFirst, addLast – getFirst, getLast – removeFirst, removeLast © 2009 Dr. Tim Margush