Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end
end CMPT 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???
Issue with Traverse Using “current -> next”? Using “current -> next -> next”? -> called “Look Ahead” CMPT Anne Lavergne25
Look Ahead Advantage: Disadvantage: CMPT Anne Lavergne26
Deletion – with previous end CMPT Anne Lavergne27
Insertion - end CMPT Anne Lavergne28
doubly headed singly linked list CMPT Anne Lavergne29 Advantages: Disadvantages:
Deletion - end CMPT Anne Lavergne30
CMPT Anne Lavergne31 singly headed doubly linked list Advantages: Disadvantages:
doubly headed doubly linked list CMPT Anne Lavergne32 Advantages: Disadvantages:
linked lists are very flexible CMPT Anne Lavergne33 singly headed singly linked circular list singly headed doubly linked circular list
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 Anne Lavergne34
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 Anne Lavergne35
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
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 Anne Lavergne37
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 Anne Lavergne38 Step 2 – Design of our List ADT
UML class diagram unchanged CMPT 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
CMPT 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):
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 Anne Lavergne41
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 Anne Lavergne42
CMPT Anne Lavergne 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
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 Anne Lavergne44
Disadvantages of using ADT’s CMPT 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