adt unsorted list - fordham university · array struct union class address pointer reference simple...

66
ADT Unsorted List

Upload: others

Post on 24-Jul-2020

14 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

ADT Unsorted List

Page 2: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Outline

• What is a list?

Page 3: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

C++ Data Types

Composite

array struct union class

Address

pointer reference

Simple

Integral Floating

char short int long enum

float double long double

Page 4: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Concept: ADT List• Abstract Data Type: A data type whose properties

(domain and operations) are specified independently of any particular implementation.

• ADT list is a homogeneous collection of elements, with linear relationship among elements • each element except the first has a unique

predecessor, and each element except the last has a unique successor.)

• Each element in list can be of any type: simple, composite, address, or list, … • Often times, element is a struct or a class type • one field/attribute of the struct/class is used as key

field (used to search for element, sort elements)

Page 5: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

ExamplesYou anticipate a busy day ahead, and decide to write down tasks that you need to finish: Typically you write down things as they occur to you…

Check emails and answer urgent emails Make doctor’s appointment Attend a meeting Start on math homework Revise history essay ….

Elements (tasks) are arranged in no particular order. This is an Unsorted List.

Page 6: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Examples: cont’dYou anticipate a busy day ahead, and decide to write down tasks that you need to finish:

Check emails and answer urgent emails Make doctor’s appointment Attend a meeting Start on math homework Revise history essay ….

If you assign a priority to each task, and rearrange todo-list in the order of priority, then it’s a sorted list

1 Start on math homework 2 Revise history essay 3 Attend a meeting 5 Make doctor’s appointment 8 Check emails…

Page 7: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Sorted and Unsorted Lists UNSORTED LIST

Elements are placed into the list in no particular order. e.g., a to-do list a shopping list

you write them down when something occurs to you…

SORTED LIST

elements are stored in sorted order -- numerically or alphabetically by elements themselves, or by a component/field of element (called a KEY member) . • class list sorted by

students last name • to-do list sorted by • priority • shopping list sorted

alphabetically

Page 8: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

ADT Unsorted List OperationsTransformers

• MakeEmpty • PutItem • DeleteItem

Observers • IsFull • GetLength • GetItem

Iterators • ResetList • GetNextItem

change state

observe state

process all

Page 9: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

What is a Generic Data Type?•Build ADT Unsorted List as a generic data type,

means that items being stored in the list is not specified, i.e., it can be of any type

• Advantage: we implement it once, and use it to store different lists: • list of to-dos, list of numbers, list of student

records, … •We have used generic data type before: • vector<int>, vector<double>, vector<DateType> … • vector is a generic data type implemented in C++

Standard Template Library

Page 10: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Recall from CS2… • function template template<class T> void swap (T & x, T & y) { T tmp; tmp = x; x = y; y = tmp; } double a[2]={3,4}; swap (a[1], a[2]);

•class template template<class T> class Pair { { public: Pair (T f, T s); // … private: T first; T second; }

Pair<int> pair1 (3,4);

Page 11: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

generic programming without template?

For now, we simulate a generic data type without using template: • implement ADT list to store elements of a user-

defined class ItemType •User (of ADT list) can define ItemType to

encapsulate any type of items as needed • as long as ItemType provides member function to

compare two items’ keys • In sample code, member function is named

ComparedTo, and returns an enumerated type value LESS, GREATER, or EQUAL. )

Page 12: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Private data

value

ComparedTo

Print

Initialize

class ItemType

ItemType Class Interface Diagram

Page 13: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

// 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; };

Page 14: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Class Constructor Rules1 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.

Page 15: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Class Interface Diagram

UnsortedType class

IsFull

GetLength

ResetList

DeleteItem

PutItem

UnsortedType

GetItem

GetNextItem

Private data:

length

info [ 0 ] [ 1 ] [ 2 ]

[MAX_ITEMS-1]

currentPos this (using static array) is just one way to implement it…

Page 16: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

14

// IMPLEMENTATION FILE ARRAY-BASED LIST ( unsorted.cpp ) #include “itemtype.h”

void UnsortedType::UnsortedType ( ) // Pre: None. // Post: List is empty. { length = 0; }

void UnsortedType::InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // Post: item is in the list. { info[length] = item; length++; }

Page 17: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Before Inserting Hsing 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.

Page 18: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

After Inserting Hsing into an Unsorted List

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . . [MAX_ITEMS-1]

Page 19: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

17

int UnsortedType::GetLength( ) const // Pre: List has been inititalized. // Post: Function value == ( number of elements in // list ). { return length; }

bool UnsortedType::IsFull ( ) const // Pre: List has been initialized. // Post: Function value == ( list is full ). { return ( length == MAX_ITEMS ); }

Page 20: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

18

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 ); case EQUAL : found = true; item = info[ location ]; break; } } return item }

Page 21: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Getting Ivan from an Unsorted List

moreToSearch: true found: false location: 0

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . [MAX_ITEMS-1]

Page 22: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Getting Ivan from an Unsorted List

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . [MAX_ITEMS-1]

moreToSearch: true found: false location: 1

