Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Similar presentations


Presentation on theme: "Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )"— Presentation transcript:

1 Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2 Dynamic Data Structure Dynamic data structureDynamic data structure is a structure that can expand and contract as a program executes. pointersThe creation and manipulation of dynamic data structures requires use of pointers. pointer –A pointer is a variable which stores the memory address of a data value.

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3 Comparison of Pointer and Nonpointer Variables The actual data value of a pointer variable is accessed indirectly. The actual data value of a nonpointer variable can be accessed directly. Pointer variable Nonpointer variable

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4 Pointer Review A call to a function with pointer parameters may need to use the & operator. –e.g., if we have an int variable value1 and f1(int *value), f1(&value1) is a legal call. A pointer can be used to represent an array. –e.g., char n[] is equal to char *n. A pointer can also represent a structure. –e.g., File * is a pointer to a File structure.

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.5 Memory Allocation (1/3) C provides a memory allocation function called malloc, which resides in the stdlib library. –This function requires an argument which indicates the amount of memory space needed. –The returned data type is (void *) and should be always cast to the specific type. E.g., Declaration: int *nump; char *letp; planet_t *planetp;

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6 Memory Allocation (2/3) Allocation: nump = (int *) malloc (sizeof (int)); letp = (char *) malloc (sizeof (char)); planetp = (planet_t *) malloc (sizeof (planet_t)); Assignment: *nump = 307; *letp = ‘Q’; *planetp = blank_planet;

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7 Memory Allocation (3/3) Memory space after allocation Memory space after assignment Pointers

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8 Heap and Stack HeapHeap is the region of memory where function malloc dynamically allocates space for variables. StackStack is the region of memory where function data areas are allocated and reclaimed.

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9 Dynamic Array Allocation C provides a function calloc which creates an array of elements of any type and initializes the array elements to zero. –Function calloc takes two arguments: the number of array elements and the size of one element. E.g., int *array_of_nums; array_of_nums = (int *) calloc(10, sizeof(int));

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10 Free Memory The allocated memory space can be released by the function free. –E.g., free(letp) returns the allocated memory space for the pointer variable letp. Once the memory space is released, we can not access the space again. Otherwise, it is considered as an illegal memory access.

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11 Multiple Pointers to a Cell double *xp, xcopyp; xp=(double *)malloc(sizeof(double)); *xp=49.5; xcopyp=xp; Be careful when releasing memory since the other pointer may still access the memory space.

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12 Linked List linked listA linked list is a sequence of nodes in which each node is linked to the node following it. In C, each node can be represented by a struct: typedef struct node_s{ char current[3]; int volts; struct node_s *linkp; }node_t;

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13 Creating Basic Nodes (1/2) node_t *n1_p, *n2_p, *n3_p; n1_p = (node_t *) malloc (sizeof(node_t)); strcpy(n1_p->current, “AC”); n1_p->volts = 115; n2_p = (node_t *) malloc (sizeof(node_t)); strcpy(n2_p->current, “DC”); n2_p->volts = 12; n3_p = n2_p;

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.14 Creating Basic Nodes (2/2)

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.15 Linking Two Nodes n1_p->linkp = n2_p; “ n2_p->volts ” is equal to “ n1_p-> linkp->volts ”

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.16 Three-Node Linked List n2_p->linkp = (node_t *)malloc(sizeof(node_t)); strcpy(n2_p->linkp->current, “AC”); n2_p->linkp->volts = 220;

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.17 Three-Element Linked List The end of a linked list is usually terminated with a null pointer. –n2_p->linkp->linkp = NULL; –The following graph shows a complete linked list whose length is three. list head –The pointer variable n1_p points to the list head.

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.18 Linked List After an Insertion We can easily insert or delete a node to or from a linked list. –The following graph shows an insertion of a new node containing “DC 9” between the second and last nodes. –Redirects the linkp of the new node to the last node. –Redirects the linkp of the second node to the new node.

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.19 Traversing a Linked List Recursively or Iteratively We can print each element in a linked list recursively or iteratively. Recursive solutionIterative solution

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.20 Binary Tree We can extend the concept of linked list to binary trees which contains two pointer fields. –Leaf node: a node with no sucessors –Root node: the first node in a binary tree. –Left/right subtree: the subtree pointed by the left/right pointer.

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.21 Binary Search Tree binary search treeA binary search tree is either empty or has the property that the item in its root has –a larger key than each item in the left subtree, and –a smaller key than each item in its right subtree.

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.22 Searching a Binary Search Tree If the tree is empty –The target key is not in the tree else if the target key is the root’s key –The target key is found else if the target key is smaller than root’s key –Search the left subtree else –Search the right subtree

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.23 Searching a Binary Search Tree Assume the target key is 42.

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.24 Building a Binary Search Tree If the tree is empty –Insert the new key in the root node else if the root’s key matches the new key –Skip insertion else if the new key is smaller than root’s key –Insert the new key in the left subtree else –Insert the new key in the right subtree

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.25 Building a Binary Search Tree Assume 40, 20, 10, 50, 65, 45, 30 are inserted in order.

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.26 Building a Binary Search Tree The above algorithm implies a recursive implementation. Recursive step


Download ppt "Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )"

Similar presentations


Ads by Google