Presentation is loading. Please wait.

Presentation is loading. Please wait.

Link-Based Implementations

Similar presentations


Presentation on theme: "Link-Based Implementations"— Presentation transcript:

1 Link-Based Implementations
Chapter 4

2 Figure 4-1 A freight train
Preliminaries Another way to organize data items Place them within objects—usually called nodes (requires the use of pointers) Linked together into a “chain,” one after the other Figure 4-1 A freight train

3 FIGURE 4-3 Several nodes linked together
Preliminaries FIGURE 4-2 A node The Node ADT is a template class. The template parameter specifies the type of data stored in the item field. The next field is a pointer to a Node. View struct Node example. FIGURE 4-3 Several nodes linked together

4 Preliminaries FIGURE 4-4 A head pointer to the first of several linked nodes – the head pointer keeps track of the front of list, giving sequential access to the other nodes in the list.

5 FIGURE 4-5 A lost node giving rise to a memory leak.
Preliminaries FIGURE 4-5 A lost node giving rise to a memory leak.

6 The Class Node LISTING 4-1 The header file for the template class Node

7 The Class Node LISTING 4-2 The implementation file for the class Node

8 The Class Node LISTING 4-2 The implementation file for the class Node

9 A Link-Based Implementation of the ADT Bag
FIGURE 4-6 A link-based implementation of the ADT bag Bag operations, given in UML notation

10 The Header File LISTING 4-3 The header file for the class LinkedBag

11 The Header File LISTING 4-3 The header file for the class LinkedBag
Error-~LinkedBag

12 Defining the Core Methods
Default Constructor Inserting at the beginning of a linked chain

13 Defining the Core Methods
FIGURE 4-7 Inserting at the beginning of a linked chain

14 Defining the Core Methods
Traverse operation (performed in toVector) visits each node in the linked chain Must move from node to node High-level pseudocode for this loop

15 Defining the Core Methods
FIGURE 4-8 The effect of the assignment curPtr = curPtr->getNext( )

16 Defining the Core Methods
Definition of toVector

17 Defining the Core Methods
Methods isEmpty and getCurrentSize

18 Implementing More Methods
Method getFrequencyOf

19 Implementing More Methods
Search for a specific entry. To avoid duplicate code, we perform this search in a private method

20 Implementing More Methods
Note: definition of the method contains calls getPointerTo

21 Implementing More Methods
Method remove also calls getPointerTo

22 Implementing More Methods
Method clear deallocates all nodes in the chain.

23 Implementing More Methods
Destructor calls clear, destroys instance of a class

24 Implementing More Methods
FIGURE 4-9 (a, b) A linked chain and its shallow copy; (a, c) a linked chain and its deep copy

25 Implementing More Methods
Copy constructor to accomplish deep copy.

26 Implementing More Methods
Copy constructor to accomplish deep copy.

27 Recursive Definitions Methods in LinkedBag - skip
Revise methods in class to use recursion Traverse chain of linked nodes Make no changes to the chain Method toVector Has a straightforward recursive implementation Must be a private method Receives head pointer as parameter Vector must also be a parameter

28 Recursive Definitions Methods in LinkedBag- skip
Method toVector

29 Recursive Definitions Methods in LinkedBag- skip
Private method getPointerTo Locates given entry within linked chain Traversal stops if it locates node that contains given entry

30 Testing Multiple ADT Implementations
Recall test program of Listing 3-2 Used ADT bag methods when we tested our implementation Can use the same code—with a few changes Change each occurrence of ArrayBag to LinkedBag and recompile the program

31 Testing Multiple ADT Implementations
LISTING 4-4 A program that tests the core methods of classes that are derived from the abstract class BagInterface

32 Testing Multiple ADT Implementations
LISTING 4-4 A program that tests the core methods of classes that are derived from the abstract class BagInterface

33 Testing Multiple ADT Implementations
LISTING 4-4 A program that tests the core methods of classes that are derived from the abstract class BagInterface

34 Testing Multiple ADT Implementations
LISTING 4-4 A program that tests the core methods of classes that are derived from the abstract class BagInterface

35 Testing Multiple ADT Implementations
LISTING 4-4 A program that tests the core methods of classes that are derived from the abstract class BagInterface

36 Testing Multiple ADT Implementations
LISTING 4-4 A program that tests the core methods of classes that are derived from the abstract class BagInterface

37 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 Increasing size of dynamically allocated array can waste storage and time Can access array items directly with equal access time An array-based implementation is a good choice for a small bag

38 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 Must traverse a linked chain to access its ith node Time to access ith node in a linked chain depends on i

39 End Chapter 4


Download ppt "Link-Based Implementations"

Similar presentations


Ads by Google