chapter 15 lists - bumsoo kim · 15-2 general linear lists a general linear list is a list in which...

Post on 15-Oct-2020

6 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Computer Science: A Structured Programming Approach Using C 1

Objectives

❏ To introduce the basic concepts of linked lists

❏ To introduce the basic concepts of stacks

❏ To introduce the basic concepts of queues

❏ To introduce the basic concepts of tree structures

❏ To introduce the basic concepts of graph structures

Chapter 15

Lists

Computer Science: A Structured Programming Approach Using C 2

FIGURE 15-1 Lists

Computer Science: A Structured Programming Approach Using C 3

15-1 List Implementations

The C language does not provide any list structures or

implementations. When we need them, we must

provide the structures and functions for them.

Traditionally, two data types, arrays and pointers, are

used for their implementation.

Array Implementation

Linked List Implementation

Pointers to Linked Lists

Topics discussed in this section:

Array Implementation

How?

Pros and cons?

Computer Science: A Structured Programming Approach Using C 4

Linked List Implementation

Linked list

An ordered collection of data in which each element contains the location of the next element or elements

A node contains data and links

Linear and non-linear structures

Pros and cons over the array?

Computer Science: A Structured Programming Approach Using C 5

Computer Science: A Structured Programming Approach Using C 6

FIGURE 15-2 Linked Lists

Computer Science: A Structured Programming Approach Using C 7

FIGURE 15-3 Nodes

Computer Science: A Structured Programming Approach Using C 8

FIGURE 15-4 Linked List Node Structures

Computer Science: A Structured Programming Approach Using C 9

15-2 General Linear Lists

A general linear list is a list in which operations, such

as retrievals, insertions, changes, and deletions, can be

done anywhere in the list, that is, at the beginning, in

the middle, or at the end of the list..

Insert a Node

Delete a Node

Locating Data in Linear Lists

Traversing Linear Lists

Building a Linear List

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 10

FIGURE 15-5 Pointer Combinations for Insert

Computer Science: A Structured Programming Approach Using C 11

FIGURE 15-6 Insert Node to Empty List

Computer Science: A Structured Programming Approach Using C 12

FIGURE 15-7 Insert Node at Beginning

Computer Science: A Structured Programming Approach Using C 13

FIGURE 15-8 Insert Node in Middle

Computer Science: A Structured Programming Approach Using C 14

FIGURE 15-9 Insert Node at End

Computer Science: A Structured Programming Approach Using C 15

PROGRAM 15-1 Insert a Node

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 15-1 Insert a Node

Computer Science: A Structured Programming Approach Using C 17

FIGURE 15-10 Delete First Node

Computer Science: A Structured Programming Approach Using C 18

FIGURE 15-11 Delete—General Case

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 15-2 Delete a Node

Computer Science: A Structured Programming Approach Using C 20

Table 15-1 Linear List Search Results

Computer Science: A Structured Programming Approach Using C 21

FIGURE 15-12 Search Results

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 15-3 Search Linear List

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 15-3 Search Linear List

Computer Science: A Structured Programming Approach Using C 24

FIGURE 15-13 Linear List Traversal

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 15-4 Print Linear List

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 15-5 Average Linear List

Computer Science: A Structured Programming Approach Using C 27

FIGURE 15-14 Design for Inserting a Node in a List

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 15-6 Build List

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 15-6 Build List

Computer Science: A Structured Programming Approach Using C 30

FIGURE 15-15 Design for Remove Node

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 15-7 Delete Key

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 15-7 Delete Key

Computer Science: A Structured Programming Approach Using C 33

FIGURE 15-16 Link List Test Driver

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 15-8 Test Driver for Link List

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 15-8 Test Driver for Link List

Computer Science: A Structured Programming Approach Using C 36

PROGRAM 15-8 Test Driver for Link List

PROGRAM 15-8 Test Driver for Link List

Computer Science: A Structured Programming Approach Using C 37

PROGRAM 15-8 Test Driver for Link List

Computer Science: A Structured Programming Approach Using C 38

Computer Science: A Structured Programming Approach Using C 39

15-3 Stacks

A stack is a linear list in which all additions and

deletions are restricted to one end, called the top.

Stacks are known as the last in–first out (LIFO) data

structure.

Stack Structures

Stack Algorithms

Stack Demonstration

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 40

A stack is a last in–first out (LIFO) data structure in which

all insertions and deletions are restricted

to one end, called the top.

Note

Computer Science: A Structured Programming Approach Using C 41

FIGURE 15-17 Stack

Computer Science: A Structured Programming Approach Using C 42

FIGURE 15-18 Conceptual and Physical Stack Implementations

Computer Science: A Structured Programming Approach Using C 43

FIGURE 15-19 Stack Data Structure

Computer Science: A Structured Programming Approach Using C 44

FIGURE 15-20 Streams

Computer Science: A Structured Programming Approach Using C 45

PROGRAM 15-9 Push Stack

Computer Science: A Structured Programming Approach Using C 46

PROGRAM 15-9 Push Stack

Computer Science: A Structured Programming Approach Using C 47

FIGURE 15-21 Pop Stack Example

