TA: Nouf Al-Harbi nouf200@hotmail.com Data Structures LAB 2 TA: Nouf Al-Harbi nouf200@hotmail.com
Data structures lab 2 Unsorted List
objectives By the end of this lab you will be able to : Implement Unsorted List Class with all its operations Unsorted List
List A list is a homogeneous collection of elements with a linear relationship between elements. each list element (except the first) has a unique predecessor each element (except the last) has a unique successor. 4 3 2 1 Nada Reema Amal Lama Sara predecessor successor Unsorted List
Unsorted & Sorted List Unsorted List Sorted List Elements are placed into the list in no particular order. List elements are in an order that is sorted in some way May be numerically or alphabetically , … etc. Unsorted List
Unsorted List Example .. Implement Unsorted List of 10 integer elements Unsorted List
UnsortedType class (data & methods members) 3 data members Info Array of 10 elements Actual data of list Length Determines the length of list Ranges between 0 (No elements and 10 (max number of elements) CurrentPos As an index Ranges between -1 (prior to 1st element )and the length - 1(points to the last element) Private data: length info [ 0 ] [ 1 ] [ 2 ] [ 9 ] currentPos
UnsortedType class (data & methods members) Constructor MakeEmpty Private data: length info [ 0 ] [ 1 ] [ 2 ] [ 9 ] currentPos InsertItem Transformers change state DeleteItem IsFull Observers Observe state LengthIs RetrieveItem ResetList Iterators Process All GetNextItem
Implementing Unsorted Class the class declaration is placed in one file (header file) Unsorted.h the implementation of all the methods is put in another fileUnsorted.cpp the Main function is put in another file UnsortedList.cpp Foe each Unsorted.cpp & UnsortedList we should inclusde Unsorted.h file #include “Unsorted. h” Unsorted List
Implementing Unsorted Class 1- class declaration Unsorted.h Unsorted List
unsorted.h // SPECIFICATION FILE ( unsorted.h ) class UnsortedType // declares a class data type { public: // 8 public member functions UnsortedType(); void MakeEmpty ( ) ; bool IsFull ( ) const ; int LengthIs ( ) const ; // returns length of list bool RetrieveItem ( int item ) ; void InsertItem ( int item ) ; void DeleteItem ( int item ) ; void ResetList ( ); int GetNextItem ( ) ; private: // 3 private data members int info[10] ; int currentPos ; int length; } ; Unsorted List
Implementing Unsorted Class 2- implementation of all the methods Unsorted.cpp Unsorted List
UnsortedType Counstructor UnsortedType::UnsortedType() { length = 0; }
MakeEmpty Method void UnsortedType::MakeEmpty ( ) { length = 0 ; }
Inside constant functions we can’t change data values IsFull Method IsFull Full True Not full false Inside constant functions we can’t change data values bool UnsortedType::IsFull ( ) const { return (length == 10) ; }
LengthIs Method LengthIs int UnsortedType::LengthIs ( ) const { Length of the list int UnsortedType::LengthIs ( ) const { return length ; }
The item that will be inserted InsertItem Method InsertItem The item that will be inserted void UnsortedType::InsertItem ( int item ) { info[length] = item ; length++ ; }
The item that will be searched for RetrieveItem Method RetrieveItem The item that will be searched for Found true Not found false bool UnsortedType::RetrieveItem (int item) { int location = 0 ; //index bool found = false ; for(location=0;location<length;location++) if(item==info[location]) found=true; } return found };
The item that will be deleted DeleteItem Method DeleteItem The item that will be deleted void UnsortedType::DeleteItem ( int item ) { int location = 0 ; for(location=0;location<length;location++) if(item==info[location]) info [location] = info [length - 1 ] ; length-- ; }
ResetList Method void UnsortedType::ResetList ( ) { currentPos = -1 ; }
The item that follows the current item GetNextItem Method GetNextItem The item that follows the current item int UnsortedType::GetNextItem ( ) { int item; currentPos++; item = info[currentPos]; return item ; }
Implementing Unsorted Class 3- Main function UnsortedList.cpp Unsorted List
In Main function .. Insert some elements into the list (taken form user) Search for element (taken from user) Ask whether the List is full or not Display the elements that are in the list. Delete an element from the list (taken form user) Display the elements that are in the list (After Deletion) Unsorted List