Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 21 Data Structures.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 21 Data Structures."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 21 Data Structures

2  2006 Pearson Education, Inc. All rights reserved. 2 Much that I bound, I could not free; Much that I freed returned to me. — Lee Wilson Dodd ‘Will you walk a little faster?’ said a whiting to a snail, ‘There’s a porpoise close behind us, and he’s treading on my tail.’ — Lewis Carroll There is always room at the top. — Daniel Webster Push on — keep moving. — Thomas Morton I’ll turn over a new leaf. — Miguel de Cervantes

3  2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To form linked data structures using pointers, self- referential classes and recursion.  To create and manipulate dynamic data structures such as linked lists, queues, stacks and binary trees.  To use binary search trees for high-speed searching and sorting.  To understand various important applications of linked data structures.  To understand how to create reusable data structures with class templates, inheritance and composition.

4  2006 Pearson Education, Inc. All rights reserved. 4 21.1 Introduction 21.2 Self-Referential Classes 21.3 Dynamic Memory Allocation and Data Structures 21.4 Linked Lists 21.5 Stacks 21.6 Queues 21.7 Trees 21.8 Wrap-Up

5  2006 Pearson Education, Inc. All rights reserved. 5 Fig. 21.1 | Two self-referential class objects linked together.

6  2006 Pearson Education, Inc. All rights reserved. 6 Common Programming Error 21.1 Not setting the link in the last node of a linked data structure to null (0) is a (possibly fatal) logic error.

7  2006 Pearson Education, Inc. All rights reserved. 7 Performance Tip 21.1 An array can be declared to contain more elements than the number of items expected, but this can waste memory. Linked lists can provide better memory utilization in these situations. Linked lists allow the program to adapt at runtime. Note that class template vector (introduced in Section 7.11) implements a dynamically resizable array- based data structure.

8  2006 Pearson Education, Inc. All rights reserved. 8 Performance Tip 21.2 Insertion and deletion in a sorted array can be time consuming—all the elements following the inserted or deleted element must be shifted appropriately. A linked list allows efficient insertion operations anywhere in the list.

9  2006 Pearson Education, Inc. All rights reserved. 9 Performance Tip 21.3 The elements of an array are stored contiguously in memory. This allows immediate access to any array element, because the address of any element can be calculated directly based on its position relative to the beginning of the array. Linked lists do not afford such immediate “direct access” to their elements. So accessing individual elements in a linked list can be considerably more expensive than accessing individual elements in an array. The selection of a data structure is typically based on the performance of specific operations used by a program and the order in which the data items are maintained in the data structure. For example, it is typically more efficient to insert an item in a sorted linked list than a sorted array.

10  2006 Pearson Education, Inc. All rights reserved. 10 Performance Tip 21.4 Using dynamic memory allocation (instead of fixed-size arrays) for data structures that grow and shrink at execution time can save memory. Keep in mind, however, that pointers occupy space and that dynamic memory allocation incurs the overhead of function calls.

11  2006 Pearson Education, Inc. All rights reserved. 11 Fig. 21.2 | A graphical representation of a list.

12  2006 Pearson Education, Inc. All rights reserved. 12 Outline Listnode.h (1 of 2)

13  2006 Pearson Education, Inc. All rights reserved. 13 Outline Listnode.h (2 of 2)

14  2006 Pearson Education, Inc. All rights reserved. 14 Outline List.h (1 of 7)

15  2006 Pearson Education, Inc. All rights reserved. 15 Outline List.h (2 of 7)

16  2006 Pearson Education, Inc. All rights reserved. 16 Outline List.h (3 of 7)

17  2006 Pearson Education, Inc. All rights reserved. 17 Outline List.h (4 of 7)

18  2006 Pearson Education, Inc. All rights reserved. 18 Outline List.h (5 of 7)

19  2006 Pearson Education, Inc. All rights reserved. 19 Outline List.h (6 of 7)

20  2006 Pearson Education, Inc. All rights reserved. 20 Outline List.h (7 of 7)

21  2006 Pearson Education, Inc. All rights reserved. 21 Outline Fig21_05.cpp (1 of 6)

22  2006 Pearson Education, Inc. All rights reserved. 22 Outline Fig21_05.cpp (2 of 6)