Computer Science: A Structured Programming Approach Using C 48

PROGRAM 15-10 Pop Stack

Computer Science: A Structured Programming Approach Using C 49

PROGRAM 15-10 Pop Stack

Computer Science: A Structured Programming Approach Using C 50

FIGURE 15-22 Design for Basic Stack Program

Computer Science: A Structured Programming Approach Using C 51

PROGRAM 15-11 Simple Stack Application Program

Computer Science: A Structured Programming Approach Using C 52

PROGRAM 15-11 Simple Stack Application Program

Computer Science: A Structured Programming Approach Using C 53

PROGRAM 15-11 Simple Stack Application Program

Computer Science: A Structured Programming Approach Using C 54

PROGRAM 15-12 Insert Data

Computer Science: A Structured Programming Approach Using C 55

PROGRAM 15-12 Insert Data

Computer Science: A Structured Programming Approach Using C 56

PROGRAM 15-13 Print Stack

Computer Science: A Structured Programming Approach Using C 57

15-4 Queues

A queue is a linear list in which data can be inserted

only at one end, called the rear, and deleted from the

other end, called the front.

Queue Operations

Queue Linked List Design

Queue Functions

Queue Demonstration

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 58

A queue is a linear list in which data can be inserted at one

end, called the rear, and deleted from the other end,

called the front. It is a first in–first out

(FIFO) restricted data structure.

Note

Computer Science: A Structured Programming Approach Using C 59

FIGURE 15-23 Queue Concept

Computer Science: A Structured Programming Approach Using C 60

Enqueue inserts an element at the rear of the queue.

Note

Computer Science: A Structured Programming Approach Using C 61

FIGURE 15-24 Enqueue

Computer Science: A Structured Programming Approach Using C 62

Dequeue deletes an element at the front of the queue.

Note

Computer Science: A Structured Programming Approach Using C 63

FIGURE 15-25 Dequeue

Computer Science: A Structured Programming Approach Using C 64

FIGURE 15-26 Conceptual and Physical Queue Implementations

Computer Science: A Structured Programming Approach Using C 65

FIGURE 15-27 Queue Data Structure

Computer Science: A Structured Programming Approach Using C 66

FIGURE 15-28 Enqueue Example

Computer Science: A Structured Programming Approach Using C 67

PROGRAM 15-14 Enqueue

Computer Science: A Structured Programming Approach Using C 68

PROGRAM 15-14 Enqueue

Computer Science: A Structured Programming Approach Using C 69

FIGURE 15-29 Dequeue Examples

Computer Science: A Structured Programming Approach Using C 70

PROGRAM 15-15 Dequeue

Computer Science: A Structured Programming Approach Using C 71

PROGRAM 15-15 Dequeue

Computer Science: A Structured Programming Approach Using C 72

PROGRAM 15-16 Simple Queue Demonstration

Computer Science: A Structured Programming Approach Using C 73

PROGRAM 15-16 Simple Queue Demonstration

Computer Science: A Structured Programming Approach Using C 74

PROGRAM 15-16 Simple Queue Demonstration

Computer Science: A Structured Programming Approach Using C 75

PROGRAM 15-17 Insert Data

Computer Science: A Structured Programming Approach Using C 76

PROGRAM 15-17 Insert Data

Computer Science: A Structured Programming Approach Using C 77

PROGRAM 15-18 Print Queue

Computer Science: A Structured Programming Approach Using C 78

15-5 Trees

Trees are used extensively in computer science to

represent algebraic formulas; as an efficient method

for searching large, dynamic lists; and for such

diverse applications as artificial intelligence systems

and encoding algorithms.

Basic Tree Concepts

Terminology

Binary Trees

Binary Search Trees

Binary Tree Example

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 79

A tree consists of a finite set of elements, called nodes, and a

finite set of directed lines, called branches, that

connect the nodes.

Note

Computer Science: A Structured Programming Approach Using C 80

FIGURE 15-30 Tree

Computer Science: A Structured Programming Approach Using C 81

FIGURE 15-31 Tree Nomenclature

Computer Science: A Structured Programming Approach Using C 82

The level of a node is its distance from the root. The height

of a tree is the level of the leaf in the longest path

from the root plus 1.

Note

Computer Science: A Structured Programming Approach Using C 83

FIGURE 15-32 Subtrees

Computer Science: A Structured Programming Approach Using C 84

A tree is a set of nodes that either:

1. Is empty, or

2. Has a designated node, called the root, from which

hierarchically descend zero or more subtrees, which are

also trees

Note

Computer Science: A Structured Programming Approach Using C 85

FIGURE 15-33 Collection of Binary Trees

Computer Science: A Structured Programming Approach Using C 86

FIGURE 15-34 Binary Tree Data Structure

Computer Science: A Structured Programming Approach Using C 87

FIGURE 15-35 Binary Tree Traversals

Computer Science: A Structured Programming Approach Using C 88

FIGURE 15-36 Binary Tree for Traversals

Computer Science: A Structured Programming Approach Using C 89

In the preorder traversal, the root is processed first,

before its subtrees.

Note

Computer Science: A Structured Programming Approach Using C 90

