47
Chapter 8 Lists CS 302 - Data Structures Mehmet H Gunes dified from authors’ slides

Chapter 8 Lists CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Embed Size (px)

Citation preview

Chapter 8

Lists

CS 302 - Data StructuresMehmet H Gunes

Modified from authors’ slides

Contents

• Specifying the ADT List • Using the List Operations • An Interface Template for the ADT List

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Specifying the ADT List

• A grocery list

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

You reference listitems by their

position

ADT List Operations

• Test whether a list is empty.• Get number of entries on a list.• Insert entry at given position on list.• Remove entry at given position from list.• Remove all entries from list.• Look at (get) entry at given position on list.• Replace (set) entry at given position on list.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

ADT List Operations

• UML diagram for the ADT list

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Abstract Data Type: LIST

• Data : A finite number of objects– Not necessarily distinct– Having the same data type – Ordered by their positions determined by client.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Abstract Data Type: LIST

• Operations– isEmpty()– getLength()– insert(newPosition, newEntry)– remove(position)– clear()– getEntry(position)– setEntry(position, newEntry)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Axioms for the ADT List• (new List()).isEmpty() = true

• (new List()).getLength() = 0

• aList.getLength() = (aList.insert(i, x)). getLength() - 1

• aList.getLength() = (aList.remove(i)). getLength() + 1

• (aList.insert(i, item)).isEmpty() = false

• (new List()).remove(i) = false

• (aList.insert(i, x)).remove(i) = aList

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Axioms for the ADT List• (new List()).getEntry(i) = error

• (aList.insert(i, x)).getEntry(i) = x

• aList.getEntry(i) = (aList.insert(i, x)). getEntry(i + 1)

• aList.getEntry(i + 1) = (aList.remove(i)). getEntry(i)

• (new List()).setEntry(i, x) = error

• (aList.setEntry(i, x)).getEntry(i) = x

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Using the List Operations

• Displaying the items on a list independent of the implementation

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Using the List Operations

• Replacing an item.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Using the List Operations

• Pseudocode statements place names in an alphabetical list

• View Interface Template for the ADT List,Listing 8-1

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

List Implementations

Chapter 9

Contents

• An Array-Based Implementation of the ADT List

• A Link-Based Implementation of the ADT List• Comparing Implementations

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Array-Based Implementation of ADT List

• Recall list operations in UML form

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Header File

• View header file for the class ArrayList, Listing 9-1

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

An array-based implementation of the ADT list

The Implementation File

• Constructor

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Implementation File

• isEmpty tests whether itemCount is zero

• getLength simply returns the value of itemCount :

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Implementation File

• Definition of the method insert

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Implementation File

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shifting items for insertion: (a) the list before the insertion; (b) copy items to produce room at position 3; (c) the result

The Implementation File

• The method getEntry.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Implementation File

• Testing core group of methods

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Implementation File

• The method setEntry

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Implementation File

• The definition of remove

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Implementation File

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

(a) Deletion can cause a gap; (b) shift items to prevent a gap at position 3; (c) the result

The Implementation File

• The method clear.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A Link-Based Implementation of the ADT List

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A link-based implementation of the ADT list

C++ Data Structures Nell Dale

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

Complexities• the order of the operation that determines if an item is in

– a list in a sorted, array-based implementation– a list in an unsorted, array-based implementation– a list in a sorted, linked implementation– a list in an unsorted, linked implementation

O(log N)

O(N)

O(N)

O(N)

Allocation of memory

STATIC ALLOCATION

Static allocation is the allocation of memory space at compile time.

DYNAMIC ALLOCATION

Dynamic allocation is the allocation of memory space at run time by using operator new.

31

3 Kinds of Program Data

• STATIC DATA: memory allocation exists throughout execution of program. static long SeedValue;

• AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function.

• DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete

32

Dynamic Array Allocation

char *ptr; // ptr is a pointer variable that // can hold the address of a char

ptr = new char[ 5 ]; // dynamically, during run time, allocates

// memory for 5 characters and places into // the contents of ptr their beginning address

ptr

6000

6000

33

Dynamic Array Allocation

char *ptr ;

ptr = new char[ 5 ];

strcpy( ptr, “Bye” );

ptr[ 1 ] = ‘u’; // a pointer can be subscripted

std::cout << ptr[ 2] ;

ptr

6000

6000 ‘B’ ‘y’ ‘e’ ‘\0’ ‘u’

34

InsertItem algorithm for Sorted Linked List

• Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location.

• Obtain a node for insertion and place item in it.

• Insert the node by adjusting pointers.

• Increment length.

35

The Inchworm Effect

36

Why is a destructor needed?When a local list variable goes out of scope, the

memory space for data member listPtr is deallocated.

But the nodes to which listPtr points are not deallocated.

A class destructor is used to deallocate the dynamic memory pointed to by the data member.

37

What is a Circular Linked List?

• A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

External Pointer to the Last Node

What is a Doubly Linked List?

• A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

Linking the New Node into the List

Deleting from a Doubly Linked List

What are the advantages of a circular doubly linked list?

What are Header and Trailer Nodes?• A Header Node is a node at the beginning of a list that

contains a key value smaller than any possible key.• A Trailer Node is a node at the end of a list that contains a

key larger than any possible key.• Both header and trailer are placeholding nodes used to

simplify list processing.

A linked list in static storage ?

A Linked List as an Array of Records

A Sorted list Stored in an Array of Nodes

An Array with Linked List of Values and Free Space

An Array with Three Lists (Including the Free List)