COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs...

53
COP3530 Data Structures 1 Data Abstraction: The Walls • Abstract Data Types (ADT) • Specifying ADTs – The ADT List – The ADT Sorted List – Designing an ADT • Implementation of ADTs – C++ classes – Array based implementation of ADT List.

description

COP3530 Data Structures302 Walls Imagine a wall separating task Q and T. The user task Q needs to know what T does. Q need not know how T does it. Wall prevents Q’s method of solution to depend on the task T’s method of solution. If T changes the implementation, Q does not have to be changed. If T’s specification changes, Q needs to change.

Transcript of COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs...

Page 1: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 1

Data Abstraction: The Walls• Abstract Data Types (ADT)• Specifying ADTs

– The ADT List– The ADT Sorted List– Designing an ADT

• Implementation of ADTs– C++ classes– Array based implementation of ADT List.

Page 2: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 2

Abstract Data Type• A moduler program is easier to read, write,

and modify.• Focus on “what” a module does. Not “how”• Build loosely coupled functions.• Functional Abstraction.• Information Hiding. What to hide?• Not only hide, but make it inaccessible.• Hide implementation details from others.

Page 3: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 3

Walls• Imagine a wall separating task Q and T.• The user task Q needs to know what T does.• Q need not know how T does it.• Wall prevents Q’s method of solution to

depend on the task T’s method of solution.• If T changes the implementation, Q does not

have to be changed.• If T’s specification changes, Q needs to

change.

Page 4: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 4

Walls (contd)

Task T

First Implementation

Task T

Second Implementation

Task Q

Client of T

Page 5: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 5

ADT Operations

Program

that uses

function S

Implementation

of

function S

Request op

Result of Op

Page 6: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 6

What is ADT?• Solution to problems often require operations

on data.• Broad categories of operations:

– Add data to a data collection– Remove data from a data collection– Make a query about the data collection

• Details vary from application to application.• Not all problems require all types of

operations.

Page 7: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 7

What is ADT? (contd)• Data Abstraction

– “What” opeations you can do with data.– Not “how” to do them.

• ADT = Collection of Data + Operations• Often, the data collection needs to maintain

certain integrity constraints.• Data Structures are useful to implement

ADTs.

Page 8: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 8

Data Structures• Data structures are constructs that you can

define in a programming language.• Arrays, Structures are data structures.• You can combine basic data structures to

make more complex data structures.• Data Structures are part of ADT

implementation.• You should also focus on efficiency of

operations when choosing data structures.

Page 9: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 9

Data Structure: An example.• Suppose you want to store both names and

salaries of a group of employees.

const MAX_EMPLOYEES = 500;string empNames[MAX_EMPLOYEES];double empSalary[MAX_EMPLOYEES];

string class takes care of memory to accommodate any size string.

Page 10: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 10

Data Structure: An exampleAlternative way for the previous example:const MAX_EMPLOYEES = 500;typedef struct{ string name; double salary;} Employee;Employee employees[MAX_EMPLOYEE];• If language doesn’t support, we build new ADTs.

Page 11: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 11

ADT vs Data Structure• ADT is like a ice dispenser machine.• The dispenser supports operations like ADT

– Chilled water– Crushed ice– ice cubes

• Water and ice are like data.• The internal design of the dispenser is like

the internal design of the ADT. We need data structures to build ADT.

Page 12: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 12

Building ADT• User of ADT wants operations to be fast.• Builder of ADT wants implementation to be

simple.• User will look for an ADT that meets their

requirement just like a customer will shop for a refrigerator that is convenient.

• User will look at the ADT interface for suitability not its implementation details.

Page 13: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 13

Wall for the ADT

ProgramData

Structure

Add

Remove

Find

Display

Interface

Request

Response

Wall of ADT operations

Page 14: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 14

Specifying an ADT• Consider a list of grocery items in 1 column• Where do you add a new item?

– At the beginning– At the end– Anywhere in the middle.

• There is a sequence for the items.• Each item has a predecessor except first.• Each item has a successor except the last.

Page 15: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 15

Specifying an ADT (contd)• How do you specify the Add operation?

