Data Structures

ShyhShyh-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

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

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 5 .

Use of Pointers 6 .

Arrays 7 .

TwoTwo-Dimensional Array 8 .

column major order Finding an entry in the i th row and j th column of a c-column 2-D array stored 2in row major order x  c v (i  1)  ( j  1) Address polynomial 9 .Storage of a 2-D Array 2  Row major order vs.

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 .

In this case what address polynomial is used by the translator to convert references of the form Array[i][j] into memory address? 12  . indices of arrays start at 0 rather than 1. and Java.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++.

Answers  r v ( j  1)  (i  1) vi  j 13 .

List   A collection of entries that appear in a sequential order Examples     Class enrollment lists ³things-to-do´ lists things-to-do´ 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 .

first-out (LIFO) structures last.Stacks    A list in which all insertions and deletions are performed at the same end A last-in.firstPush 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


A Stack in Memory


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


Your procedure should print an error message if the stack is empty.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. 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 .

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

Operations on Queues 32 .

Crawling Queue 33 .

Circular Queues (1) 34 .

Circular Queues (2) 35 .

C Remove two entries Insert entries D.       Insert entries A. Assume that the block reserved for the queue can contain only four entries. keep a record of the circular queue during the following scenario. E Remove an entry Insert entry F Remove an entry 36 .Mini Review  Using paper and pencil. B.

Answer H A B C H C T E H C T E F D H D E H E F 37 T H B C H C T T T T D H D T .

Organization Chart 38 .

Trees 39 .

File Structure of Windows 40 .

Trees  Nodes    Root Terminal (leaf) Parent. siblings   Subtrees Depth 41 . children.

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. y x w 46 z . Draw another diagram showing how the tree would appear in contiguous storage.


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 .

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

Answer V T S U X Y Z R 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 .

UserUser-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 .

61 . SalesRep1. SalesRep2. EmployeeType DistManager. } EmployeeType.Customized Data Types struct { char Name[8]. } Employee. int Age. float SkillRating. int Age. float SkillRating. typedef struct { char Name[8].

Node* right. } Node. 62 . Node* root. Node* left.Declaration of Nodes of a Binary Tree typedef struct { char data.

Classes  Data types   A predetermined storage system A collection of predefined operations Define both storage system and operations  Classes  63 .

C++ Class 64 .

pop(). StackOne. int OldValue = StackOne.Using Classes    StackOfIntegers StackOne(50).push(106). 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)  2RXY 1RXY DRXY DR0S S Load address (direct addressing)  Load through pointer (indirect addressing)   XY  Save through pointer  ER0S R 70 .

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

Answers    CA CA CA 72 .

8. 26. 24. 19. 43 73 . 30. 16. 5. 14.Exercise  Review problems 2.

Sign up to vote on this title
UsefulNot useful