Download presentation
Presentation is loading. Please wait.
Published byKelly Walsh Modified over 9 years ago
1
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 1 Chapter 18 List ADT Animated Version
2
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 2 Chapter 18 Objectives After you have read and studied this chapter, you should be able to –Describe the key features of the List ADT –Implement the List ADT using an array and linked list –Describe and implement the iterator pattern –Explain the key differences between the array and linked implementations of the List ADT
3
The List ADT An abstract data type (ADT) is a mathematical specification of a set of data and the corresponding set of operations performed on those data –ADT does not specify how the set of data is represented or how the set of operations is implemented (that’s why it’s abstract) In this chapter, we study the List ADT. A list is an linearly ordered collection of elements with no duplicates. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 3
4
A Sample List ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 4
5
The add Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 5
6
The clear Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 6
7
The contains Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 7
8
The get Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 8
9
The indexOf Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 9
10
The isEmpty Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 10
11
The remove Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 11
12
The set Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 12
13
The size Operation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 13
14
The List Interface: NPSList ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 14 package edu.nps.util; interface NPSList { public void add(E item); public void add(int index, E item) throws IndexOutOfBoundsException; public void clear( ); public boolean contains(E item); public E get(int index) throws IndexOutOfBoundsException; public int indexOf(E item); public boolean isEmpty( ); public E remove(int index)throws IndexOutOfBoundsException; public boolean remove(E item); public E set(int index, E item) throws IndexOutOfBoundsException; public int size( ); }
15
The Array Implementation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 15
16
The Array Implementation: NPSArrayList The NPSArrayList class is the array implementation of the NPSList interface Two data members: an array of elements E and the size to keep track of the number of elements in the array The default initial size of the element array is set to DEFAULT_SIZE. A new array that is 1.5 times larger than the current array is created when there’s an overflow Please refer to the source code for full details ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 16
17
The Linked-List Implementation ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 17
18
Linked List: Add ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 18
19
Linked List: Add to the End Chapter 18 - 19
20
Linked List: Add at Position 0 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 20
21
Linked List: Add at Position I (Before) ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 21
22
Linked List: Add at Position I (After) ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 22
23
Linked List: Remove (Before) ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 23
24
Linked List: Remove (After) ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 24
25
NPSLinkedList The NPSLinkedList class is the linked-list implementation of the NPSList interface Three data members: head and tail pointers and the size to keep track of the number of elements in the array The inner class LinkNode is used to represent a single node in the linked list Please refer to the source code for full details ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 25
26
Using the Head Node ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 26
27
Using the Head Node - 2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 27
28
NPSLinkedListWithHeader The NPSLinkedListWithHeader class is the linked-list implementation of the NPSList interface with a header node Three data members: head and tail pointers and the size to keep track of the number of elements in the array The inner class LinkNode is used to represent a single node in the linked list Please refer to the source code for full details ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 28
29
The Iterator Design Pattern One of the most common operations we perform on a data structure is a traversal (also called scanning). An iterator is a design pattern that allows a consistent way of accessing elements, regardless of the underlying data structure. When we implement a data structure, we can support the iterator so the users of our data structure can access the elements in a consistent way. See the final version of the NPSLinkedList and the NPSArrayList classes. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 - 29
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.