Sei sulla pagina 1di 73

Data Structures

Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University

Data Structures

Explores ways in which users can be shielded from the details of actual data storage (memory cells and address) and be allowed to access information as though it were stored in a more convenient form Concept of abstraction The term user can be either human or a software module
2

Static vs. Dynamic Structures

Static structures

Shape and size of the structure do not change over time Easier to manage Either shape or size of the structure changes over time Must deal with adding and deleting data entries as well as finding the memory space required by a growing data structure
3

Dynamic structures

Pointers

A memory cell (or perhaps a block of memory cells) that contains the address of another memory cell Examples

Program counter as an instruction pointer URL

Many programming languages include pointers as a primitive data type

Allow the declaration, allocation, and manipulation of pointers


4

Used to create dynamic data structures

Stack and Heap Space


Heap Storage

Stack Storage

Use of Pointers

Arrays

Two-Dimensional Array

Storage of a 2-D Array

Row major order vs. column major order Finding an entry in the i th row and j th column of a c-column 2-D array stored in row major order

x c (i 1) ( j 1)
Address polynomial
9

Mini Review

Show how the array below would be arranged in main memory when stored in row major order
5 4 1 3 2 9 7 8 6

10

Answer

5 3 7 4 2 8 1 9 6

11

Mini Review

Give a formula for finding the entry in the i th row and j th column of a 2-D array stored in column major order
In C, C++, and Java, indices of arrays start at 0 rather than 1. In this case what address polynomial is used by the translator to convert references of the form Array[i][j] into memory address?
12

Answers

x r ( j 1) (i 1)

ci j
13

List

A collection of entries that appear in a sequential order Examples


Class enrollment lists things-to-do lists Dictionaries Sentences

Appear in both static and dynamic forms


14

Contiguous Lists

15

Contiguous List

Convenient storage structure when implementing static lists Inconvenient in dynamic cases

Delete a name

Move entries to keep the list in the same order


Move the entire list to obtain an available block of contiguous cells large enough for the expanded list

Add a name

16

Linked List

17

Deleting an Entry

18

Inserting an Entry

19

Printing a Linked List

20

Stacks

A list in which all insertions and deletions are performed at the same end A last-in, first-out (LIFO) structures Push and pop
Top B A

C
21

Backtracking

22

Backtracking

Reverse the action taken to get in


Top C B A Main Base

23

Print a List in Reverse Order (1)

24

Print a List in Reverse Order (2)

25

Procedure ReversePrint

26

A Stack in Memory

27

Procedures in Stack Systems

Push an entry Pop an entry Testing for an empty stack Testing for a full stack Can be also implemented as a linked list

28

Mini Review

Based on the technique for implementing a stack in a contiguous block of cells, what condition indicates that the stack is empty? Design a procedure for popping an entry off the stack that is implemented with a stack pointer. Your procedure should print an error message if the stack is empty.

29

Answers

The stack pointer points to the cell immediately below the base of the stack procedure pop() if( the stack pointer points below the stack base ) then ( exit with error message ) Extract the entry pointed by the stack pointer; Adjust the stack pointer to point to the next lower stack entry
30

Queue

A list in which all insertions are performed at one end while all deletions are made at the other A first-in, first-out (FIFO) structure Head (front) and tail (rear)

31

Operations on Queues

32

Crawling Queue

33

Circular Queues (1)

34

Circular Queues (2)

35

Mini Review

Using paper and pencil, keep a record of the circular queue during the following scenario. Assume that the block reserved for the queue can contain only four entries.

Insert entries A, B, C Remove two entries Insert entries D, E Remove an entry Insert entry F Remove an entry
36

Answer
H A B C
H C T

H B C
H C

T
E

H
C T D E H E

H
D

H
D

T
F
37

Organization Chart

38

Trees

39

File Structure of Windows

40

Trees

Nodes

Root Terminal (leaf) Parent, children, siblings

Subtrees Depth

41

Binary Tree Node

42

Linked Storage System

43

Storage without Pointers

44

Inefficient Storage

45

Mini Review

Draw a diagram representing how the tree below appears in memory when stored using the left and right child pointers. Draw another diagram showing how the tree would appear in contiguous storage.
y x z

w
46

Answer
Y Z NIL NIL

NIL

NIL NIL

47

Binary Tree Package

Search for the presence of an entry

Use the binary search algorithm

Print the list in order Insert a new entry

48

Ordered Tree

49

Search the Binary Tree

50

Search Binary Tree

51

Printing a Binary Tree

52

Mini Review

Draw a binary tree to store the list R, S, T, U, V, W, X, Y, and Z for future searching

53

Answer
V T S R Y

U
W

54

Printing a Tree in Order

55

Traversing a Binary Tree

Inorder traversal

Left Root Right Root Left Right Left Right Root

Preorder traversal

Postorder traversal

56

Inserting an Entry (1)

57

Inserting an Entry (2)

58

Inserting an Entry

59

User-Defined Types

Expressing an algorithm is often more convenient if types other than those provided as primitives in the programming language Many modern programming languages allow programmers to define additional data types, using the primitive types and structures as building blocks
60

Customized Data Types


struct { char Name[8]; int Age; float SkillRating; } Employee; typedef struct { char Name[8]; int Age; float SkillRating; } EmployeeType; EmployeeType DistManager, SalesRep1, SalesRep2;
61

Declaration of Nodes of a Binary Tree


typedef struct { char data; Node* left; Node* right; } Node;
Node* root;
62

Classes

Data types

A predetermined storage system A collection of predefined operations Define both storage system and operations

Classes

63

C++ Class

64

Using Classes

StackOfIntegers StackOne(50); StackOne.push(106); int OldValue = StackOne.pop();

65

Encapsulation

The private data and procedures can not be referenced directly from outside All access to the instance must be performed via the public method The integrity of these data types is protected from poorly conceived modifications

66

Standard Template Library

A collection of predefined classes in C++ that describe popular data structures The programmer is thus relieved from the task of describing these structures in detail

67

Expanding the Machine Language to Take Advantage of Pointers

68

Loading a Register from a Memory Cell Located by a Pointer in a Register

69

Pointers in Machine Language


Machine language defined in Appendix C Load data (immediate addressing)

Load address (direct addressing)

2RXY 1RXY

Load through pointer (indirect addressing)

Save through pointer

DRXY DR0S ER0S

XY S

70

Mini Review

Suppose the machine language has been extended to include pointer operations. Moreover, suppose register 8 contains the pattern DB, the memory cell at address DB contains the pattern CA, and the cell at address A5 contains the pattern CA. What bit pattern will be in register 5 immediately after executing the following instructions:

25A5 15CA D508


71

Answers

CA CA CA

72

Exercise

Review problems 2, 5, 8, 14, 16, 19, 24, 26, 30, 43

73

Potrebbero piacerti anche