4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.

Slides:



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

Linked Lists Geletaw S..
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
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
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Topic 11 Linked Lists -Joel Spolsky
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
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.
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.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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.
Summary of lectures (1 to 11)
© 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
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Linked Structures, LinkedStack
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structure & Algorithms
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Linked List, Stacks Queues
Unit – I Lists.
CHAPTER 4: Linked Structures
Chapter 4 Linked Structures.
Storage Strategies: Dynamic Linking
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Topic 11 Linked Lists -Joel Spolsky
Arrays and Linked Lists
Linked List Intro CSCE 121 J. Michael Moore.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists 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.
Problem Understanding
Linked List Intro CSCE 121.
Topic 11 Linked Lists -Joel Spolsky
Problem Understanding
Presentation transcript:

4-1 Topic 6 Linked Data Structures

4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques for managing a linked list Discuss the need for a separate node class to form linked structures

4-3 Array Limitations What are the limitations of an array, as a data structure? Fixed size Physically stored in consecutive memory locations To insert or delete items, may need to shift data

4-4 Linked Data Structures A linked data structure consists of items that are linked to other items How? each item points to another item Singly linked list: each item points to the next item Doubly linked list: each item points to the next item and to the previous item

4-5 Conceptual Diagram of a Singly- Linked List front

4-6 Advantages of Linked Lists The items do not have to be stored in consecutive memory locations: the successor can be anywhere physically So, can insert and delete items without shifting data Can increase the size of the data structure easily Linked lists can grow dynamically (i.e. at run time) – the amount of memory space allocated can grow and shrink as needed

4-7 Nodes A linked list is an ordered sequence of items called nodes A node is the basic unit of representation in a linked list A node in a singly linked list consists of two fields: A data portion A link (pointer) to the next node in the structure The first item (node) in the linked list is accessed via a front or head pointer The linked list is defined by its head (this is its starting point)

4-8 Singly Linked List data. head these are nodes head pointer "defines" the linked list (note that it is not a node)

4-9 Linked List Note: we will hereafter refer to a singly linked list just as a “linked list” Traversing the linked list How is the first item accessed? The second? The last? What does the last item point to? We call this the null link

4-10 Discussion How do we get to an item’s successor? How do we get to an item’s predecessor? How do we access, say, the 3rd item in the linked list? How does this differ from an array?

4-11 Linked List Operations We will now examine linked list operations: Add an item to the linked list We have 3 situations to consider: insert a node at the front insert a node in the middle insert a node at the end Delete an item from the linked list We have 3 situations to consider: delete the node at the front delete an interior node delete the last node

4-12 Inserting a Node at the Front front node node points to the new node to be inserted, front points to the first node of the linked list front node 1.Make the new node point to the first node (i.e. the node that front points to)

4-13 front node 2. Make front point to the new node (i.e the node that node points to)

4-14 Inserting a Node in the Middle front node Let's insert the new node after the third node in the linked list front node 1. Locate the node preceding the insertion point, since it will have to be modified (make current point to it) current insertion point

4-15 front node 2. Make the new node point to the node after the insertion point (i.e. the node pointed to by the node that current points to) current front node 3. Make the node pointed to by current point to the new node current X

4-16 Discussion Inserting a node at the front is a special case; why? Is inserting a node at the end a special case?

4-17 Deleting the First Node front front points to the first node in the linked list, which points to the second node front Make front point to the second node (i.e. the node pointed to by the first node) X

4-18 Deleting an Interior Node front 1. Traverse the linked list so that current points to the node to be deleted and previous points to the node prior to the one to be deleted previouscurrent front 2. We need to get at the node following the one to be deleted (i.e. the node pointed to by the node that current points to) previouscurrent

4-19 front 3. Make the node that previous points to, point to the node following the one to be deleted previous current X

4-20 Discussion Deleting the node at the front is a special case; why? Is deleting the last node a special case?

4-21 References As Links Recall that in Java, a reference variable contains a reference or pointer to an object We can show a reference variable obj as pointing to an object: obj A linked structure uses references to link one object to another

4-22 Implementation of Linked List In Java, a linked list is a list of node objects, each of which consists of two references: A reference to the data object A reference to the next node object The head pointer is the reference to the linked list, i.e. to the first node object in the linked list The last node has the null value as its reference to the “next” node object

4-23 Linked List of Node Objects. head linked list object these are node objects these are the data objects

4-24 Node Objects For our linked list implementations, we will define a class called LinearNode to represent a node It will be defined for the generic type T Why is it a good idea to have separate node class? Note that it is called “LinearNode” to avoid confusion with a different class that will define nodes for non-linear structures later

4-25 The LinearNode Class Attributes (instance variables): element: a reference to the data object next : a reference to the next node so it will be of type LinearNode elementnext data object

4-26 The LinearNode Class Methods: we only need Getters Setters

4-27 public class LinearNode { private LinearNode next; private T element; public LinearNode( ){ next = null; element = null; } public LinearNode (T elem){ next = null; element = elem; } // cont’d.. LinearNode.java

4-28 public LinearNode getNext( ){ return next; } public void setNext (LinearNode node){ next = node; } public T getElement( ){ return element; } public void setElement (T elem) { element = elem; } LinearNode.java (cont’d)

4-29 Example: Create a LinearNode Object Example: create a node that contains the integer 7 Integer intObj = new Integer(7); LinearNode inode = new LinearNode (intObj); or LinearNode inode = new LinearNode (new Integer(7));

4-30 Exercise: Build a Linked List Exercise: create a linked list that contains the integers 1, 2, 3, …, 10

4-31 Doubly Linked Lists In a doubly linked list, each node has two links: A reference to the next node in the list A reference to the previous node in the list What is the “previous” reference of the first node in the list? What is the advantage of a doubly linked list? What is a disadvantage?

4-32 Doubly Linked List. head. tail