chapter 3 adt unsorted list. list definitions linear relationship each element except the first has...
TRANSCRIPT
![Page 1: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/1.jpg)
Chapter 3ADT Unsorted List
![Page 2: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/2.jpg)
List Definitions
Linear relationship
Each element except the first has a unique predecessor, and
Each element except the last has a unique successor.
Length
The number of items in a list;
The length can vary over time.
2
![Page 3: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/3.jpg)
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.
3
![Page 4: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/4.jpg)
ADT Operations
• Constructor
• Transformer
• Observer
• Iterator
4
![Page 5: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/5.jpg)
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
5
![Page 6: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/6.jpg)
ADT Unsorted List Operations
Transformers • MakeEmpty • PutItem • DeleteItem
Observers • IsFull• GetLength
• GetItem
Iterators • ResetList • GetNextItem
change state
observe state
process all
6
![Page 7: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/7.jpg)
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.
7
![Page 8: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/8.jpg)
// 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;};
8
![Page 9: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/9.jpg)
Class Interface Diagram
UnsortedType class
IsFull
GetLength
ResetList
DeleteItem
PutItem
UnsortedType
GetItem
GetNextItem
Private data:
length
info [ 0 ] [ 1 ] [ 2 ]
[MAX_ITEMS-1]
currentPos
9
![Page 10: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/10.jpg)
10
// 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++;
}
10
![Page 11: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/11.jpg)
Before Inserting Henry into anUnsorted List
11
length 3
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] .
. .
[MAX_ITEMS-1]
The item willbe placed intothe length location,and length will beincremented.
11
![Page 12: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/12.jpg)
After Inserting Henry into anUnsorted List
length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
. . [MAX_ITEMS-1]
12
![Page 13: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/13.jpg)
13
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 );}
13
![Page 14: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/14.jpg)
14
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;}
14
![Page 15: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/15.jpg)
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]
15
![Page 16: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/16.jpg)
Getting Ivan from an Unsorted List
length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
.
[MAX_ITEMS-1]
moreToSearch: true
found: false
location: 1
16
![Page 17: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/17.jpg)
Getting Ivan from an Unsorted List
length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
moreToSearch: true
found: false
location: 2
17
![Page 18: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/18.jpg)
Getting Ivan from an Unsorted List
length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
moreToSearch: true
found: false
location: 3
18
![Page 19: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/19.jpg)
Getting Ivan from an Unsorted List
length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
moreToSearch: false
found: false
location: 4
19
![Page 20: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/20.jpg)
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-- ;
}
20
![Page 21: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/21.jpg)
Deleting Bradley from an Unsorted List
location: 0 length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
Key Bradley hasnot been matched.
21
![Page 22: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/22.jpg)
Deleting Bradley from an Unsorted List
location: 1
Key Bradley hasbeen matched.
length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
22
![Page 23: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/23.jpg)
Deleting Bradley from an Unsorted List
location: 1
length 4
info [ 0 ] Maxwell
[ 1 ] Henry
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
Placed copy oflast list elementinto the position where the key Bradley was before.
23
![Page 24: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/24.jpg)
Deleting Bradley from anUnsorted List
location: 1
length 3
info [ 0 ] Maxwell
[ 1 ] Henry
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
Decremented length.
24
![Page 25: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/25.jpg)
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++;
return info [currentPos];
}
25
![Page 26: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/26.jpg)
Private data
value
ComparedTo
Initialize
class ItemType
ItemType Class Interface Diagram
26
![Page 27: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/27.jpg)
// 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 memberint value ; // could be any different
// type, including a class} ;
Specifying class ItemType
27
![Page 28: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/28.jpg)
// 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; }
28
![Page 29: Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the](https://reader036.vdocument.in/reader036/viewer/2022062308/56649cb75503460f9497c828/html5/thumbnails/29.jpg)
UML diagrams
29