Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,

Slides:



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

Pointers.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Stacks, Queues, and Linked Lists
CS 367 – Introduction to Data Structures
Linear Lists – Linked List Representation
Linked Lists Linked Lists Representation Traversing a Linked List
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
CHP-5 LinkedList.
Linked List Variations
Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
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.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering 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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
CS2006- Data Structures I Chapter 5 Linked Lists III.
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
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Single Linked Lists Objectives In this lesson, you will learn to: *Define single linked list *Identify the following types of linked lists: Single linked.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists and Generics Written by J.J. Shepherd.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Data Structures
Lecture 6 of Computer Science II
Chapter 4 Linked Structures.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
UNIT – I Linked Lists.
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Doubly Linked List Review - We are writing this code
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Circularly Linked Lists
Chapter 18: 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.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Linked Lists in C and C++
Introduction to C++ Linear Linked Lists
Linked Lists.
Linked Lists.
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Lists List Implementations

2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes, connected by pointer links. A linked list is accessed by keeping a pointer to the first node of the list. This pointer to the first node of a list is typically named head. Subsequent nodes are accessed via a link pointer member that is stored in each node.”

3 OO Linked List In the OO world, the Linked List (LL) is a class with the public interface described last time. The pointer to the first node is a private data member of the LL class Other private data members may be defined if needed (tail, number of elements, etc)

4 Class Relationships The List class contains a pointer to the first Node The Node class contains a Data object as a data member – this is known as “composition” or “aggregation” –The List and Node cannot/should not/will not know anything about the Data. –The List and Node MUST use only the Data’s public interface

5 LL diagram next Data next Data next Data head Other Private Data members A singly linked list List ObjectNode Object Data Object

6 Inserting into a Linked List If the list is not sorted, why not always insert at the head? What’s the asymptotic performance of this kind of insert? If the list is sorted, search to find the proper place in the list, then insert. What’s the asymptotic performance of this insert?

7 next 42 next 6399 next 10 head Other Private Data members next 17 next 42 next 6399 next 10 head Other Private Data members Insert 17 at the head

8 next 42 next 6399 next 10 head Other Private Data members Insert 17 in sorted order next 42 next 6399 next 10 head Other Private Data members next 17

9 Finding an element Starting with the head pointer in the List, follow the “next” links until you find the element in question or get to the end of the list What is the worst case asymptotic performance of find( )? How is the performance different in a sorted linked list?

10 Pseudo-code for find( ) int find (Data d) { NodePtr p = list.head; while (p not NULL and it’s not the one you want) go to the next element If p not NULL, return success; If p is NULL, return failure }

11 removal in Linked List Find the node that contains the data element you want to remove. Make the node before the node that contains that element point to the node after the node that contains that element (NULL if the last element is being deleted), then free the memory for the node

12 Diagram for remove( ) next 42 next 6399 next 10 head Other Private Data members next 17 To remove( 17 ), make the node with 10 point to the node with 42, then free the memory for the node with 17.

13 Thinking about remove( ) A problem – we can’t use find( ) because we need to modify the node before the one that has the element we want to delete. Besides, find() doesn’t return a pointer to a node. Two solutions –The code in remove( ) keeps track of two pointers – one used to find the node to be removed and one to “trail behind” and point to the one before the one to be removed –Write a private method called “findPrevious( )” that is called from remove( ) that finds the node before the one with the specified element and returns a pointer to it

14 remove (cont’d) Problem – there may not be a node after the one being removed Solution – not a problem. The next pointer of the node before the one being removed will become NULL without any special code

15 remove( ) (cont’d) Another problem -- there is no node before the node being removed. This can only occur when we are removing the first node. Solution –Special code to handle this case

16 remove( ) (cont’d) What do we do if the element we are asked to delete is not in the list? What do we do if duplicates are allowed in the list?

17 remove( ) Pseudo-code remove (Data d) { if (removing first element) get a pointer to node being removed modify head pointer to point to 2 nd node else get a pointer to the node being removed get a pointer to the node before the one to be removed (special code or findPrevious() ) modify next pointer of this “previous” node free memory of node being removed }

18 Making remove() easier The code that makes remove( ) somewhat complex is the code for finding the node that comes before the node you want to remove. You either have an extra pointer or write a special (private) function that we’ve called findPrevious() An alternative is to build the necessary pointer into the linked list. This pointer (usually called “prev”) points to the node that comes before this node. Then we can write an alternative version of find( ) (let’s call it findIt() ) that returns a pointer to the node and use “prev” to access the previous node.

19 Double Linked List next 42 next 6399 next 10 head Other Private Data members next 17 prev Now when we want to remove( 17 ), we can call findIt( 17 ) and use the “prev” pointer as the one that points to the node before the one we want to remove. Care must be taken to perform the pointer modifications in the correct order.

20 Circular Linked List Some applications require a more sophisticated list. In this circular list, the “next” pointer of the last node points back to the first node instead of being NULL. This adds a little complication to find ( ). next 42 next 63 next 99 next 10 head Other Private Data members next 17

21 A Doubly Linked Circular List The most general kind of list is not only circular, but doubly linked. The “next” pointer of the last node points to the first node and the “prev” pointer of the first node points to the last node. next 42 next 63 next 99 next 10 head Other Private Data members next 17 prev