PROGRAM 15-19 Preorder Traversal of a Binary Tree

Computer Science: A Structured Programming Approach Using C 91

FIGURE 15-37 Preorder Traversal—A B C D E F

Computer Science: A Structured Programming Approach Using C 92

FIGURE 15-38 Algorithmic Traversal of Binary Tree

Computer Science: A Structured Programming Approach Using C 93

PROGRAM 15-20 Inorder Traversal of a Binary Tree

Computer Science: A Structured Programming Approach Using C 94

FIGURE 15-39 Inorder Traversal—C B D A E F

Computer Science: A Structured Programming Approach Using C 95

In the inorder traversal, the root is processed

between its subtrees.

Note

Computer Science: A Structured Programming Approach Using C 96

In a binary search tree, the left subtree contains key values

less than the root, and the right subtree contains

key values greater than or equal to the root.

Note

Computer Science: A Structured Programming Approach Using C 97

FIGURE 15-40 Binary Search Tree

Computer Science: A Structured Programming Approach Using C 98

FIGURE 15-41 Valid Binary Search Trees

Computer Science: A Structured Programming Approach Using C 99

FIGURE 15-42 Invalid Binary Search Trees

Computer Science: A Structured Programming Approach Using C 100

All BST insertions take place at a leaf or a leaflike node.

Note

Computer Science: A Structured Programming Approach Using C 101

FIGURE 15-43 BST Insertion

Computer Science: A Structured Programming Approach Using C 102

PROGRAM 15-21 Binary Tree Insert Function

Computer Science: A Structured Programming Approach Using C 103

PROGRAM 15-21 Binary Tree Insert Function

Computer Science: A Structured Programming Approach Using C 104

FIGURE 15-44 Binary Tree Example

Computer Science: A Structured Programming Approach Using C 105

PROGRAM 15-22 Binary Tree Example

Computer Science: A Structured Programming Approach Using C 106

PROGRAM 15-22 Binary Tree Example

Computer Science: A Structured Programming Approach Using C 107

PROGRAM 15-22 Binary Tree Example

Computer Science: A Structured Programming Approach Using C 108

PROGRAM 15-22 Binary Tree Example

Computer Science: A Structured Programming Approach Using C 109

15-6 Graphs

A graph is a collection of nodes, called vertices, and a

collection of segments, called lines, connecting pairs

of vertices. In other words, a graph consists of two sets,

a set of vertices and a set of lines.

Graph Traversal

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 110

FIGURE 15-45 Directed and Undirected Graphs

Computer Science: A Structured Programming Approach Using C 111

Graphs may be directed or undirected. In a directed graph,

each line, called an arc, has a direction indicating

how it may be traversed. In an undirected graph,

the line is known as an edge, and it may

be traversed in either direction.

Note

Computer Science: A Structured Programming Approach Using C 112

A file is an external

collection of related data treated as a unit.

Note

Computer Science: A Structured Programming Approach Using C 113

FIGURE 15-46 Cycles and Loops

Computer Science: A Structured Programming Approach Using C 114

FIGURE 15-47 Connected and Disjoint Graphs

Computer Science: A Structured Programming Approach Using C 115

FIGURE 15-48 Depth-first Traversal of a Tree

Computer Science: A Structured Programming Approach Using C 116

In the depth-first traversal, all of a node’s descendents are

processed before moving to an adjacent node.

Note

Computer Science: A Structured Programming Approach Using C 117

FIGURE 15-49 Depth-first Traversal of a Graph

Computer Science: A Structured Programming Approach Using C 118

FIGURE 15-50 Breadth-first Traversal of a Tree

Computer Science: A Structured Programming Approach Using C 119

FIGURE 15-51 Breadth-first Traversal of a Graph

Computer Science: A Structured Programming Approach Using C 120

In the breadth-first traversal, all adjacent

vertices are processed before processing

the descendents of a vertex.

Note

Computer Science: A Structured Programming Approach Using C 121

15-7 Software Engineering

Because lists are useful structures, programmers use

them in many applications. Rather than rewrite their

functions each time we need them, we can write

functions once and put them in a library. The name

given to a complete set of these functions is abstract

data type (ADT).

Atomic and Composite Data

Data Structure and Abstract Data Type

A Model for an Abstract Data Type

ADT Data Structure

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 122

Atomic Data Type

1. A set of values.

2. A set of operations on the values.

Note

Computer Science: A Structured Programming Approach Using C 123

Data Structure

1. A combination of elements, each of which is either a data

type or another data structure.

2. A set of associations or relationships (structure) involving

the combined elements.

Note

Computer Science: A Structured Programming Approach Using C 124

Table 15-2 Two Structures

Computer Science: A Structured Programming Approach Using C 125

In the concept of abstraction

We know what a data type can do.

How it is done is hidden.

Note

Computer Science: A Structured Programming Approach Using C 126

FIGURE 15-52 Structures for Holding a List

Computer Science: A Structured Programming Approach Using C 127

Abstract Data Type

1. Declaration of data.

2. Declaration of operations.

Note

Computer Science: A Structured Programming Approach Using C 128

FIGURE 15-53 Abstract Data Type Model

top related