Implementation of Unsorted List Class in Data Structures Lab
In this lab, you will learn to implement the Unsorted List Class along with its key operations, such as inserting, deleting, and retrieving items. An unsorted list contains elements with no specific order, where each element (except the first) has a unique predecessor and each element (except the last) has a unique successor. You will gain hands-on experience working with a list of integers, understanding the methods for managing and manipulating the list, and observing its behavior via various operations.
Implementation of Unsorted List Class in Data Structures Lab
E N D
Presentation Transcript
Data StructuresLAB 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. 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
Private data: length info [ 0 ] [ 1 ] [ 2 ] [ 9 ] currentPos 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) UnsortedType Constructor MakeEmpty 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 inclusdeUnsorted.h file • #include “Unsorted. h” Unsorted List
Implementing Unsorted Class 1- class declaration Unsorted.h Unsorted List
unsorted.h Unsorted List
Implementing Unsorted Class 2- implementation of all the methods Unsorted.cpp Unsorted List
IsFull Method • Full True • Not full false Inside constant functions we can’t change data values
LengthIs Method • Length of the list
InsertItem Method • The item that will be inserted
RetrieveItem Method • The item that will be searched for • Found true • Not found false
DeleteItem Method • The item that will be deleted
GetNextItem Method The item that follows the current 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