Download presentation
Presentation is loading. Please wait.
1
Chapter 4 Link Based Implementations
CS Data Structures Mehmet H Gunes Modified from authors’ slides
2
Figure 4-1 A freight train
Preliminaries Another way to organize data items Place them within objects—usually called nodes Linked together into a “chain,” one after the other Figure 4-1 A freight train Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
3
Preliminaries Components that can be linked A node
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
4
Preliminaries Several nodes linked together
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
5
Preliminaries A head pointer to the first of several linked nodes
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
6
Preliminaries A lost node
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
7
The Class Node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
8
The Class Node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
9
The Class Node Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
10
Link-Based Implementation of ADT Bag
A link-based implementation of the ADT bag Bag operations, given in UML notation Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
11
The header file for the class LinkedBag
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
12
The header file for the class LinkedBag
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
13
Defining the Core Methods
Default Constructor Inserting at the beginning of a linked chain Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
14
Link-Based Implementation of ADT Bag
Inserting at the beginning of a linked chain Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
15
Defining the Core Methods
Traverse operation visits each node in linked chain Must move from node to node High-level pseudocode for this loop Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
16
Link-Based Implementation of ADT Bag
The effect of the assignment curPtr = curPtr->getNext() Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
17
Defining the Core Methods
Definition of toVector Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
18
Defining the Core Methods
Methods isEmpty and getCurrentSize Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
19
Implementing More Methods
Method getFrequencyOf Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
20
Implementing More Methods
Search for a specific entry. To avoid duplicate code, we perform this search in a private method Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
21
Implementing More Methods
Note: definition of the method contains calls getPointerTo Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
22
Implementing More Methods
Method remove also calls getPointerTo Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
23
Implementing More Methods
Method clear deallocates all nodes in the chain Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
24
Implementing More Methods
Destructor calls clear, destroys instance of a class Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
25
Link-Based Implementation of ADT Bag
(a) A linked chain and its shallow copy; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
26
Testing Multiple ADT Implementations
(b) a linked chain and its deep copy Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
27
Implementing More Methods
Copy constructor to accomplish deep copy. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
28
Implementing More Methods
Copy constructor to accomplish deep copy. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
29
Testing Multiple ADT implementations
Used ADT bag methods when we tested our implementation test program of Listing 3-2 Can use the same code—with a few changes Change each occurrence of ArrayBag to LinkedBag and recompile the program Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
30
Testing Multiple ADT Implementations
A program that tests the core methods of classes that are derived from the abstract class BagInterface Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
31
Testing Multiple ADT Implementations
A program that tests the core methods of classes that are derived from the abstract class BagInterface Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
32
Testing Multiple ADT Implementations
A program that tests the core methods of classes that are derived from the abstract class BagInterface Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
33
Testing Multiple ADT Implementations
A program that tests the core methods of classes that are derived from the abstract class BagInterface Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
34
Testing Multiple ADT Implementations
Sample output 1 of test program Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
35
Testing Multiple ADT Implementations
Sample output 2 of test program Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
36
Comparing Array-Based and Link-Based Implementations
Arrays easy to use, but have fixed size Not always easy to predict number of items in ADT Array could waste space Can be dynamically resized Increasing size of dynamically allocated array can waste storage and time Can access array items directly with equal access time Dynamic arrays are better when direct access of items is frequent Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
37
Comparing Array-Based and Link-Based Implementations
Linked chains do not have fixed size In a chain of linked nodes, an item points explicitly to the next item Link-based implementation requires more memory Array items accessed directly, equal access time Must traverse linked chain for ith item access time varies Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2017
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
Insert Item 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. 49
42
The Inchworm Effect 50
43
Linking the New Node into the List
44
Deleting from a Doubly Linked List
What are the advantages of a circular doubly linked list?
45
A linked list in static storage ?
A Linked List as an Array of Records
46
A Sorted list Stored in an Array of Nodes
47
An Array with Linked List of Values and Free Space
48
An Array with Three Lists (Including the Free List)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.