+ All Categories
Home > Documents > Chapter 13-1 Applied Arrays: Lists and Strings

Chapter 13-1 Applied Arrays: Lists and Strings

Date post: 25-Feb-2016
Category:
Upload: kim
View: 50 times
Download: 2 times
Share this document with a friend
Description:
Chapter 13-1 Applied Arrays: Lists and Strings. Dale/Weems. Chapter 13 Topics. Meaning of a List Insertion and Deletion of List Elements Selection Sort of List Elements Insertion and Deletion using a Sorted List Binary Search in a Sorted List Order of Magnitude of a Function - PowerPoint PPT Presentation
26
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems
Transcript
Page 1: Chapter 13-1 Applied Arrays:  Lists and Strings

1

Chapter 13-1

Applied Arrays: Lists and Strings

Dale/Weems

Page 2: Chapter 13-1 Applied Arrays:  Lists and Strings

2

Chapter 13 Topics Meaning of a List Insertion and Deletion of List Elements Selection Sort of List Elements Insertion and Deletion using a Sorted List Binary Search in a Sorted List Order of Magnitude of a Function Declaring and Using C Strings Using typedef with Arrays

Page 3: Chapter 13-1 Applied Arrays:  Lists and Strings

3

What is a List? A list is a variable-length, linear

collection of homogeneous elements

Linear means that each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

Page 4: Chapter 13-1 Applied Arrays:  Lists and Strings

4 Basic Kinds of ADT Operations

Constructors -- create a new instance (object) of an ADT

Transformers -- change the state of one or more of the data values of an instance

Observers -- allow client to observe the state of one or more of the data values of an instance without changing them

Iterators -- allow client to access the data values in sequence

4

Page 5: Chapter 13-1 Applied Arrays:  Lists and Strings

ADT List Operations

Transformers Insert Delete Sort

Observers IsEmpty IsFull Length IsPresent

change state

observe state

5

Page 6: Chapter 13-1 Applied Arrays:  Lists and Strings

6

ADT List Operations

Iterator Reset GetNextItem

Reset prepares for the iteration GetNextItem returns the next item in

sequence No transformer can be called between calls

to GetNextItem (Why?)

Access in Sequence

Page 7: Chapter 13-1 Applied Arrays:  Lists and Strings

ADT Unsorted List Data Components

length

data[0.. MAX_LENGTH -1]

currentPos

number of elements in list

array of list elements

used in iteration

7

Page 8: Chapter 13-1 Applied Arrays:  Lists and Strings

8

Array-based class List

Reset

IsFull

Length

IsPresent

Delete

IsEmpty

Insert

GetNexItem

Private data:lengthdata [0] [1] [2]

[MAX_LENGTH-1]

currentPos

SelSort

Page 9: Chapter 13-1 Applied Arrays:  Lists and Strings

9

// Specification file array-based list (“list.h”)const int MAX_LENGTH = 50;typedef int ItemType;

class List // Declares a class data type{public: // Public member functions List(); // constructor bool IsEmpty () const; bool IsFull () const; int Length () const; // Returns length of list void Insert (ItemType item); void Delete (ItemType item); bool IsPresent(ItemType item) const; void SelSort (); void Reset (); ItemType GetNextItem (); private: // Private data members

int length; // Number of values currently stored ItemType data[MAX_LENGTH];

int CurrentPos; // Used in iteration };

9

Page 10: Chapter 13-1 Applied Arrays:  Lists and Strings

10

Sorted and Unsorted Lists

UNSORTED LIST

Elements are placed into the list in no particular order

SORTED LIST

List elements are in sorted in some way -- either numerically or alphabetically

Page 11: Chapter 13-1 Applied Arrays:  Lists and Strings

11

// Implementation file array-based list (“list.cpp”)

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

using namespace std;

int List::Length () const// Post: Return value is length{

return length;}

bool List::IsFull () const// Post: Return value is true if length is equal// to MAX_LENGTH and false otherwise{

return (length == MAX_LENGTH);}

11

Page 12: Chapter 13-1 Applied Arrays:  Lists and Strings

12

List::List ()// Constructor// Post: length == 0{

length = 0;}

void List::Insert (/* in */ ItemType item)// Pre: length < MAX_LENGTH && item is assigned// Post: data[length@entry] == item && // length == length@entry + 1{

data[length] = item; length++;

}

