CS Data Structures Chapter 8 Lists Mehmet H Gunes

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
CHP-5 LinkedList.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 ADT Sorted List.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
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.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
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.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 4 Link Based Implementations
Link Based Implementations
Link-Based Implementations
Chapter 16: Linked Lists.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
C++ Programming:. Program Design Including
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Pointers, Polymorphism, and Memory Allocation
Chapter 4 Linked Lists.
Processing Data in External Storage
A Bag Implementation that Links Data
Chapter 16 Tree Implementations
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 4 Linked Lists
Chapter 13 Queues and Priority Queues
This pointer, Dynamic memory allocation, Constructors and Destructor
Top Ten Words that Almost Rhyme with “Peas”
Array Lists Chapter 6 Section 6.1 to 6.3
C++ Plus Data Structures
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Chapter 12 Sorted Lists and their Implementations
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 3 Array-Based Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 14: Queue and Priority Queue Implementations
List Implementations Chapter 9
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
List Implementations Chapter 9.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Chapter 16 Tree Implementations
A List Implementation That Links Data
Sorted Lists and Their Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 7 Implementations of the ADT Stack
Lists Chapter 8.
Linked Lists Chapter 5 (continued)
Queues and Priority Queue Implementations
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
A List Implementation that Links Data
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

CS 302 - Data Structures Chapter 8 Lists Mehmet H Gunes Modified from authors’ slides

Specifying the ADT List Things you make lists of Chores Addresses Groceries Lists contain items of the same type Operations Count items Add, remove items Retrieve © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Specifying the ADT List A grocery list © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

ADT List Operations Test whether a list is empty. Get number of entries on a list. Insert entry at given position on list. Remove entry at given position from list. Remove all entries from list. Look at (get) entry at given position on list. Replace (set) entry at given position on list. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Specifying the ADT List UML diagram for the ADT list © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Specifying the ADT List Definition: ADT List Finite number of objects Not necessarily distinct Same data type Ordered by position as determined by client © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Axioms for ADT List © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Interface Template for ADT List A C++ interface for lists © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Interface Template for ADT List A C++ interface for lists © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Interface Template for ADT List A C++ interface for lists © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Interface Template for ADT List A C++ interface for lists © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Using the List Operations Displaying the items on a list. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Using the List Operations Replacing an item. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Sorted and Unsorted Lists Elements are placed into the list in no particular order SORTED LIST List elements are in an order that is sorted in some way either numerically, alphabetically by the elements themselves, or by a component of the element called a KEY member

Complexities the order of the operation that determines if an item is in a list in a sorted, array-based implementation a list in an unsorted, array-based implementation a list in a sorted, linked implementation a list in an unsorted, linked implementation O(log N) O(N) O(N) O(N)

Allocation of memory STATIC ALLOCATION Static allocation is the allocation of memory space at compile time DYNAMIC ALLOCATION Dynamic allocation is the allocation of memory space at run time by using operator new

3 Kinds of Program Data STATIC DATA: memory allocation exists throughout execution of program static long SeedValue; AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete

Why is a destructor needed? When a local list variable goes out of scope, the memory space for data member pointer is deallocated But the nodes to which pointer points are not deallocated A class destructor is used to deallocate the dynamic memory pointed to by the data member 18

Chapter 9 List Implementations

Array-Based Implementation of the ADT List List operations in their UML form © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Array-Based Implementation of the ADT List Array-based implementation is a natural choice Both an array and a list identify their items by number However ADT list has operations such as getLength that an array does not Must keep track of number of entries © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File An array-based implementation of the ADT list © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File The header file for the class ArrayList © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File The header file for the class ArrayList © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File The header file for the class ArrayList © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Constructor, methods isEmpty and getLength © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method getEntry © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method insert © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Shifting items for insertion © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method getEntry © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method replace © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method remove © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Shifting items to remove an entry © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method clear © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT List We can use C++ pointers instead of an array to implement ADT list Link-based implementation does not shift items during insertion and removal operations We need to represent items in the list and its length © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Link-Based Implementation of the ADT List A link-based implementation of the ADT list © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File The header file for the class LinkedList © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File The header file for the class LinkedList © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Header File The header file for the class LinkedList © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Constructor © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method getEntry © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method getNodeAt © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Insertion process requires three high-level steps: Create a new node and store the new data in it. Determine the point of insertion. Connect the new node to the linked chain by changing pointers. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method insert © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method insert © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Inserting a new node between existing nodes of a linked chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Inserting a new node between existing nodes of a linked chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Inserting a new node between existing nodes of a linked chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Inserting a new node at the end of a chain of linked nodes © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Removing a node from a chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Removing the last node © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method remove © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method remove © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation File Method clear and the destructor © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Using Recursion in LinkedList Methods Possible to process a linked chain by Processing its first node and Then the rest of the chain recursively Logic used to add a node © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Using Recursion in LinkedList Methods Recursively adding a node at the beginning of a chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Using Recursion in LinkedList Methods Recursively adding a node between existing nodes in a chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Using Recursion in LinkedList Methods Recursively adding a node between existing nodes in a chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Using Recursion in LinkedList Methods Recursively adding a node between existing nodes in a chain © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Comparing Implementations Time to access the ith node in a chain of linked nodes depends on i You can access array items directly with equal access time Insertions and removals with link-based implementation Do not require shifting data Do require a traversal © 2017 Pearson Education, Hoboken, NJ.  All rights reserved