Download presentation
Published byDonna Anderson Modified over 9 years ago
1
CS 302 - Data Structures Chapter 8 Lists Mehmet H Gunes
Modified from authors’ slides
2
Contents Specifying the ADT List Using the List Operations
An Interface Template for the ADT List Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
3
Specifying the ADT List
A grocery list You reference list items by their position Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
4
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. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
5
ADT List Operations UML diagram for the ADT list
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
6
Abstract Data Type: LIST
Data : A finite number of objects Not necessarily distinct Having the same data type Ordered by their positions determined by client. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
7
Abstract Data Type: LIST
Operations isEmpty() getLength() insert(newPosition, newEntry) remove(position) clear() getEntry(position) setEntry(position, newEntry) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
8
Axioms for the ADT List (new List()).isEmpty() = true
(new List()).getLength() = 0 aList.getLength() = (aList.insert(i, x)). getLength() - 1 aList.getLength() = (aList.remove(i)). getLength() + 1 (aList.insert(i, item)).isEmpty() = false (new List()).remove(i) = false (aList.insert(i, x)).remove(i) = aList Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
9
Axioms for the ADT List (new List()).getEntry(i) = error
(aList.insert(i, x)).getEntry(i) = x aList.getEntry(i) = (aList.insert(i, x)). getEntry(i + 1) aList.getEntry(i + 1) = (aList.remove(i)). getEntry(i) (new List()).setEntry(i, x) = error (aList.setEntry(i, x)).getEntry(i) = x Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
10
Using the List Operations
Displaying the items on a list independent of the implementation Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
11
Using the List Operations
Replacing an item. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
12
Using the List Operations
Pseudocode statements place names in an alphabetical list View Interface Template for the ADT List, Listing 8-1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
13
List Implementations Chapter 9
14
Contents An Array-Based Implementation of the ADT List
A Link-Based Implementation of the ADT List Comparing Implementations Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
15
Array-Based Implementation of ADT List
Recall list operations in UML form Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
16
An array-based implementation of the ADT list
The Header File View header file for the class ArrayList, Listing 9-1 An array-based implementation of the ADT list Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
17
The Implementation File
Constructor Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
18
The Implementation File
isEmpty tests whether itemCount is zero getLength simply returns the value of itemCount : Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
19
The Implementation File
Definition of the method insert Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
20
The Implementation File
Shifting items for insertion: (a) the list before the insertion; (b) copy items to produce room at position 3; (c) the result Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
21
The Implementation File
The method getEntry. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
22
The Implementation File
Testing core group of methods Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
23
The Implementation File
The method setEntry Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
24
The Implementation File
The definition of remove Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
25
The Implementation File
(a) Deletion can cause a gap; (b) shift items to prevent a gap at position 3; (c) the result Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
26
The Implementation File
The method clear. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
27
A Link-Based Implementation of the ADT List
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
28
C++ Data Structures Nell Dale
29
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
30
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)
31
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. 31
32
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 32
33
Dynamic Array Allocation
char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run time, allocates // memory for 5 characters and places into // the contents of ptr their beginning address 6000 6000 ptr 33
34
Dynamic Array Allocation
char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; // a pointer can be subscripted std::cout << ptr[ 2] ; 6000 ‘u’ 6000 ‘B’ ‘y’ ‘e’ ‘\0’ ptr 34
35
InsertItem algorithm for Sorted Linked List
Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location. Obtain a node for insertion and place item in it. Insert the node by adjusting pointers. Increment length. 35
36
The Inchworm Effect 36
37
Why is a destructor needed?
When a local list variable goes out of scope, the memory space for data member listPtr is deallocated. But the nodes to which listPtr points are not deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member. 37 37
38
What is a Circular Linked List?
A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.
39
External Pointer to the Last Node
40
What is a Doubly Linked List?
A doubly linked list is a list in which each node is linked to both its successor and its predecessor.
41
Linking the New Node into the List
42
Deleting from a Doubly Linked List
What are the advantages of a circular doubly linked list?
43
What are Header and Trailer Nodes?
A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key. A Trailer Node is a node at the end of a list that contains a key larger than any possible key. Both header and trailer are placeholding nodes used to simplify list processing.
44
A linked list in static storage ?
A Linked List as an Array of Records
45
A Sorted list Stored in an Array of Nodes
46
An Array with Linked List of Values and Free Space
47
An Array with Three Lists (Including the Free List)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.