Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CMPT 225 Abstract Data Types.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Abstract Data Types. Typical operations on data  Add data to a data collection  Remove data from a data collection  Ask questions about the data in.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved 4-1 Chapter 4 Data Abstraction: The Walls.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
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 Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 3 Data.
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.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Modularity Keeps the complexity of a large program manageable by systematically controlling the interaction of its components Isolates errors Eliminates.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
April 24, 2017 Chapter 4 Data Abstraction The Walls
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.
1 Object-Oriented Programming Using C++ CLASS 6. 2 Specifying ADTs: The ADT List Except for the first and last items in a list, each item has a unique.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Data Abstraction: The Walls
Chapter 7 Queues.
CC 215 Data Structures Queue ADT
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Data Abstraction: The Walls
CC 215 Data Structures Stack ADT
Homework 4 questions???.
Chapter 4 Linked Lists
Data Abstraction A technique for increasing the modularity of a program The need to support several operations on the data arise need to define abstract.
Linked Lists Chapter 4.
Queues.
Chapter 4 Linked Lists.
List Implementations Chapter 9.
Chapter 4 Linked Lists.
Data Abstraction: The Walls
Chapter 16 Linked Structures
Data Abstraction: The Walls
The List Container and Iterators
Presentation transcript:

Chapter 3 Data Abstraction: The Walls

© 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data to a data collection –Remove data from a data collection –Ask questions about the data in a data collection

© 2005 Pearson Addison-Wesley. All rights reserved3-3 Abstract Data Types Data abstraction –Asks you to think what you can do to a collection of data independently of how you do it –Allows you to develop each data structure in relative isolation from the rest of the solution

© 2005 Pearson Addison-Wesley. All rights reserved3-4 Abstract Data Types Abstract data type (ADT) –An ADT is composed of A collection of data A set of operations on that data –Specifications of an ADT indicate What the ADT operations do, not how to implement them –Implementation of an ADT Includes choosing a particular data structure

© 2005 Pearson Addison-Wesley. All rights reserved3-5 Abstract Data Types Figure 3.4 A wall of ADT operations isolates a data structure from the program that uses it

© 2005 Pearson Addison-Wesley. All rights reserved3-6 The ADT List Except for the first and last items, each item has a unique predecessor and a unique successor Head or front do not have a predecessor Tail or end do not have a successor

© 2005 Pearson Addison-Wesley. All rights reserved3-7 The ADT List Items are referenced by their position within the list Specifications of the ADT operations –Define the contract for the ADT list –Do not specify how to store the list or how to perform the operations ADT operations can be used in an application without the knowledge of how the operations will be implemented

© 2005 Pearson Addison-Wesley. All rights reserved3-8 The ADT List ADT List operations –Create an empty list –Determine whether a list is empty –Determine the number of items in a list –Add an item at a given position in the list –Remove the item at a given position in the list –Remove all the items from the list –Retrieve (get) item at a given position in the list

© 2005 Pearson Addison-Wesley. All rights reserved3-9 The ADT List The ADT sorted list –Maintains items in sorted order –Inserts and deletes items by their values, not their positions

© 2005 Pearson Addison-Wesley. All rights reserved3-10 Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT –What data does a problem require? –What operations does a problem require?

© 2005 Pearson Addison-Wesley. All rights reserved3-11 Implementing ADTs Choosing the data structure to represent the ADT’s data is a part of implementation –Choice of a data structure depends on Details of the ADT’s operations Context in which the operations will be used Implementation details should be hidden behind a wall of ADT operations –A program would only be able to access the data structure using the ADT operations

© 2005 Pearson Addison-Wesley. All rights reserved3-12 An Array-Based ADT List A list’s items are stored in an array items Both an array and a list identify their items by number

© 2005 Pearson Addison-Wesley. All rights reserved3-13 An Array-Based ADT List A list’s k th item will be stored in items[k-1] Figure 3.11 An array-based implementation of the ADT list

© 2005 Pearson Addison-Wesley. All rights reserved3-14 Array-Based ADT List Implementation // ************************************* // Header file ListA.h for the ADT list // Array-based implementation // ************************************* const int MAX_LIST = maximum-size-of-list; typedef desired-type-of-list-item ListItemType; class List{ public: List(); // default constructor // destructor is supplied by // compiler

