unsorted lists
Download
Skip this Video
Download Presentation
Unsorted Lists

Loading in 2 Seconds...

play fullscreen
1 / 21

Unsorted Lists - PowerPoint PPT Presentation


  • 123 Views
  • Uploaded on

Unsorted Lists. CS 308 – Data Structures. 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Unsorted Lists' - kennita


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
unsorted lists

Unsorted Lists

CS 308 – Data Structures

what is a list
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.
what is an unsorted list
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 ofthe items in the list.
operations
Operations
  • MakeEmpty
  • Boolean IsFull
  • int LengthIs
  • RetrieveItem (ItemType& item, Boolean& found)
  • InsertItem (ItemType item)
  • DeleteItem (ItemType item)
  • ResetList
  • void GetNextItem (ItemType& item)
retrieveitem itemtype item boolean found
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 keymatches item's key, then found=true and item is a copy of someItem; otherwise, found=false and item is unchanged,(2) List is unchanged.
insertitem itemtype item
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.
deleteitem itemtype item
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'skey.
unsorted list implementation
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;

};

unsorted list implementation11
Unsorted List Implementation

(cont.)

template

void UnsortedType::MakeEmpty()

{

length = 0;

}

template

bool UnsortedType::IsFull() const

{

return (length == MAX_ITEMS);

}

template

int UnsortedType::LengthIs() const

{

return length;

}

unsorted list implementation13
Unsorted List Implementation

(cont.)

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];

}

else

location++;

}

unsorted list implementation14
Unsorted List Implementation

(cont.)

template

void UnsortedType::InsertItem (ItemType item)

{

info[length] = item;

length++;

}

unsorted list implementation cont
Unsorted List Implementation (cont.)

template

void UnsortedType::DeleteItem(ItemType item)

{

int location = 0;

while(item != info[location])

location++;

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

length--;

}

unsorted list implementation cont17
Unsorted List Implementation (cont.)

template

void UnsortedType::ResetList()

{

currentPos = -1;

}

template

bool UnsortedType::IsLastItem()

{

return(currentPos == length - 1);

}

template

void UnsortedType::GetNextItem (ItemType& item)

{

currentPos++;

item = info[currentPos];

}

slide19
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 item’s key. list2 contains all the items of list whose keys are greater than item’s key.

slide20
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);

}

}

ad