2-1unsortedlists

Upload: mohamedkhalid

Post on 31-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 2-1UnsortedLists

    1/22

    1

    Unsorted Lists

    Data Structures Section 2-1

  • 8/14/2019 2-1UnsortedLists

    2/22

    2

    What is a list?

    A list is a homogeneous collection ofelements.

    Linear relationship between elements:

    (1) Each element except the first one hasa unique predecessor.

    (2) Each element except the last one has

    a unique successor. Length: the number of items in the list.

  • 8/14/2019 2-1UnsortedLists

    3/22

    3

    What is an unsorted list?

    A list in which data items are placed inno particular order.

    What is a sorted list?

    A list in which data items are placed in aparticular order.

    Key: a member of the class whose value isused to determine the order ofthe items inthe list.

  • 8/14/2019 2-1UnsortedLists

    4/22

    4

  • 8/14/2019 2-1UnsortedLists

    5/22

    5

    Operations

    MakeEmpty Boolean IsFull

    int LengthIs

    RetrieveItem (ItemType& item, Boolean& found)

    InsertItem (ItemType item)

    DeleteItem (ItemType item)

    ResetList

    void GetNextItem (ItemType& item)

  • 8/14/2019 2-1UnsortedLists

    6/22

    6

    RetrieveItem (ItemType& item,

    Boolean& found)

    Function: Retrieves list element whose keymatches 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 elementsomeItem whose keymatches item's key, thenfound=true and item is a copy ofsomeItem;otherwise, found=false and item is unchanged,(2) List is unchanged.

  • 8/14/2019 2-1UnsortedLists

    7/22

    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/14/2019 2-1UnsortedLists

    8/22

    8

    DeleteItem (ItemType item)

    Function: Deletes the element whose keymatches item's key

    Preconditions: (1) List has beeninitialized, (2) Key member of item hasbeen initialized, (3) There is only oneelement in list which has a key matching

    item's key.

    Postconditions: No element in list has a

    key matching item's key.

  • 8/14/2019 2-1UnsortedLists

    9/22

    9

    ResetList

    Function: Initializes current position for

    an iteration through the list.

    Preconditions: List has been initialized

    Postconditions: Current position is priorto first element in list.

  • 8/14/2019 2-1UnsortedLists

    10/22

    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 isupdated to next position, (2) item is acopy of element at current position.

  • 8/14/2019 2-1UnsortedLists

    11/22

    11

    Unsorted List Implementation

    template

    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 info[MAX_ITEMS];

    int currentPos;};

  • 8/14/2019 2-1UnsortedLists

    12/22

    12

  • 8/14/2019 2-1UnsortedLists

    13/22

    13

    Unsorted List Implementationtemplate

    void UnsortedType::MakeEmpty(){

    length = 0;}

    templatebool UnsortedType::IsFull() const{

    return (length == MAX_ITEMS);}

    template

    int UnsortedType::LengthIs() const{

    return length;}

    (cont.)

  • 8/14/2019 2-1UnsortedLists

    14/22

    14

  • 8/14/2019 2-1UnsortedLists

    15/22

    15

    Unsorted List Implementation

    template

    void UnsortedType::RetrieveItem (ItemType& item,bool& found)

    {

    int location = 0;found = false;

    while( (location < length) && !found)

    if (item == info[location]) { found = true;

    item = info[location]; }

    elselocation++;

    }

    (cont.)

  • 8/14/2019 2-1UnsortedLists

    16/22

    16

    Unsorted List Implementation

    template

    void UnsortedType::InsertItem (ItemTypeitem){

    info[length] = item;

    length++;}

    (cont.)

  • 8/14/2019 2-1UnsortedLists

    17/22

    17

    Unsorted List Implementation

    (cont.)template

    void UnsortedType::DeleteItem(ItemType item){

    int location = 0;while(item != info[location])

    location++;

    info[location] = info[length - 1];length--;

    }

  • 8/14/2019 2-1UnsortedLists

    18/22

    18

  • 8/14/2019 2-1UnsortedLists

    19/22

    19

    Unsorted List Implementation

    (cont.)template

    void UnsortedType::ResetList(){

    currentPos = -1;}

    templatebool UnsortedType::IsLastItem(){

    return(currentPos == length - 1);}

    templatevoid UnsortedType::GetNextItem (ItemType& item){

    currentPos++;item = info[currentPos];

    }

  • 8/14/2019 2-1UnsortedLists

    20/22

  • 8/14/2019 2-1UnsortedLists

    21/22

    21

    Write a client function that splits anunsAorted 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 keyof item.

    Preconditions: list has been initialized and is notempty.

    Postconditions: list1 contains all the items of listwhose keys are less than or equal to items key. list2contains all the items of list whose keys are greaterthan items key.

  • 8/14/2019 2-1UnsortedLists

    22/22

    22

    ItemType listItem;

    list.ResetList();while ( !list.IsLastItem()) {

    list.GetNextItem(listItem);

    if(listItem > item) {

    if (!list2.IsFull())

    list2.InsertItem(listItem);

    }

    else {

    if ( !list1.IsFull())

    list1.InsertItem(listItem);

    }

    }