Page 23: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Getting Ivan from an Unsorted Listlength 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . . [MAX_ITEMS-1]

moreToSearch: true found: false location: 2

Page 24: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Getting Ivan from an Unsorted List

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . . [MAX_ITEMS-1]

moreToSearch: true found: false location: 3

Page 25: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Getting Ivan from an Unsorted List

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . .

[MAX_ITEMS-1]

moreToSearch: false found: false location: 4

Page 26: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

void UnsortedType::DeleteItem ( ItemType item ) // Pre: item’s key has been inititalized. // An element in the list has a key that matches item’s. // Post: No element in the list has a key that matches item’s. { int location = 0 ; while (item.ComparedTo (info [location] ) != EQUAL ) location++;

// move last element into position where item was located

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

Page 27: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Deleting Bradley from an Unsorted List

location: 0 length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . . [MAX_ITEMS-1]

Key Bradley has not been matched.

Page 28: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Deleting Bradley from an Unsorted List

location: 1

Key Bradley has been matched.

length 4

info [ 0 ] Maxwell

[ 1 ] Bradley

[ 2 ] Asad

[ 3 ] Hsing . . . [MAX_ITEMS-1]

Page 29: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Deleting Bradley from an Unsorted List

location: 1 length 4

info [ 0 ] Maxwell

[ 1 ] Hsing

[ 2 ] Asad

[ 3 ] Hsing . . .

[MAX_ITEMS-1]

Placed copy of last list element into the position where the key Bradley was before.

Page 30: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Deleting Bradley from anUnsorted List

location: 1

length 3

info [ 0 ] Maxwell

[ 1 ] Hsing

[ 2 ] Asad

[ 3 ] Hsing . . . [MAX_ITEMS-1]

Decremented length.

Page 31: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

void UnsortedType::ResetList ( ) // Pre: List has been inititalized. // Post: Current position is prior to first element in list. { currentPos = -1; }

ItemType UnsortedType::GetNextItem () // Pre: List has been initialized. Current position is defined. // Element at current position is not last in list. // Post: Current position is updated to next position. // item is a copy of element at current position. { currentPos++; item = info [currentPos]; }

Page 32: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Example: Iterating through an unsorted list

void PrintList (UnsortedList list) { int length; ItemType item; list.ResetList(); for (int i=1; i<=length; i++) { item = list.GetNextItem(); item.Print (); } }

This is a common way to access all items in a list one by one, following linear relationship

Page 33: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

// SPECIFICATION FILE ( itemtype.h )

const int MAX_ITEM = 5 ; enum RelationType { LESS, EQUAL, GREATER };

class ItemType // declares class data type { public : // 3 public member functions RelationType ComparedTo ( ItemType ) const; void Print ( ) const; void Initialize ( int number ) ;

private : // 1 private data member int value ; // could be any different // type, including a class } ;

Specifying class ItemType to store int

Page 34: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

// IMPLEMENTATION FILE ( itemtype.cpp ) // Implementation depends on the data type of value.

#include “itemtype.h” #include <iostream>

RelationType Itemtype::ComparedTo(ItemType otherItem) const

{ if ( value < otherItem.value ) return LESS; else if ( value > otherItem.value ) return GREATER; else return EQUAL; } void ItemType::Print ( ) const { using namespace std; cout << value << endl; } void ItemType::Initialize ( int number ) { value = number; }

Page 35: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Practice #1 • Download source codes to your computer

• cpUnsortedList

• Compile and link unsorted.cpp, ItemType.cpp, listDriver.cpp and run it

• Declare an ItemType to store Appointment class • so that we can create a unsortedList of appointments…

• Wrap DateType up in ItemType

• or give an alias to DateType as ItemType?

• Modify listDriver.cpp to test unsorted list of appointment.

• How do you modify an element in the list? • e.g., change description of a particular appointment in the list

Page 36: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Practice #2: switch to use operators• Modify UnsortedList so that comparison operators are used instead of

CompareTo() method

• Now we can use any data type that have overloaded comparison operators with unsorted list:

• for example: to create a list of int: • give an alias to int as ItemType before defining UnsortedList

typedef int ItemType; // class UnsortedList{ public: … ItemType GetItem (ItemType item, bool & found); … private: ItemType info[MAX_ITEMS]; };

Page 37: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

What is a pointer variable?• A pointer variable is a variable whose value is the address

of a location in memory.

• To declare a pointer variable, you must specify type of value that the pointer will point to.

int* ptr; // ptr will hold the address of an int

char* q; // q will hold the address of a char

Page 38: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Using a pointer variable

int x; x = 12;

int* ptr; ptr = &x;

NOTE: Because ptr holds the address of x, we say that ptr “points to” x

2000

12 x

3000

2000 ptr

Page 39: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

2000

12 x

3000

2000 ptr

int x; x = 12;

int* ptr; ptr = &x;

std::cout << *ptr;

NOTE: the int pointed to by ptr is denoted by *ptr

Unary operator * is deference (indirection) operator

Page 40: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

int x; x = 12;

int* ptr; ptr = &x;

*ptr = 5; // changes the value // at adddress ptr to 5

Using dereference operator

2000

