Chapter 3 ADT Unsorted List
description
Transcript of Chapter 3 ADT Unsorted List
![Page 1: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/1.jpg)
Chapter 3ADT Unsorted List
![Page 2: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/2.jpg)
Lecture 7
![Page 3: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/3.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.
3
![Page 4: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/4.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.
4
![Page 5: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/5.jpg)
Abstract Data Type (ADT)
• A data type whose properties (domain and operations) are specified independently of any particular implementation.
5
![Page 6: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/6.jpg)
What
How
• Application (or user) level: modeling real-life data in a specific context.
• Logical (or ADT) level: abstract view of the domain and operations.
• Implementation level: specific representation of the structure to hold the
data items, and the coding for operations.
Data from 3 different levels
6
Why
![Page 7: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/7.jpg)
ADT Operations
• Constructor
• Transformer
• Observer
• Iterator
7
![Page 8: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/8.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
8
![Page 9: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/9.jpg)
ADT Unsorted List Operations
Transformers • MakeEmpty • PutItem • DeleteItem
Observers • IsFull• GetLength
• GetItem
Iterators • ResetList • GetNextItem
change state
observe state
process all
9
![Page 10: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/10.jpg)
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.
10
![Page 11: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/11.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;};
11
![Page 12: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/12.jpg)
Class Constructor
A special member function of a class
that is implicitly invoked when a class
object is defined.
12
![Page 13: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/13.jpg)
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.
13
![Page 14: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/14.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
14
![Page 15: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/15.jpg)
15
// 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++;
}
15
![Page 16: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/16.jpg)
Before Inserting Henry into anUnsorted List
16
length 3
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] .
. .
[MAX_ITEMS-1]
The item willbe placed intothe length location,and length will beincremented.
16
![Page 17: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/17.jpg)
After Inserting Henry into anUnsorted List
length 4
info [ 0 ] Maxwell
[ 1 ] Bradley
[ 2 ] Asad
[ 3 ] Henry .
. . [MAX_ITEMS-1]
17
![Page 18: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/18.jpg)
18
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 );}
18
![Page 19: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/19.jpg)
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;}
19
![Page 20: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/20.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]
20
![Page 21: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/21.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
21
![Page 22: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/22.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
22
![Page 23: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/23.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
23
![Page 24: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/24.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
24
![Page 25: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/25.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-- ;
}
25
![Page 26: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/26.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.
26
![Page 27: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/27.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]
27
![Page 28: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/28.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.
28
![Page 29: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/29.jpg)
Deleting Bradley from anUnsorted List
location: 1
length 3
info [ 0 ] Maxwell
[ 1 ] Henry
[ 2 ] Asad
[ 3 ] Henry .
. .
[MAX_ITEMS-1]
Decremented length.
29
![Page 30: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/30.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];
}
30
![Page 31: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/31.jpg)
Private data
value
ComparedTo
Initialize
class ItemType
ItemType Class Interface Diagram
31
![Page 32: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/32.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
32
![Page 33: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/33.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; }
33
![Page 34: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/34.jpg)
UML diagrams
34
![Page 35: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/35.jpg)
Lecture 8
![Page 36: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/36.jpg)
36
C++ Data Types
Structured
array struct union class
Address
pointer reference
Simple
Integral Floating
char short int long enum
float double long double
36
![Page 37: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/37.jpg)
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 the type of value that the pointer will point to.
• For example,
int* ptr; // ptr will hold the address of an int
char* q; // q will hold the address of a char
37
![Page 38: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/38.jpg)
2000
12
x
3000
2000
ptr
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
38
![Page 39: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/39.jpg)
2000
12
x
3000
2000
ptr
int x; x = 12;
int* ptr; ptr = &x;
std::cout << *ptr;
NOTE: The value pointed to by ptr is denoted by *ptr
Unary operator * is the deference (indirection) operator
39
![Page 40: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/40.jpg)
int x; x = 12;
int* ptr; ptr = &x;
*ptr = 5; // changes the value // at adddress ptr to 5
Using the dereference operator
2000
12
x
3000
2000
ptr
40
5
![Page 41: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/41.jpg)
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
ch
5000
4000
q
41
Z
6000
4000
p
![Page 42: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/42.jpg)
Pointer dereferencing and member selection
42
![Page 43: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/43.jpg)
2000
ptr
Dynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
std::cout << *ptr;
New is an operator
43
![Page 44: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/44.jpg)
Dynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
std::cout << *ptr;
NOTE: Dynamic data has no variable name
2000
ptr
44
![Page 45: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/45.jpg)
Dynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
std::cout << *ptr;
2000
ptr
‘B’
45
![Page 46: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/46.jpg)
Dynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
std::cout << *ptr;
delete ptr;
2000
ptr
NOTE: Delete deallocates the memory pointed to by ptr.
?
46
![Page 47: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/47.jpg)
what does new do?
• takes a pointer variable,
• allocates memory for it to point, and
• leaves the address of the assigned memory in the pointer variable.
• If there is no more memory, the pointer variable is set to NULL.
47
![Page 48: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/48.jpg)
There 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
}
The NULL Pointer
48
![Page 49: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/49.jpg)
int* ptr = new int; *ptr = 3;
ptr = new int; // changes value of ptr *ptr = 4;
What happens here?
3
ptr
3
ptr
4
49
![Page 50: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/50.jpg)
Memory Leak
A 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?
50
![Page 51: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/51.jpg)
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
51
![Page 52: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/52.jpg)
The object or array currently pointed to by the pointer is deallocated, and
the pointer is considered unassigned.
The memory is returned to the free store.
Square brackets are used with delete to deallocate a dynamically allocated array of classes.
Using operator delete
52
![Page 53: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/53.jpg)
• 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
53
![Page 54: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/54.jpg)
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
54
![Page 55: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/55.jpg)
Remember?
• A list is a homogeneous collection of elements, with a linear relationship between elements.
• Each list element (except the first) has a unique predecessor, and
• each element (except the last) has a unique successor.
55
![Page 56: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/56.jpg)
ADT Unsorted List Operations
Transformers • MakeEmpty • PutItem • DeleteItem
Observers • IsFull• GetLength
• GetItem
Iterators • ResetList • GetNextItem
change state
observe state
process all
56
![Page 57: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/57.jpg)
#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?
57
struct NodeType;
struct NodeType { ItemType info; NodeType* next;} ;
![Page 58: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/58.jpg)
class UnsortedType<char>
MakeEmpty
~UnsortedType
DeleteItem . . .
PutItem
UnsortedType
GetItem
GetNextItem
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
58
![Page 59: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/59.jpg)
59
// 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;}
59
![Page 60: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/60.jpg)
60
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,// original 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; }
60
![Page 61: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/61.jpg)
61
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++;
}
61
![Page 62: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/62.jpg)
Inserting ‘B’ into an Unsorted List
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
62
![Page 63: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/63.jpg)
location = new NodeType;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
63
![Page 64: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/64.jpg)
location->info = item ;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
‘B’
64
![Page 65: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/65.jpg)
location->next = listData ;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
‘B’
65
![Page 66: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/66.jpg)
listData = location ;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
‘B’
66
![Page 67: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/67.jpg)
length++ ;
‘X’ ‘C’ ‘L’
Private data:
length 4
listData
currentPos ?
item
location
‘B’
‘B’
67
![Page 68: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/68.jpg)
UML diagrams
68
![Page 69: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/69.jpg)
Big-O Comparison of Unsorted List Operations
69
![Page 70: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/70.jpg)
Overview
![Page 71: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/71.jpg)
Overview
• The order of adding 1 to each element in a one dimensional array of N integers.
A. O(1)
B. O(logN)
C. O(N)
D. O(N logN)
E. O(N*N)
• The order of adding 1 to each element in a square two dimensional array of integers where the number of rows is N.
A. O(1)
B. O(logN)
C. O(N)
D. O(N logN)
E. O(N*N)
71
![Page 72: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/72.jpg)
Overview
• What is special about the last node in a dynamic linked list?
A. Its component (data) member is empty.
B. Its component (data) member contains the value 0.
C. Its link member is empty.
D. Its link member contains the value NULL.
E. It has no link member.
• A fixed-sized structure; • the mechanism for accessing the structure is built into C++.
• A variable-sized, user-defined structure; • the mechanism for accessing the structure must be provided through functions.
72
array
list
![Page 73: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/73.jpg)
Overview• To prevent a compile-time error, how should the following code be
changed?struct ListNode // Line 1
{ // Line 2
int dataVal; // Line 3
NodeType* next; // Line 4
}; // Line 5
A. Insert the following before line 1:
typedef ListNode* NodeType*;
B. Insert the following before line 1:
struct ListNode;
typedef ListNode* NodeType*;
C. Replace line 4 with the following:
ListNode* next;
D. Do either b or c above.
E. Do any of a, b, or c above.73
![Page 74: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/74.jpg)
Overview• What symbol does C++ use to terminate the internal representation
of strings?
A. 'n'
B. '\n'
C. '\0'
D. '\#'
E. C++ doesn't use a symbol to terminate a string.
• A generic data type is one in which the types of the items being manipulated are defined, but the operations are not.
• It is not possible to use a list without knowing how it is implemented.• A constructor cannot be explicitly called by the client program. • O(1) is called constant time.• O(N) is called linear time. • A destructor is a special operation that is implicitly called when a
class object goes out of scope. 74
![Page 75: Chapter 3 ADT Unsorted List](https://reader030.fdocuments.net/reader030/viewer/2022012317/56813d6f550346895da75187/html5/thumbnails/75.jpg)
Overview• Deleting from an unsorted list requires that the elements below the one
being deleted be moved up one slot.• The algorithms for finding an element in an unsorted list is O(n).• The next item in a linked list always can be found by accessing the
next physical location in memory.• Given only the external pointer to a linked list, it is faster to insert a
node at the front of the list than at the back.• The external pointer is considered to be one of the nodes of a linked
list • If currPtr points to a node in a dynamic linked list, the operation
currPtr++ advances to the next node in the list.• Reading components into an initially empty list is faster if the list is
represented directly as an array rather than a linked list.• With a list ADT, insertions and deletions at the front of the list are faster
with a linked list representation than with a direct array representation. • With a list ADT, insertions and deletions at the back of the list are faster
with a linked list representation than with a direct array representation. 75