CHAP13 - 1 Chapter 13 Applied Arrays: Lists and Strings...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington 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 3 What is a List? A list is a varying-length, linear collection of homogeneous elements. linear means each list element (except the first) has a unique predecessor , and each element (except the last) has a unique successor 3 Basic Kinds of ADT Operations Constructor-- creates a new instance (object) of an ADT Transformer-- changes the state of one or more of the data values of an instance Observer-- allows us to observe the state of one or more of the data values of an instance without changing them 4 ADT List Operations Transformers Insert Delete SelSort Observers IsEmpty IsFull Length IsPresent Print change state observe state 5 ADT Unsorted List Data Components length data[ 0 . . MAX_LENGTH -1 ] number of elements in list array of list elements 6 7 Array-based class List IsFull Length SelSort IsPresent Delete IsEmpty Insert Print Private data: length data [ 0 ] [ 1 ] [ 2 ] [MAX_LENGTH-1] 8 // 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 Print ( ) ; private : // private data members int length ; // number of values currently stored ItemType data[MAX_LENGTH] ; } ; 8 9 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 or alphabetically. 10 // IMPLEMENTATION FILE ARRAY-BASED LIST ( list.cpp ) #include list.h #include <iostream> using namespace std; int List :: Length ( ) const // Post: Function value == length { return length ; } bool List :: IsFull ( ) const // Post: Function value == true, if list == MAX_LENGTH // == false, otherwise { return ( length == MAX_LENGTH ) ; } 10 11 List :: List ( ) // Constructor // Post: length == { 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++ ; } 11 Before Inserting 64 into an Unsorted List length 3 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ] ....
View Full Document

Page1 / 76

CHAP13 - 1 Chapter 13 Applied Arrays: Lists and Strings...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online