12 5 x

3000

2000 ptr

Page 41: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

char ch; ch = ‘A’;

char* q; q = &ch;

*q = ‘Z’; char* p; p = q; // the right side has value 4000 // now p and q both point to ch

Another Example 4000

A Z ch

5000 6000

4000 4000 q p

Page 42: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

38

C++ Data Types

Structured

array struct union class

Address

pointer reference

Simple

Integral Floating

char short int long enum

float double long double

Page 43: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

2000

ptr

Dynamically Allocated Data

char* ptr;

ptr = new char;

*ptr = ‘B’;

std::cout << *ptr;

New is an operator

Page 44: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Dynamically Allocated Data

char* ptr;

ptr = new char;

*ptr = ‘B’;

std::cout << *ptr;

NOTE: Dynamic data has no variable name

2000

ptr

Page 45: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Dynamically Allocated Data

char* ptr;

ptr = new char;

*ptr = ‘B’;

std::cout << *ptr;

2000

ptr

‘B’

Page 46: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Dynamically Allocated Data

char* ptr;

ptr = new char;

*ptr = ‘B’;

std::cout << *ptr;

delete ptr;

2000

ptr

NOTE: delete deallocates memory pointed to by ptr.

?

Page 47: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

NULL PointerThere is a pointer constant called NULL

available in cstddef.

NULL is not a memory address; it means that the pointer variable points to nothing.

It is an error to dereference a pointer whose value is NULL. It is the programmer’s job to check for this.

while (ptr != NULL) { . . . // ok to use *ptr here }

Page 48: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

The object or array currently pointed to by the pointer is deallocated, i.e.,the memory is returned to the free store (or heap).

Square brackets are used with delete to deallocate a dynamically allocated array of classes.

Using operator delete

Page 49: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

int* ptr = new int; *ptr = 3;

ptr = new int; // changes value of ptr *ptr = 4;

What happens here?

3 ptr

3 ptr

4

Page 50: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Memory LeakA memory leak occurs when dynamic memory (that was created

using operator new) has been left without a pointer to it by the programmer, and so is inaccessible.

int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5;

8 ptr

-5

ptr2

How else can an object become inaccessible?

Page 51: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Causing a Memory Leak

int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5;

ptr = ptr2; // here the 8 becomes inaccessible

8 ptr

-5

ptr2

8 ptr

-5

ptr2

Page 52: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

• occurs when two pointers point to the same object and delete is applied to one of them.

int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2;

A Dangling Pointer

8 ptr

-5

ptr2

FOR EXAMPLE,

Page 53: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2;

delete ptr2; // ptr is left dangling ptr2 = NULL;

Leaving a Dangling Pointer

8 ptr

-5

ptr2

8 ptr

NULL

ptr2

Page 54: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Now: implement a list using linked list

• A list is a homogeneous collection of elements, with a linear relationship between elements.

• That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.

Page 55: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

ADT Unsorted List Operations

Transformers • MakeEmpty • PutItem • DeleteItem

Observers • IsFull • GetLength • GetItem

Iterators • ResetList • GetNextItem

change state

observe state

process all

Page 56: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

53

#include “ItemType.h” // unsorted.h . . . class UnsortedType { public : // LINKED LIST IMPLEMENTATION // The public interface is the same

private : // The private part is different NodeType<ItemType>* listData; int length; NodeType<ItemType>* currentPos; };

Do we have to keep a length field? Do we need an IsFull?

Page 57: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

class UnsortedType<char>

MakeEmpty

~UnsortedType

DeleteItem . . .

PutItem

UnsortedType

GetItem

GetNextItem

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

Page 58: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

55

// LINKED LIST IMPLEMENTATION ( unsorted.cpp ) #include “itemtype.h”

UnsortedType::UnsortedType ( ) // constructor // Pre: None. // Post: List is empty. { length = 0; listData = NULL; }

int UnsortedType::GetLength( ) const // Post: Function value = number of items in the list. { return length; }

Page 59: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

56

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 // a copy of that element is returned; otherwise, // origianl item is returned. { bool moreToSearch; NodeType<ItemType>* location; location = listData; found = false ; moreToSearch = ( location != NULL ); while ( moreToSearch && !found ) { if ( item == location->info ) // match here { found = true; item = location->info; } else // advance pointer { location = location->next; moreToSearch = ( location != NULL ); }

} return item; }

Page 60: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

57

void UnsortedType::PutItem ( ItemType item ) // Pre: list is not full and item is not in list. // Post: item is in the list; length has been incremented. { NodeType<ItemType>* location; // obtain and fill a node location = new NodeType<ItemType>; location->info = item; location->next = listData; listData = location; length++; }

Page 61: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

Inserting ‘B’ into an Unsorted List

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

Page 62: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

location = new NodeType;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

Page 63: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

location->info = item ;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

Page 64: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

location->next = listData ;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

Page 65: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

listData = location ;

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

Page 66: ADT Unsorted List - Fordham University · array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double. Concept: ADT

length++ ;

‘X’ ‘C’ ‘L’

Private data:

length 4

listData

currentPos ?

item

location

‘B’

‘B’