Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.

Slides:



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

Linked Lists.
Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
CHP-5 LinkedList.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Linked Structures, LinkedStack
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structure & Algorithms
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Advanced Programming 7/10/20161 Ananda Gunawardena.
Chapter 16: Linked Lists.
STACKS & QUEUES for CLASS XII ( C++).
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Chapter 5: The List Abstract Data Type
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
CSCI-255 LinkedList.
Linked Lists Chapter 5 (continued)
Review Deleting an Element from a Linked List Deletion involves:
Storage Strategies: Dynamic Linking
Lists CS 3358.
Fundamentals of Programming II Working with Arrays
Chapter 6: The Stack Abstract Data Type
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
LINKED LISTS CSCD Linked Lists.
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Arrays and Linked Lists
Chapter 18: Linked Lists.
Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
CS212D: Data Structures Week 5-6 Linked List.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Problem Understanding
Chapter 17: Linked Lists.
Lecture 14 Linked Lists CSE /26/2018.
Linked Lists Chapter 5 (continued)
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists Chapter 5 (continued)
Topic 11 Linked Lists -Joel Spolsky
CMPT 225 Lecture 5 – linked list.
Problem Understanding
Presentation transcript:

Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray

Introduction What will you use as the backing store for your collection types? Array and linked structures have different characteristics It is important to know these characteristics as they affect the performance of the collection using them

The Array Structure Static array Dynamic array size determined at compile time this is limiting Dynamic array Size determine at run time An object in Java with its own properties Can be resized (larger or smaller)

Array Characteristics An array is a homogeneous data structure – all elements must be of the same type: this mean each cell will be the same size The elements of an array are in adjacent memory locations Because each cell has the same size and the cells are adjacent in memory, it is possible to compute the address of an array cell; so an array access “costs” a computation followed by a memory access  an array is a random (direct) access data structure

Array Operations Traversing – can be bidirectional Resizing – can make a new array that is smaller or larger than the original array – a θ(size of new array) operation Replacing an element – an O(1) operation Inserting an element – an O(n) operation due to data movement Deleting an element – O(1) or O(n) depending on implementation

Array Versus Linked Structures

Linked Structure Characteristics Nodes in a linked structure are allocated individually, so the number of nodes in the linked structure can grow and shrink as needed The nodes of a linked structure are not necessarily adjacent in memory Because the nodes of a linked structure are not necessarily adjacent in memory, the computer cannot calculate the address of a node as it could for a cell in an array, so each node must store the location of node in the structure – this reference is the link  a linked structure is a sequential access data structure