12

Page 13: Chapter 13-1 Applied Arrays:  Lists and Strings

13

Assertions

Abstract assertions (located in the specification file): written in terms that are meaningful to the user of the ADT

Implementation assertions (located in the implementation file): more precise by referring directly to data structures and algorithms

Page 14: Chapter 13-1 Applied Arrays:  Lists and Strings

Before Inserting 64 into anUnsorted List

length 3

data [0] 15

[1] 39

[2] -90

[3] .

.

[MAX_LENGTH-1]

The item willbe placed intothe length location,and length will beincremented

14

item 64

Page 15: Chapter 13-1 Applied Arrays:  Lists and Strings

After Inserting 64 into anUnsorted List

length 4

data [0] 15

[1] 39

[2] -90

[3] 64 .

.

[MAX_LENGTH-1]

The item willbe placed intothe length location,and length will beincremented

15

item 64

Page 16: Chapter 13-1 Applied Arrays:  Lists and Strings

16

bool List::IsEmpty () const// Post: Return value is true if length is equal// to zero and false otherwise{

return (length == 0);}

bool List::IsPresent( /* in */ ItemType item) const // Searches the list for item, reporting whether found// Post: Function value is true, if item is in // data[0 . . length-1] and is false otherwise{ int index = 0;

while (index < length && item != data[index]) Index++;

return (index < length);}

16

Page 17: Chapter 13-1 Applied Arrays:  Lists and Strings

17

void List::Delete ( /* in */ ItemType item) // Pre: length > 0 && item is assigned// Post: IF item is in data array at entry// First occurrence of item is no longer in array// && length == length@entry - 1// ELSE// length and data array are unchanged

{ int index = 0;

while (index < length && item != data[index]) index++;

// IF item found, move last element into // item’s place if (index < length) { data[index] = data[length - 1];

length--; }} 17

Page 18: Chapter 13-1 Applied Arrays:  Lists and Strings

Deleting 39 from anUnsorted List

index: 0

39 hasnot been matched

18

item 39

length 4

data [0] 15

[1] 39

[2] -90

[3] 64 .

.

[MAX_LENGTH-1]

Page 19: Chapter 13-1 Applied Arrays:  Lists and Strings

Deleting 39 from anUnsorted List

index: 1

19

item 39

length 4

data [0] 15

[1] 39

[2] -90

[3] 64 .

.

[MAX_LENGTH-1]

39 hasbeen matched

Page 20: Chapter 13-1 Applied Arrays:  Lists and Strings

Deleting 39 from anUnsorted List

index: 1

20

item 39

length 4

data [0] 15

[1] 64

[2] -90

[3] 64 .

.

[MAX_LENGTH-1]

Placed copy oflast list elementinto the position where 39 was before

Page 21: Chapter 13-1 Applied Arrays:  Lists and Strings

Deleting 39 from anUnsorted List

index: 1

21

item 39

length 3

data [0] 15

[1] 64

[2] -90

[3] 64 .

.

[MAX_LENGTH-1]

Decremented length

Page 22: Chapter 13-1 Applied Arrays:  Lists and Strings

22

What should currentPos be initialized to in order to access the first item?

void List::Reset()// Post: currentPos has been initialized.{ currentPos = 0;}

22

Preparing for Iteration

Page 23: Chapter 13-1 Applied Arrays:  Lists and Strings

23

Iteration Operator

ItemType GetNextItem ()// Pre: No transformer has been executed since last call// Post:Return value is currentPos@entry// Current position has been updated// If last item returned, next call returns first item{ ItemType item; item = data[currentPos]; if (currentPos == length - 1) currentPos = 0; else currentPos++; return item; }

Page 24: Chapter 13-1 Applied Arrays:  Lists and Strings

24

Reset

currentPos: 0

24

item ?

length 4

data [0] 15

[1] 64

[2] -90

[3] 64 .

.

[MAX_LENGTH-1]

Page 25: Chapter 13-1 Applied Arrays:  Lists and Strings

25

GetNextItem

currentPos: 1

25

item 15

length 4

data [0] 15

[1] 64

[2] -90

[3] 64 .

.

[MAX_LENGTH-1]

currentPos is incrementeditem is returned

Page 26: Chapter 13-1 Applied Arrays:  Lists and Strings

26

The End of Chapter 13 Part 1


Recommended