nyhoff, adts, data structures and problem solving with c++, second edition, 2005 pearson education,...

43
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edi tion, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909- 3 1 More Linking Up with Linked Lists Chapter 11

Upload: samantha-fisher

Post on 18-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Objectives Survey common variants of linked lists and why they are used Study in detail an application of linked lists to implement sparse polynomials Describe doubly-linked lists and how they are used to implement C++ STL list container Build a class that makes it possible to do arithmetic with large integers Look briefly at some other applications of multiply- linked lists

TRANSCRIPT

Page 1: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

1

More Linking Up with Linked Lists

Chapter 11

Page 2: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

2

Chapter Contents

11.1 Some Variants of Singly-Linked Lists11.2 Linked Implementation of Sparse

Polynomials11.3 Doubly-Linked Lists and the Standard C+

+ list11.4 Case Study: Larger-Integer Arithmetic11.5 Other Multiply-Linked Lists

Page 3: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

3

Chapter Objectives• Survey common variants of linked lists and why

they are used• Study in detail an application of linked lists to

implement sparse polynomials• Describe doubly-linked lists and how they are used

to implement C++ STL list container• Build a class that makes it possible to do arithmetic

with large integers• Look briefly at some other applications of multiply-

linked lists

Page 4: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

4

Linked Lists with Head Nodes

• Consider linked lists from Chapter 6– First node is different from others– Has no predecessor

• Thus insertions and deletions must consider two cases– First node or not first node– The algorithm is different for each

Page 5: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

5

Linked Lists with Head Nodes

• Dual algorithms can be reduced to one– Create a "dummy" head node– Serves as predecessor holding actual first

element

• Thus even an empty list has a head node

Page 6: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

6

Linked Lists with Head Nodes

• For insertion at beginning of list– Head node is predecessor for new nodenewptr->next = predptr->next;predptr->next = newptr;

Page 7: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

7

Linked Lists with Head Nodes

• For deleting first element from a list with a head node– Head node is the predecessorpredptr->next = ptr->next;delete ptr;

Page 8: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

8

Circular Linked Lists• Set the link in last node to point to first node

– Each node now has both predecessor and successor

– Insertions, deletions now easier• Special consideration required

for insertion to empty list, deletion from single item list

Page 9: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

9

Circular Linked Lists

• Traversal algorithm must be adjustedif (first != 0) // list not empty{ ptr = first;

do { // process ptr->data

ptr = ptr->next; } while (ptr != first); }

• A do-while loop must be used instead of a while loop– Why is this required?

Page 10: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

10

Linked Implementation of Sparse Polynomials

• Consider a polynomial of degree n– Can be represented by a list

• For a sparse polynomial this is not efficient

Page 11: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

11

Linked Implementation of Sparse Polynomials

• We could represent a polynomial by a list of ordered pairs– { (coef, exponent) … }

• Fixed capacity ofarray still problematic– Wasted space for

sparse polynomial

Page 12: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

12

Linked Implementation of Sparse Polynomials

• Linked list of these ordered pairs provides an appropriate solution– Each node has form shown

• Now whether sparse or well populated, the polynomial is represented efficiently

Page 13: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

13

Linked Implementation of Sparse Polynomials

• Note start of Polynomial class template– Type parameter CoefType–Term and Node are inner classes

• Addition operator– Adds coefficients of like degrees– Must traverse the two addend polynomials– Requires temporary pointers for each polynomial

(the addends and the resulting sum)

Page 14: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

14

Addition Operator

• Requires temporary pointers for each polynomial (the addends and the resulting sum)

Page 15: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

15

Addition Operator• As traversal takes place

– Compare exponents– If different, node with smaller exponent and its coefficient

attached to result polynomial– If exponents same, coefficients added, new

corresponding node attached to result polynomial

View source code

Page 16: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

16

Doubly-Linked Lists

• Bidirectional lists– Nodes have data part,

forward and backward link• Facilitates both forward and backward

traversal– Requires pointers to both first and last nodes

Page 17: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

17

Doubly-Linked Lists

• To insert a new node– Set forward and backward links to point to

predecessor and successor

– Then reset forward link of predecessor, backward link of successor

Page 18: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

18

Doubly-Linked Lists

• To delete a node– Reset forward link of predecessor, backward link

of successor– Then delete removed node

Page 19: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

19

The STL list<T> Class Template

• A sequential container – Optimized for insertion and erasure at arbitrary

points in the sequence.– Implemented as a circular doubly-linked list with

head node.

Page 20: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

20

Comparing List<t> With Other Containers

• Note : list<T> does not support direct access – does not have the subscript operator [ ].

Property Array vector<T> deque<T> list<T>Direct/random access ([]) (exclnt)(good)XSequential access

Insert/delete at front (poor)

Insert/delete in middle

