Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Data Structures

Similar presentations


Presentation on theme: "Dynamic Data Structures"— Presentation transcript:

1 Dynamic Data Structures
Chapter 13 Dynamic Data Structures

2 Dynamic Data Structure
Dynamic data structure is a structure that can expand and contract as a program executes. The creation and manipulation of dynamic data structures requires use of pointers. A pointer is a variable which stores the memory address of a data value. 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. Nonpointer variable Pointer variable 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. 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. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

5 Pointer Review (Ex1) Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

6 Pointer Review (Ex1 Cont.)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

7 Pointer Review (Ex2) Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

8 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., Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

9 Memory Allocation (2/3) Declaration____________________________ int *nump; char *letp; planet_t *planetp; 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; Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

10 Memory Allocation (3/3) Example
Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

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

12 Memory Allocation in Heap
Memory space after allocation Memory space after assignment Pointers Pointers

13 malloc example Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

14 Deallocating memory Function free deallocates memory—i.e., the memory is returned to the system so that it can be reallocated in the future. To free memory dynamically allocated by the preceding malloc call, use the statement free( Ptr ); Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

15 Deallocating memory Example
Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

16 Deallocating (free)

17 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)); Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

18 Dynamic Array Allocation
Example int x=5; Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

19 calloc example

20 Linked List Basics Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

21 Linked List A 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; Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

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

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

24 Linking Two Nodes n1_p->linkp = n2_p;
Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

25 Chain access “n2_p->volts” is equal to “n1_p-> linkp->volts”
Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

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

27 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. The pointer variable n1_p points to the list head. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

28 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. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

29 Advantages of Linked Lists (1/2)
A linked list is an important data structure because it can be modified easily. For example, a new node containing DC 9 can be inserted between the nodes DC 12 and AC 220 by changing only one pointer value (the one from DC 12 ) and setting the pointer from the new node to point to AC 220 . This means of modifying a linked list works regardless of how many elements are in the list. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

30 Advantages of Linked Lists (1/2)
Similarly, it is quite easy to delete a list element. Only one pointer value within the list must be changed, specifically, the pointer that currently points to the element being deleted. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.


Download ppt "Dynamic Data Structures"

Similar presentations


Ads by Google