Unsorted Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Unsorted Lists

Description:

Length: the number of items in the list. What is an unsorted list? ... return(currentPos == length - 1); template class ItemType ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 22
Provided by: Penelope69
Learn more at: https://www.cse.unr.edu
Category:
Tags: length | lists | unsorted

less

Transcript and Presenter's Notes

Title: Unsorted Lists


1
Unsorted Lists
  • CS 308 Data Structures

2
What is a list?
  • A list is a homogeneous collection of elements.
  • Linear relationship between elements
  • (1) Each element except the first one has a
    unique predecessor.
  • (2) Each element except the last one has a
    unique successor.
  • Length the number of items in the list.

3
What is an unsorted list?
  • A list in which data items are placed in no
    particular order.

What is a sorted list?
  • A list in which data items are placed in a
    particular order.
  • Key a member of the class whose value is used to
    determine the order of the items in the list.

4
(No Transcript)
5
Operations
  • MakeEmpty
  • Boolean IsFull
  • int LengthIs
  • RetrieveItem (ItemType item, Boolean found)
  • InsertItem (ItemType item)
  • DeleteItem (ItemType item)
  • ResetList
  • void GetNextItem (ItemType item)

6
RetrieveItem (ItemType item, Boolean found)
  • Function Retrieves list element whose key
    matches item's key (if present).
  • Preconditions (1) List has been initialized,
  • (2) Key member of item has been initialized.
  • Postconditions (1) If there is an element
    someItem whose key matches item's key, then
    foundtrue and item is a copy of someItem
    otherwise, foundfalse and item is unchanged,
    (2) List is unchanged.

7
InsertItem (ItemType item)
  • Function Adds item to list
  • Preconditions
  • (1) List has been initialized,
  • (2) List is not full,
  • (3) item is not in list.
  • Postconditions item is in list.

8
DeleteItem (ItemType item)
  • Function Deletes the element whose key matches
    item's key
  • Preconditions (1) List has been initialized, (2)
    Key member of item has been initialized, (3)
    There is only one element in list which has a key
    matching item's key.
  • Postconditions No element in list has a key
    matching item's key.

9
ResetList
  • Function Initializes current position for an
    iteration through the list.
  • Preconditions List has been initialized
  • Postconditions Current position is prior to
    first element in list.

10
void GetNextItem (ItemType item)
  • Function Gets the next element in list.
  • Preconditions (1) List has been initialized,
  • (2) Current position is defined
  • Postconditions (1) Current position is updated
    to next position, (2) item is a copy of element
    at current position.

11
Unsorted List Implementation
  • templateltclass ItemTypegt
  • class UnsortedType
  • public
  • void MakeEmpty()
  • bool IsFull() const
  • int LengthIs() const
  • void RetrieveItem(ItemType, bool)
  • void InsertItem(ItemType)
  • void DeleteItem(ItemType)
  • void ResetList()
  • bool IsLastItem()
  • void GetNextItem(ItemType)
  • private
  • int length
  • ItemType infoMAX_ITEMS
  • int currentPos

12
(No Transcript)
13
Unsorted List Implementation
(cont.)
  • templateltclass ItemTypegt
  • void UnsortedTypeltItemTypegtMakeEmpty()
  • length 0
  •  
  • templateltclass ItemTypegt
  • bool UnsortedTypeltItemTypegtIsFull() const
  • return (length MAX_ITEMS)
  •  
  • templateltclass ItemTypegt
  • int UnsortedTypeltItemTypegtLengthIs() const
  • return length

14
(No Transcript)
15
Unsorted List Implementation
(cont.)
  • templateltclass ItemTypegt
  • void UnsortedTypeltItemTypegtRetrieveItem
    (ItemType item, bool found)
  • int location 0
  • found false
  •  
  • while( (location lt length) !found)
  •  
  • if (item infolocation)
  •   found true
  • item infolocation
  • else
  • location

16
Unsorted List Implementation
(cont.)
  • templateltclass ItemTypegt
  • void UnsortedTypeltItemTypegtInsertItem (ItemType
    item)
  • infolength item
  • length

17
Unsorted List Implementation (cont.)
  • templateltclass ItemTypegt
  • void UnsortedTypeltItemTypegtDeleteItem(ItemType
    item)
  • int location 0
  • while(item ! infolocation)
  • location
  •  
  • infolocation infolength - 1
  • length--

18
(No Transcript)
19
Unsorted List Implementation (cont.)
  • templateltclass ItemTypegt
  • void UnsortedTypeltItemTypegtResetList()
  • currentPos -1
  •  
  • templateltclass ItemTypegt
  • bool UnsortedTypeltItemTypegtIsLastItem()
  • return(currentPos length - 1)
  •  
  • templateltclass ItemTypegt
  • void UnsortedTypeltItemTypegtGetNextItem
    (ItemType item)
  • currentPos
  • item infocurrentPos

20
(No Transcript)
21
  • Write a client function that splits an unsorted
    list into two unsorted lists using the following
    specification.
  • SplitLists (UnsortedType list, ItemType item,
    UnsortedType list1, UnsortedType list 2)
  • Function Divides list into two lists according
    to the key of item.
  • Preconditions list has been initialized and is
    not empty.
  • Postconditions list1 contains all the items of
    list whose keys are less than or equal to items
    key. list2 contains all the items of list whose
    keys are greater than items key.

22
  • ItemType listItem
  • list.ResetList()
  • while ( !list.IsLastItem())
  • list.GetNextItem(listItem)
  • if(listItem gt item)
  • if (!list2.IsFull())
  • list2.InsertItem(listItem)
  • else
  • if ( !list1.IsFull())
  • list1.InsertItem(listItem)

23
Exercises
  • 1, 9
Write a Comment
User Comments (0)
About PowerShow.com