© 2005 Pearson Addison-Wesley. All rights reserved3-15 Array-Based ADT List Implementation // list operations: bool isEmpty() const; // Determines whether a list is empty. // Precondition: None. // Postcondition: Returns true if the // list is empty; otherwise returns // false.

© 2005 Pearson Addison-Wesley. All rights reserved3-16 Array-Based ADT List Implementation int getLength() const; // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of // items that are currently in the list.

© 2005 Pearson Addison-Wesley. All rights reserved3-17 Array-Based ADT List Implementation void insert(int index, ListItemType newItem, bool& success); // Inserts an item into the list at // position index. // Precondition: index indicates the // position at which the item should be // inserted in the list. // Postcondition: If insertion is // successful, newItem is at position // index in the list, and other items are // renumbered accordingly, and success is // true; otherwise success is false. // Note: Insertion will not be successful // if index getLength()+1.

© 2005 Pearson Addison-Wesley. All rights reserved3-18 Array-Based ADT List Implementation void remove(int index, bool& success); // Deletes an item from the list at a // given position. // Precondition: index indicates where // the deletion should occur. // Postcondition: If 1 <= index <= // getLength(), the item at position // index in the list is deleted, other // items are renumbered accordingly, // and success is true; otherwise success // is false.

© 2005 Pearson Addison-Wesley. All rights reserved3-19 Array-Based ADT List Implementation void retrieve(int index, ListItemType& dataItem, bool& success) const; // Retrieves a list item by position. // Precondition: index is the number of // the item to be retrieved. // Postcondition: If 1 <= index <= // getLength(), dataItem is the value of // the desired item and success is true; // otherwise success is false.

© 2005 Pearson Addison-Wesley. All rights reserved3-20 Array-Based ADT List Implementation private: ListItemType items[MAX_LIST]; // array of list items int size; // number of items in list int translate(int index) const; // Converts the position of an item in a // list to the correct index within its // array representation. }; // end List class

© 2005 Pearson Addison-Wesley. All rights reserved3-21 Array-Based ADT List Implementation // ************************************** // Implementation file ListA.cpp for the ADT // list // Array-based implementation // ***************************************** #include "ListA.h" //header file List::List() : size(0){ } // end default constructor

© 2005 Pearson Addison-Wesley. All rights reserved3-22 Array-Based ADT List Implementation bool List::isEmpty() const{ return size == 0; } // end isEmpty

© 2005 Pearson Addison-Wesley. All rights reserved3-23 Array-Based ADT List Implementation int List::getLength() const{ return size; } // end getLength

© 2005 Pearson Addison-Wesley. All rights reserved3-24 Array-Based ADT List Implementation int List::translate(int index) const{ return index - 1; } // end translate

© 2005 Pearson Addison-Wesley. All rights reserved3-25 Array-Based ADT List Implementation void List::insert(int index, ListItemType newItem, bool& success){ success = (index >= 1) && (index <= size + 1) && (size < MAX_LIST); if (success){ // make room for new item by shifting all // items at positions >= index toward the end // of the list (no shift if index == size+1) for (int pos = size; pos >= index; --pos) items[translate(pos+1)] = items[translate(pos)]; // insert new item items[translate(index)] = newItem; ++size; // increase the size of the list by one } } // end insert

© 2005 Pearson Addison-Wesley. All rights reserved3-26 Array-Based ADT List Implementation void List::remove(int index, bool& success){ success = (index >= 1) && (index <= size) ; if (success){ // delete item by shifting all items at // positions > index toward the beginning // of the list (no shift if index == size) for (int fromPosition = index+1; fromPosition <= size; ++fromPosition) items[translate(fromPosition-1)] = items[translate(fromPosition)]; --size; // decrease the size of the list by one } } // end remove

© 2005 Pearson Addison-Wesley. All rights reserved3-27 Array-Based ADT List Implementation void List::retrieve(int index, ListItemType& dataItem, bool& success) const{ success = (index >= 1) && (index <= size); if (success) dataItem = items[translate(index)]; } // end retrieve

© 2005 Pearson Addison-Wesley. All rights reserved3-28 Summary Data abstraction controls the interaction between a program and its data structures Abstract data type (ADT): a set of data- management operations together with the data values upon which they operate An ADT should be fully defined before any implementation decisions Hide an ADT’s implementation by defining the ADT as a C++ class