Link-Based Implementations

Slides:



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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures: A Pseudocode Approach with C
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Data Structures Using C++ 2E
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
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.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
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
Chapter 16: Linked Lists.
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Copy Constructor / Destructors Stacks and Queues
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 Linked Lists.
C++ Object-Oriented Programming
Class: Special Topics Copy Constructors Static members Friends this
Bag Implementations that Use Arrays
A Bag Implementation that Links Data
Chapter 4 Linked Lists
The Bag and Sequence Classes with Linked Lists
A Bag Implementation that Links Data
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Chapter 12 Sorted Lists and their Implementations
Chapter 3 Array-Based Implementations
Queue and Priority Queue Implementations
List Implementations Chapter 9
List Implementations Chapter 9.
Array-Based Implementations
Doubly Linked List Implementation
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Array-Based Implementations
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Class: Special Topics 2 For classes using memory allocation
Bag Implementations that Use Arrays
Doubly Linked List Implementation
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
A List Implementation That Links Data
A List Implementation that Links Data
Presentation transcript:

Link-Based Implementations Chapter 4

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

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

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.

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.

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

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

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

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

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

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

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

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

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

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

Defining the Core Methods Definition of toVector

Defining the Core Methods Methods isEmpty and getCurrentSize

Implementing More Methods Method getFrequencyOf

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

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

Implementing More Methods Method remove also calls getPointerTo

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

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

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

Implementing More Methods Copy constructor to accomplish deep copy.

Implementing More Methods Copy constructor to accomplish deep copy.

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

Recursive Definitions Methods in LinkedBag- skip Method toVector

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

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

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

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

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

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

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

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

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

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

End Chapter 4