– Add newitem at position pos.– pos = 1 new item is first item– pos can be between 1 and (# of items in list + 1)

• How do you specify remove?– Remove item at position pos.

• How do you retrieve an item?– Retrieve item at position pos.

• Find the length of the list.

Page 16: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 16

List ADT• Create an empty list.• Destroy a list.• Check if the list is empty.• Find the number of items in the list.• Insert an item at a given position.• Delete an item at a given position.• Retrieve (look) an item at a given position.

Page 17: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 17

List ADT Operations// Create an empty list.CreateList()

// Destroy a list.DestroyList()

// Return the # of items in list.ListLength()

Page 18: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 18

List ADT Operations(contd)// Insert a new item at position// newPos of a list, if// 1 <= newPos <= ListLength()+1// The new item becomes the item// at position newPos. Success // indicates whether the operation// was successful or not.ListInsert(newPos, newItem, success)

Page 19: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 19

List ADT Operations(contd)// Delete the item at position pos// if 1 <= pos <= ListLength().// The existining items are// renumbered accordingly.// success indicates whether the// operation was successful or not.ListDelete(pos, success)

Page 20: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 20

List ADT Operations(contd)// Retrieve the item at position// pos of a list into dataItem, if// 1 <= pos <= ListLength(). The // list is left unchanged. success// indicates whether the operation// was successful or not.

ListRetrieve(pos, dataItem, success)

Page 21: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 21

Using List ADTmyList.CreateList()myList.ListInsert(1, milk, success)

myList.ListInsert(2, eggs, success)

myList.ListInsert(3, butter, success)

myList.ListDelete(2, success)myList.ListLength()

Page 22: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 22

ADT Contract• Client programs should only rely on ADT

specification, not on its implementation.• As long as client follows the rules, any

change in the implementation of the ADT will not affect the client program.

• The specification of ADT is the contract between the ADT and its client.

• Client can use the ADT based only on the contract.

Page 23: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 23

List ADT Usage• Client wants to display all items in a list.DisplayList(list)// Display the items in list.for (pos = 1..list.ListLength()){ list.ListRetrive(pos, dataItem, success) Display dataItem}

Page 24: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 24

List ADT UsageReplace(list, pos, newItem, success)// Replaces item at pos in list by // newItem. success indicates whether// the operation was successful or notlist.ListDelete(pos, success)if (success){ list.ListInsert(pos, newItem, success)}

Page 25: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 25

Contract Advantages• Client does not need to get distracted about

the ADT implementation details.• Client can concentrate on ADT usage.• Improvements in implementation of ADT

affects only the ADT code, not client code.• ADT implementation is isolated from client.• contract binds the ADT implementers.

Page 26: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 26

Sorted List ADT• Consider a list of items sorted by name.• How are the list operations affected now?

– We no longer need the position for insert.– position is implicit except for retrieve.

• The ADT operations must make sure that the list always remains sorted.

• For example, insert need to add the new item at the appropriate place.

Page 27: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 27

Sorted List ADT Specification// Insert newItem into its proper// sorted position in a sorted// list. success indicates whether// the operation was successful// or not.

SortedListInsert(newItem, success)

Page 28: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 28

Designing an ADT• Consider an application where you want to

print all holidays in a year.• We may need an ADT that deals with dates.• Clearly, we need an operation to get the

first date of a year.• We need to know if a date is holiday.• Given a date, we need to find the next date.• Compare two dates.

Page 29: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 29

ListHolidaysListHolidays(year)// Displays the dates of all// holidays in a given year.date = FirstDay(year);while (IsBefore(date, FirstDay(year+1))){ if (IsHoliday(date)) { write date + “is a holiday” } date = NextDay(date);}

Page 30: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 30

Implementing ADTs• Given the ADT specification, how do we

implement the ADT?• We choose an appropriate data structure.• Verify if all the operations can be

implemented with this data structure.• If efficiency is part of the specification, then

we need to verify that too.• Look at alternative data structures and

choose the best.

Page 31: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 31

Implementing ADTs• Once you choose the data structure, you

should use top-down approach to implement each of the ADT operations.

• As you work through successive level of abstraction, you will break the data structure into smaller pieces.

• Refine until the data structure can be easily implemented in the target language.

Page 32: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 32

Implementing ADTs• Data structure and the implementation of

the operations should be hidden from the client programs.

• In non-object oriented languages, both data structure and the ADT operations are distinct pieces.

• The data structure may not be hidden from the client.

• Clients might violate the wall and access the data structures directly. This may be intentionally or accidentally.

Page 33: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 33

Implementing ADTs• Object-oriented languages have better

support for enforcing the wall of the ADT.• C++ provides a way to prevent the clients

from accessing the ADT data.• Hiding the data and implementation details

is not straight forward but can be done with careful planning.

• The algorithm can be easily hidden by placing all the code in .cc files and giving clients only object or library files.

Page 34: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 34

C++ Classes• OOD deals with a collection of objects and

their relationship.• C++ classes are used to represent these

objects.• Objects provide encapsulation. (Walls)• Object = Data + Operations (or methods).• Object hides its inner details from the

programmers who uses it.• ADT operations are object’s behaviors.• Encapsulation hides implementation details.

Page 35: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 35

Object

Methods

Data

Object’s data

and methods

are

encapsulated

Request

Results

Page 36: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 36

Classes and Instances• Class is a new data type in C++.• variables of this type are called instances.• class has data member as well as function members.

• Every member function has access to all the data members. Thus, they need not be passed as parameters.

• To access the data or function member of a class instance, you qualify the member with the instance using the . notation.

Page 37: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 37

Classes and Instances (contd)• An object is an instance of a class.• By default, all members of a class are private.

• The class designer can designate members as belonging to public category.

• Only public member are accessible to clients.

• Clients are functions or program that created the object.

• All members of a structure are public by default.

Page 38: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 38

Constructor and Destructor• Every ADT has support for Create and

Destroy.• All classes in C++ have 1 or more

constructor functions and 0 or 1 destructor function.

• Constructors are used to initialize the object.

• Constructors are called automatically when an object is created.

• Destructor is called automatically when the object is destroyed. Only one.

Page 39: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 39

Constructors• Name of constructors is same as class name.• Constructors should not have return type. Not

even void.• Constructors can be overloaded.• Default constructor is one with no parameters.• If a class has no constructors, then the

compiler will supply a default constructor which basically does nothing.

• Copy constructor has one parameter whose type is same as the class. const reference.

Page 40: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 40

Constructors (Example)class Sphere{public: // Default Constructor Sphere(); // Copy Constructor Sphere(const Sphere &rhs); // Another constructor Sphere(double initialRadius); :}

Page 41: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 41

Constructors (contd)// Default Constructor called.Sphere sphere1;// Copy Constructor called.Sphere sphere2(sphere1);

• If a class is missing default constructor but has others, you must initialize an object with something when you declare.

Sphere sphere1; // invalid.

Page 42: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 42

Constructor Coding• Assume that Sphere class has variable radius of type double in private section.

Sphere::Sphere(){ radius = 1.0;}Sphere::Sphere(const Sphere &rhs){ radius = rhs.radius;}:: is scope resolution operator.

Page 43: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 43

Constructor Coding (contd)• An alternative better way to initialize data

members of a class is to use the intializer list. The body of the function can be used other computational work, if any. The Initializers are allowed only for constructor functions. Use , for additional variables.

Sphere::Sphere():radius(1.0){}Sphere::Sphere(const Sphere &rhs): radius(rhs.radius){}

Page 44: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 44

Destructor• Destructors are useful to perform cleanup

work when an object is destroyed.• In particular, they are useful to free any

dynamic memory allocated by constructors.• If there is no work to be done when an

object is destroyed, there is no need for the destructor function.

Sphere::~Sphere() // No parameters{}

Page 45: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 45

Compiler Generated Constructors• If a class has NO constructors, compiler

supplies a default constructor that does nothing.

• If a class is missing a copy constructor, the compiler will generate one that simply assigns the data members of one instance to those of the others.

• If a class is missing destructor function, the compiler will supply one. Does nothing.

Page 46: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 46

Implementation• Typically, we place the code for all member

functions of a class and non-member functions related to the class in the source file (.cpp or .cc).

• We can compile this independly of the application program and create the object file.

• The object file can also be placed in a library file.

Page 47: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 47

Sphere Member Function (example)Assume PI is defined in sphere.hdouble Sphere::Area() const{ return 4.0 * PI * radius * radius;}double Sphere::SetRadius(double newRadius){ if (newRadius >= 0) { radius = newRadius; }}Exercise: Read the rest on page 132.

Page 48: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 48

Using Sphere Class (p 133)#include <iostream.h>#include <sphere.h>int main(){ Sphere unitSphere; // default is 1.0 Sphere mySphere(5.1);

unitSphere.DisplayStatistics(); mySphere.SetRadius(4.2); return 0;}

Page 49: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 49

Array-Based ADT List• One way to implement a list is using an

array and an integer to store the length.• Item at Position 1 is stored at index 0.• Item at Position 2 is stored at index 1 etc.• We need to decide on capacity of array.• If array is full, Insert will be unsuccessful.• Item type depends on the application.• Insert and Delete involve shifting of items.

Page 50: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 50

Array-Based ADT List (contd)

5size

0 1 2 3 4 5 C - 1

Array Index

1 2 3 4 5 6

ADT Position

12 3 19 10 7 ? ? ?

Unused entries

Page 51: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 51

Array-Based ADT (contd)

6size

0 1 2 3 4 5 C - 1

Array Index

1 2 3 4 5 6

ADT Position

12 3 34 19 10 7 ? ?

Unused entries

After Insert 34 at Pos 3.

New Item

Page 52: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 52

Some ADT Guideline• Declare member functions that do not

change the data values as const.– Safeguards against accidental mistakes– Needed to call these functions on const objects.

• Keep variables that are to be hidden from clients in the private section of the class.

• Do not depend on compiler generated constructors or destructor.

• Always define default & Copy constructors.

Page 53: COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.

COP3530 Data Structures 53

Exercise 3.1 p142int ListSum(const List &list){ int sum = 0; int length = list.ListLength(); int pos, item; bool success;

for (pos = 1; pos <= length; pos++) { list.ListRetrieve(pos, item, success); sum = sum + item; } return sum;}