Insert/delete at end

Overhead lowest low low/mediumhigh

Page 21: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

21

list<t> Iterators• list<T>'s iterator is "weaker" than that for vector<T>. vector<T>: random access iterators list<T>: bidirectional iterators

• Operations in common ++ Move iterator to next element

(like ptr = ptr-> next) -- Move iterator to preceding element

(like ptr = ptr-> prev) * dereferencing operator

(like ptr-> data)

Page 22: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

22

list<t> Iterators• Operators in common

= assignment (for same type iterators) it1 = it2 makes it1 positioned at

same element as it2 == and !=

(for same type iterators) checks whether iterators are

positioned at the same element See basic list operations,

Table 11-2, pg 621 View demonstration of list operations, Fig. 11-1

Page 23: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

23

Example: Internet Addresses

• Consider a program that stores IP addresses of users who make a connection with a certain computer– We store the connections in an AddressCounter object

– Tracks unique IP addresses and how many times that IP connected

• View source code, Fig. 11.2– Note uses of STL list and operators

Page 24: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

24

The STL list<T> Class Template

Node structure

struct list_node{ pointer next,

prev;T data; }

Page 25: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

25

The STL list<T> Class Template

• But it's allo/deallo-cation scheme is complex– Does not simply using new and delete

operations.• Using the heap manager is inefficient for

large numbers of allo/deallo-cations– Thus it does it's own memory management.

Page 26: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

26

The STL list<T> Memory Management

When a node is allocated1. If there is a node on the free list, allocate it.

• This is maintained as a linked stack

2. If the free list is empty:a) Call the heap manager to allocate a block of

memory (a "buffer", typically 4K)b) Carve it up into pieces of size required for a

node of a list<T>.

Page 27: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

27

The STL list<T> Memory Management

• When a node is deallocated– Push it onto the free list.

• When all lists of this type T have been destroyed– Return it to the heap

Page 28: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

28

Case Study: Large-Integer Arithmetic

• Recall that numeric representation of numbers in computer memory places limits on their size– 32 bit integers, two's complement max

2147483647• We will design a BigInt class

– Process integers of any size– For simplicity, nonnegative integers only

Page 29: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

29

BigInt Design

• First step : select a storage structure– We choose a linked list– Each node sores a block of up to 3 consecutive

digits

– Doubly linked list for traversing in both directions

Page 30: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

30

BigInt Design

• Input in blocks of 3 integers, separated by spaces– As each new block entered, node added at end

• Output is traversal, left to right

Page 31: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

31

BigInt Design

• Addition adds the groupings right to left– Keeping track of carry digits

Page 32: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

32

BigInt Implementation

• Standard list type provides all the tools we need

• Note class declaration, Fig. 11.3A• View class definition, Fig. 11.3B• Driver program to demonstrate use of the

class, Fig 11.4

Page 33: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

33

Multiply-Ordered Lists

• Ordered linked list– Nodes arranged so data items are in

ascending/descending order• Straightforward when based on one data

field– However, sometimes necessary to maintain links

with a different ordering• Possible solution

– Separate ordered linked lists – but wastes space

Page 34: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

34

Multiply-Ordered Lists

• Better approach– Single list– Multiple links

Page 35: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

35

Sparse Matrices• Usual storage is 2D array or 2D vector• If only a few nonzero entries

– Can waste space• Stored more efficiently with linked structure

– Similar to sparse polynomials– Each row is a linked list– Store only nonzero entries for the row

Page 36: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

36

• For

we represent with

Sparse Matrices

A =

Page 37: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

37

Sparse Matrices

• This still may waste space– Consider if many rows were all zeros

• Alternative implementation– Single linked list– Each node has row, column,

entry, link• Resulting list

Page 38: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

38

Sparse Matrices

• However … this loses direct access to rows• Could replace array of pointers with

– Linked list of row head nodes– Each contains pointer to non empty row list

Page 39: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

39

Sparse Matrices

• If columnwise processing is desired– Use orthogonal list– Each node stores row, column, value, pointer to

row successor, pointer to column successor

Page 40: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

40

Sparse Matrices• Note the resulting

representation of the matrix

A =

Page 41: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

41

Generalized Lists

• Examples so far have had atomic elements– The nodes are not themselves lists

• Consider a linked list of strings– The strings themselves can be linked lists of

charactersThis is an

example of a generalized list

Page 42: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

42

Generalized Lists

• Commonly represented as linked lists where– Nodes have a tag field along with data and link

• Tag used to indicate whether data field holds– Atom– Pointer

to a list

Page 43: Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,  2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 More Linking

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

43

Generalized Lists

• Lists can be shared– To represent

(2, (4,6), (4,6))• For polynomials in two variables

P(x,y) = 3 + 7x + 14y2 + 25y7 – 7x2y7 + 18x6y7