SLNode<E> The self-referential 1 package sgray.ch3; 2 3 /** 4 * The structure of a node in the singly linked list 5 */ 6 public class SLNode<E> { 7 private E element; // the data field 8 private SLNode<E> successor; // link to successor 9 10 /** 11 * Create an empty <tt>SLNode</tt> object. 12 */ 13 public SLNode() { 14 this.element = null; 15 this.successor = null; 16 } 17 18 /** 19 * Create an <tt>SLNode</tt> that stores <tt>theElement</tt> and 20 * whose successor is <tt>theSuccessor</tt>. 21 * @param theElement the element to store in this node 22 * @param theSuccessor this node’s successor 23 */ 24 public SLNode( E theElement, SLNode<E> theSuccessor ) { 25 this.element = theElement; 26 this.successor = theSuccessor; 27 } The self-referential (recursive) part of the definition

SLNode Construction SLNode<Circle> node2 = new SLNode<Circle>(); SLNode<Circle> node1 = new SLNode<Circle>( circle1, node2 );

Accessing the successor temp = node1.getSuccessor(); SLNode<Circle> temp = null;

A Singly-linked List using SLNode public class SinglyLinkedList<E> { SLNode<E> head; // access point to the linked list SLNode<E> tail; int length; // # data nodes in the linked list Issue: how to represent an empty list This has implications for coding several of the support methods Options: a null head and tail means the list is empty use “dummy nodes” for the head and tail

Option 1: a null head/tail Means Empty Indicates “no more elements” Using head and tail as references to the first and last nodes of a linked structure

Option 2: Use dummy nodes Indicates “no more elements” Using dummy nodes for the head and tail of the linked structure. The dummy nodes are shaded.

Inserting an Element at the List head indicates pseudocode public void addAtHead(E theElement) { 1. Create the new SLNode SLNode<E> newnode = new SLNode<E>(theElement, null); 2. Set the new node’s next field to be the same as head’s next field newnode.setSuccessor(head.getSuccessor()); 3. Set head’s next field to reference the new node head.setSuccessor(newnode); 4. Increment length by 1 length++; }

Adding at the head: Step 1 (empty list) 1. Create the new SLNode SLNode<E> newnode = new SLNode<E>( theElement, null );

Adding at the head: Step 2 (empty list) 2. Set the new node’s next field to be the same as head’s next field newnode.setSuccessor( head.getSuccessor() );

Adding at the head: Step 3 (empty list) 3. Set head’s next field to reference the new node head.setSuccessor( newnode );

Adding at the head: Step 1 (!empty list) 1. Create the new SLNode SLNode<E> newnode = new SLNode<E>( theElement, null );

Adding at the head: Step 2 (!empty list) 2. Set the new node’s next field to be the same as head’s next field newnode.setSuccessor( head.getSuccessor() );

Adding at the head: Step 3 (!empty list) Does the order of the operations matter? 3. Set head’s next field to reference the new node head.setSuccessor( newnode );

Deleting a Node: (pseudo)code 1 public E remove ( int p ) { Verify that p is within the linked list bounds 2 if (( p < 0) || ( p >= this.length ) ) 3 throw new IndexOutOfBoundsException(); Move cursor to node in position p – 1 4 SLNode<E> cursor = head; // good for p == 0 5 if ( p > 0 ) cursor = find( p - 1 ); Let target = cursor's successor; the node to remove 6 SLNode<E> target = cursor.getSuccessor(); Link cursor to target's successor 7 cursor.setSuccessor( target.getSuccessor() ); Get the target’s element 8 E element = target.getElement(); Unlink target from the linked list for garbage collection 9 target.setSuccessor( null ); 10 target.setElement( null ); Decrement length 11 length--; Return the element stored in the target node 12 return element; 13 }

Deleting a Node: get in position Move cursor to node in position p – 1 SLNode<E> cursor = find( p – 1 ); // for p = 1

Deleting a Node: update cursor Let target = cursor's successor; the node to remove target = cursor.getSuccessor()

Deleting a Node: link cursor to successor Link cursor to target's successor cursor.setSuccessor( target.getSuccessor() )

Deleting a Node: Cleanup Unlink target from the linked list for garbage collection target.setSuccessor( null ); target.setElement( null );

A Doubly-Linked List The singly-linked list is unidirectional At the expense of an additional link (and the consequent code complexity) we can have a bidirectional list We need to add a second “link” attribute to the node definition

Let DLNode extend SLNode 1 package gray.adts.ch3; 2 3 /** 4 * The structure of a node in the doubly linked 5 * list. 6 */ 7 public class DLNode<E> extends SLNode<E> { 8 private DLNode<E> predecessor; 9 10 /** 11 * Constructor. Create an empty <tt>DLNode</tt> object. 12 */ 13 public DLNode() { 14 super(); 15 this.predecessor = null; 16 } 17 . . . // rest of the definition inherit element and successor fields from SLNode

Circular Linked List

Time Complexity for Array Operations Time Complexity for the Array Data Structure (n = # of occupied cells) Operation Cost read (anywhere in the array) (1) add/remove (at the logical end of the array) add/remove (in the interior of the array) (n) resize (length of new array) find by position find by target

Time Complexity for Linked Operations Time Complexity for the Linked Data Structure Operation Cost Singly Linked Doubly Linked read (anywhere in the linked list) (n) add/remove (at the head) (1) add/remove (at the tail) add/remove (in the interior of the structure) resize N/A find by position find by target