Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end.

Similar presentations


Presentation on theme: "Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end."— Presentation transcript:

1 Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end

2 Deletion @ end CMPT 225 - Anne Lavergne24... delete( ) if (head != NULL) // Move to the end of the list Node* current = head; // Anchor while (current -> next != NULL) current = current -> next; // Then what???

3 Issue with Traverse Using “current -> next”? Using “current -> next -> next”? -> called “Look Ahead” CMPT 225 - Anne Lavergne25

4 Look Ahead Advantage: Disadvantage: CMPT 225 - Anne Lavergne26

5 Deletion – with previous pointer @ end CMPT 225 - Anne Lavergne27

6 Insertion - Improvement @ end CMPT 225 - Anne Lavergne28

7 doubly headed singly linked list CMPT 225 - Anne Lavergne29 Advantages: Disadvantages:

8 Deletion - Improvement @ end CMPT 225 - Anne Lavergne30

9 CMPT 225 - Anne Lavergne31 singly headed doubly linked list Advantages: Disadvantages:

10 doubly headed doubly linked list CMPT 225 - Anne Lavergne32 Advantages: Disadvantages:

11 linked lists are very flexible CMPT 225 - Anne Lavergne33 singly headed singly linked circular list singly headed doubly linked circular list

12 Be Careful Do not confuse components of a linked list (attributes of List ADT class) ◦ Such as “head” and “tail” with local variables of various methods manipulating linked list ◦ Such as “current” and “previous” CMPT 225 - Anne Lavergne34

13 Activity - Problem Statement We are asked to develop a software application to manage customer accounts These accounts are to be ◦ listed in ascending alphabetical sort order of customer last name or in ascending numerical sort order of customer SIN a lot Let’s design our underlying data structure to our ADT class ◦ Let’s create a linked list that would allow us to perform these operations in O(n) CMPT 225 - Anne Lavergne35

14 Back to solving our Problem Well, in this course, We shall design an ADT Remember where we were when we took our Side Trip – Pointers and linked lists? 36

15 Back to solving our Problem We were in the process of solving our social network problem using a data collection List ADT but this time (in Part 3), we decided to implement this data collection List ADT class using a linked list as opposed to an array -> linked-based implementation of List ADT class CMPT 225 - Anne Lavergne37

16 Part of our List ADT design remain unchanged 1.Our list ADT remains a position-oriented 2.The operations our List ADT needs to perform remain unchanged  Determine the number of elements in a list  Insert an element at a given position in the list  Remove the element at a given position in the list  Remove all the elements from the list  Look at (get) the element at a given position in the list  Replace (set) an element at a given position on the list -> List ADT Public Interface remains unchanged CMPT 225 - Anne Lavergne38 Step 2 – Design of our List ADT

17 UML class diagram unchanged CMPT 225 - Anne Lavergne39 FriendsBook - numberOfMembers : integer - members : List + join( newProfile : Profile ) : void + leave( profile : Profile ) : void + modify( profile : Profile ) : void + search( target : Profile ) : void Profile - name : String - image : String - status : String - numberOfFriends : integer - friends : String[0..*] 0..* + getElementCount( ) : integer + insert( newPosition : integer, newElement: Profile) : boolean + remove( position : integer ) : boolean + clear( ) : void + getElement(position : integer) : Profile + setElement(position : integer, newElement : Profile) : void - elementCount : integer - elements : Profile[0..*] + addFriend( aFriend : String ) : void

18 CMPT 225 - Anne Lavergne40 Step 2 – Design of our List ADT So, the only thing that needs to be redesigned, i.e., changed is the underlying data structure of our List ADT and the algorithm of its methods -> Basically: what is behind the wall! FriendsBook (client code) Public interface ListADT Insert Etc... Program linked list of Profile objects Class attributes (private):

19 Step 3 - Solution # 3 – Code Posted on our course web site Week 5: ◦ Code demonstrating the use of an ADT (linked-based implementation of our List ADT):  FriendsBook Application - version 5: ListADT.h, and ListADT.cppListADT.hListADT.cpp ◦ Note that the FriendsBook and the class Profile have not changed CMPT 225 - Anne Lavergne41

20 Let’s compare Solution #2 with Solution #3 Let’s compare Solution #2 in which we implemented our data collection List ADT class using an array as an underlying data structure (CDT) with Solution #3 in which we implemented our data collection List ADT class using a linked list (singly headed singly linked – SHSL) as an underlying data structure (CDT) CMPT 225 - Anne Lavergne42

21 CMPT 225 - Anne Lavergne 22 43 FriendsBook (client code) Public interface ListADT Program array of Profile objects Insert Etc... Class attributes (private): Solution # 2 Comparing them using Wall diagrams FriendsBook (client code) Public interface ListADT Insert Etc... Program linked list of Profile objects Class attributes (private): Solution # 3

22 Advantages of using ADT’s These 2 implementations (array-based and linked-based) of our data collection List ADT class demonstrate some of the advantages of using an ADT (as opposed to not using an ADT) in our solution: 1.Application easy to understand, design, implement, maintain, debug, test because each class is assigned only one responsibility 2.Ease s/w development team work 3.Ease modification (of private/hidden section of ADT) 4.Ease reusability CMPT 225 - Anne Lavergne44

23 Disadvantages of using ADT’s CMPT 225 - Anne Lavergne45 1. Must design the Public Interface of our ADT very well since, once it has been released, it cannot change 2. Need to write getters and setters


Download ppt "Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end."

Similar presentations


Ads by Google