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.

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.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
ADTs unsorted List and Sorted List
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Chapter 6 Queues and Deques.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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.
© 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.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
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,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 9 B-1 Chapter 9 (continued) Advanced Java Topics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
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/Headington.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 9: Implementing ADTs Lecturer: Santokh Singh Please try to Understand All concepts involved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
An Array-Based Implementation of the ADT List
CSCE 210 Data Structures and Algorithms
Data Abstraction: The Walls
Abstract Data Types (ADT)
Data Abstraction: The Walls
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.
Implementations of the ADT Stack
Chapter 4 Linked Lists.
Ch. 4 Data Abstraction Modular programming Procedural abstraction
Data Abstraction: The Walls
Abstract Data Types (ADT)
Chapter 4 Linked Lists.
Data Abstraction: The Walls
Array-Based Implementations
Data Abstraction: The Walls
Presentation transcript:

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 predecessor and a unique successor Head (or front) does not have a predecessor Tail (or end) does not have a successor

3 The ADT List Items are referenced by their position within the list Specifications of the ADT operations –Define an operation 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

4 The ADT List ADT List Operations –Create an empty list –Destroy a list –Determine whether a list is empty –Determine the number of items in a list –Insert an item at a given position in the list –Delete the item at a given position in the list –Look at (retrieve ) the item at a given position in the list

5 The ADT List Operation Contract for the ADT List createList() destroyList() isEmpty():boolean {query} getLength():integer {query} insert(in index:integer, in newItem:ListItemType, out success:boolean) remove(in index:integer, out success:boolean) retrieve(in index:integer, out dataItem:ListItemType, out success:boolean) {query}

6 The ADT List Pseudocode to create the list milk, eggs, butter aList.createList() aList.insert(1, milk, success) aList.insert(2, eggs, success) aList.insert(3, butter, success)

7 The ADT List milk, eggs, butter Insert bread after milk aList.insert(2, bread, success) milk, bread, eggs, butter Insert juice at end of list aList.insert(5, juice, success) milk, bread, eggs, butter, juice

8 The ADT List milk, bread, eggs, butter, juice Remove eggs aList.remove(3, success) milk, bread, butter, juice Insert apples at beginning of list aList.insert(1, apples, success) apples, milk, bread, butter, juice

9 The ADT List apples, milk, bread, butter, juice Pseudocode function that displays a list displayList(in aList:List) for (position = 1 to aList.getLength()) { aList.retrieve(position, dataItem, success) Display dataItem } // end for

10 The ADT List Figure 3-7 The wall between displayList and the implementation of the ADT list

11 The ADT Sorted List The ADT sorted list –Maintains items in sorted order –Inserts and deletes items by their values, not their positions

12 The ADT Sorted List Operation Contract for the ADT Sorted List sortedIsEmpty():boolean{query} sortedGetLength():integer{query} sortedInsert(in newItem:ListItemType, out success:boolean) sortedRemove(in index:integer, out success :boolean) sortedRetrieve(in index:integer, out dataItem:ListItemType, out success :boolean){query} locatePosition(in anItem:ListItemType, out isPresent:boolean):integer{query}

13 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?

14 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

15 Implementing ADTs Implementation details should be hidden behind a wall of ADT operations –A program (client) should only be able to access the data structure by using the ADT operations

16 Implementing ADTs Figure 3-8 ADT operations provide access to a data structure

17 Implementing ADTs Figure 3-9 Violating the wall of ADT operations

18 An Array-Based ADT List Both an array and a list identify their items by number –Using an array to represent a list is a natural choice –Store a list’s items in an array items Distinguish between the list’s length and the array’s size –Keep track of the list’s length

19 An Array-Based ADT List Header file ListA.h */ const int MAX_LIST = maximum-size-of-list ; typedef desired-type-of-list-item ListItemType; class List { public:... private: ListItemType items[MAX_LIST]; int size; }; // end List

20 An Array-Based ADT List A list’s k th item is stored in items[k-1] Figure 3-11 An array-based implementation of the ADT list

21 An Array-Based ADT List To insert an item, make room in the array Figure 3-12 Shifting items for insertion at position 3

22 An Array-Based ADT List To delete an item, remove gap in array Figure 3-13 (a) Deletion causes a gap; (b) fill gap by shifting

23 C++ Exceptions Throwing exceptions –A throw statement throws an exception throw ExceptionClass ( stringArgument ); –Methods that throw an exception have a throw clause void myMethod(int x) throw(MyException) { if (...) throw MyException(“MyException: …”);... } // end myMethod You can use an exception class in the C++ Standard Library or define your own

24 An ADT List Implementation Using Exceptions We define two exception classes #include using namespace std; class ListIndexOutOfRangeException : public out_of_range { public: ListIndexOutOfRangeException(const string & message = “”) : out_of_range(message.c_str()) {} }; // end ListException

25 An ADT List Implementation Using Exceptions #include using namespace std; class ListException : public logic_error { public: ListException(const string & message = “”) : logic_error(message.c_str()) {} }; // end ListException

26 An ADT List Implementation Using Exceptions ListAexcept.h */ #include “ListException.h” #include “ListIndexOutOfRangeException.h”... class List {public:... void insert(int index, const ListItemType& newItem) throw(ListIndexOutOfRangeException, ListException);... } // end List

27 An ADT List Implementation Using Exceptions ListAexcept.cpp */ void List::insert(int index, const ListItemType& newItem) throw(ListIndexOutOfRangeException, ListException); { if (size > MAX_LIST) throw ListException(“ListException: ” + “List full on insert”);... } // end insert

28 Group In-class exercise: Write the implementation for the following tasks of the ADT List Operation Contract for the ADT List createList() destroyList() isEmpty():boolean {query} getLength():integer {query} insert(in index:integer, in newItem:ListItemType, out success:boolean) remove(in index:integer, out success:boolean) retrieve(in index:integer, out dataItem:ListItemType, out success:boolean) {query}