Docsity

ADT Unsorted List - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download ADT Unsorted List - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Chapter 3

ADT Unsorted List

  • Lecture

List Definitions

Unsorted list

A list in which data items are placed in no

particular order;

the only relationship between data elements is the

list predecessor and successor relationships.

Sorted list

A list that is sorted by the value in the key;

There is a semantic relationship among the

keys of the items in the list.

Key

The attributes that are used to determine the

logical order of the list. Docsity.com^4

Abstract Data Type (ADT)

• A data type whose properties

(domain and operations) are

specified independently of any

particular implementation.

ADT Operations

• Constructor

• Transformer

• Observer

• Iterator

Sorted and Unsorted Lists

UNSORTED LIST

Elements are placed

into the list in

no particular order.

SORTED LIST

List elements are in an

order that is sorted in

some way

  • either numerically,
  • alphabetically by the

elements themselves, or

by a component of the

element

  • called a KEY member

What is a Generic Data Type?

A generic data type is a type for which the

operations are defined

but the types of the items being manipulated

are not defined.

One way to simulate such a type for our

UnsortedList ADT is via

a user-defined class ItemType with

member function ComparedTo

returning an enumerated type value LESS,

GREATER, or EQUAL.

// SPECIFICATION FILE ( unsorted.h ) #include “ItemType.h”

class UnsortedType // declares a class data type { public : // 8 public member functions void UnsortedType ( ); bool IsFull ( ) const; int GetLength ( ) const ; // returns length of list ItemType GetItem ( ItemType item, bool& found); void PutItem ( ItemType item ); void DeleteItem ( ItemType item ); void ResetList ( ); ItemType GetNextItem ();

private : // 3 private data members int length; ItemType info[MAX_ITEMS]; int currentPos; };

Class Constructor Rules

1 A constructor cannot return a function value,

and has no return value type.

2 A class may have several constructors.

The compiler chooses the appropriate constructor by the

number and types of parameters used.

3 Constructor parameters are placed in a parameter list

in the declaration of the class object.

4 The parameterless constructor is the default

constructor.

5 If a class has at least one constructor, and an array of

class objects is declared, then one of the constructors

must be the default constructor, which is invoked for

each element in the array.

Class Interface Diagram

UnsortedType class

IsFull

GetLength

ResetList

DeleteItem

PutItem

UnsortedType

GetItem

GetNextItem

Private data:

length

info [ 0 ]

[ 1 ] [ 2 ]

[MAX_ITEMS-1]

currentPos

Before Inserting Henry into an

Unsorted List

length 3

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ]

[MAX_ITEMS-1]

The item will

be placed into

the length location,

and length will be

incremented.

After Inserting Henry into an

Unsorted List

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Henry

[MAX_ITEMS-1]

19

ItemType UnsortedType::GetItem ( ItemType item, bool& found ) // Pre: Key member of item is initialized. // Post: If found, item ’ s key matches an element ’ s key in the list // and a copy of that element is returned; // otherwise, input item is returned. { bool moreToSearch; int location = 0;

found = false; moreToSearch = ( location < length ); while ( moreToSearch && !found ) { switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++; moreToSearch = ( location < length ); break; case EQUAL : found = true; item = info[ location ]; break; } } return item; } (^) Docsity.com 19

Getting Ivan from an Unsorted List

moreToSearch: true

found: false

location: 0

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Henry

[MAX_ITEMS-1]