23  2006 Pearson Education, Inc. All rights reserved. 23 Outline Fig21_05.cpp (3 of 6)

24  2006 Pearson Education, Inc. All rights reserved. 24 Outline Fig21_05.cpp (4 of 6)

25  2006 Pearson Education, Inc. All rights reserved. 25 Outline Fig21_05.cpp (5 of 6)

26  2006 Pearson Education, Inc. All rights reserved. 26 Outline Fig21_05.cpp (6 of 6)

27  2006 Pearson Education, Inc. All rights reserved. 27 Error-Prevention Tip 21.1 Assign null (0) to the link member of a new node. Pointers should be initialized before they are used.

28  2006 Pearson Education, Inc. All rights reserved. 28 Fig. 21.6 | Operation insertAtFront represented graphically.

29  2006 Pearson Education, Inc. All rights reserved. 29 Fig. 21.7 | Operation insertAtBack represented graphically.

30  2006 Pearson Education, Inc. All rights reserved. 30 Fig. 21.8 | Operation removeFromFront represented graphically.

31  2006 Pearson Education, Inc. All rights reserved. 31 Fig. 21.9 | Operation removeFromBack represented graphically.

32  2006 Pearson Education, Inc. All rights reserved. 32 Fig. 21.10 | Circular, singly linked list.

33  2006 Pearson Education, Inc. All rights reserved. 33 Fig. 21.11 | Doubly linked list.

34  2006 Pearson Education, Inc. All rights reserved. 34 Fig. 21.12 | Circular, doubly linked list.

35  2006 Pearson Education, Inc. All rights reserved. 35 Outline Stack.h (1 of 2)

36  2006 Pearson Education, Inc. All rights reserved. 36 Outline Stack.h (2 of 2)

37  2006 Pearson Education, Inc. All rights reserved. 37 Outline Fig21_14.cpp (1 of 3)

38  2006 Pearson Education, Inc. All rights reserved. 38 Outline Fig21_14.cpp (2 of 3)

39  2006 Pearson Education, Inc. All rights reserved. 39 Outline Fig21_14.cpp (3 of 3)

40  2006 Pearson Education, Inc. All rights reserved. 40 Outline Stack composition.h (1 of 2)

41  2006 Pearson Education, Inc. All rights reserved. 41 Outline Stack composition.h (2 of 2)

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline Queue.h (1 of 2)

43  2006 Pearson Education, Inc. All rights reserved. 43 Outline Queue.h (2 of 2)

44  2006 Pearson Education, Inc. All rights reserved. 44 Outline Fig21_17.cpp (1 of 3)

45  2006 Pearson Education, Inc. All rights reserved. 45 Outline Fig21_17.cpp (2 of 3)

46  2006 Pearson Education, Inc. All rights reserved. 46 Outline Fig21_17.cpp (2 of 3)

47  2006 Pearson Education, Inc. All rights reserved. 47 Fig. 21.18 | A graphical representation of a binary tree.

48  2006 Pearson Education, Inc. All rights reserved. 48 Fig. 21.19 | A binary search tree.

49  2006 Pearson Education, Inc. All rights reserved. 49 Outline Treenode.h (1 of 2)

50  2006 Pearson Education, Inc. All rights reserved. 50 Outline Treenode.h (2 of 2)

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline Tree.h (1 of 6)

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline Tree.h (2 of 6)

53  2006 Pearson Education, Inc. All rights reserved. 53 Outline Tree.h (3 of 6)

54  2006 Pearson Education, Inc. All rights reserved. 54 Outline Tree.h (4 of 6)

55  2006 Pearson Education, Inc. All rights reserved. 55 Outline Tree.h (5 of 6)

56  2006 Pearson Education, Inc. All rights reserved. 56 Outline Tree.h (6 of 6)

57  2006 Pearson Education, Inc. All rights reserved. 57 Outline Fig21_22.cpp (1 of 3)

58  2006 Pearson Education, Inc. All rights reserved. 58 Outline Fig21_22.cpp (2 of 3)

59  2006 Pearson Education, Inc. All rights reserved. 59 Outline Fig21_22.cpp (3 of 3)

60  2006 Pearson Education, Inc. All rights reserved. 60 Fig. 21.23 | A binary search tree.


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 21 Data Structures."

Similar presentations


Ads by Google