Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 13 Dynamic Data Structures 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. 2 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 Copyright ©2004 Pearson Addison-Wesley. All rights reserved. Nonpointer variable 3 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. 4 Pointer Review (Ex1) Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5 Pointer Review (Ex1 Cont.) Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 6 Pointer Review (Ex2) Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7 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; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8 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; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9 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. 10 Memory Allocation (3/3) Memory space after allocation Pointers Copyright ©2004 Pearson Addison-Wesley. All rights reserved. Memory space after assignment Pointers 11 malloc example Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 12 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. 13 Deallocating (free) Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 14 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. 15 calloc example Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 16 Linked List Basics Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 17 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. 18 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; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 19 Creating Basic Nodes (2/2) Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 20 Linking Two Nodes • n1_p->linkp = n2_p; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 21 Chain access • “n2_p->volts” is equal to “n1_p-> linkp->volts” Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 22 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. 23 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. 24 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. 25 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. 26 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. 27 Stack Basics Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 28 Representing a stack with a linked list • • • • Like a push down stack of books. Push (insert) at top (pointed by Head) Pop (remove) from the top (pointed by Head) Last in first out (LIFO) architecture. Head Copyright ©2004 Pearson Addison-Wesley. All rights reserved. Push 7 restp restp 5 restp restp 3 restp X Pop 29 Stacks • A stack can be implemented as a constrained version of a linked list. • New nodes can be added to a stack and removed from a stack only at the top. • For this reason, a stack is referred to as a last-in, firstout (LIFO) data structure. • A stack is referenced via a pointer to the top element of the stack. • The link member in the last node of the stack is set to NULL to indicate the bottom of the stack. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 30 Stacks • Stacks and linked lists are represented identically. • The difference between stacks and linked lists is that insertions and deletions may occur anywhere in a linked list, but only at the top of a stack. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 31 Queue Basics Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 32 Representing a Queue with a linked list • • • • Like a queue of people waiting Push at the Head (i.e at the end of the list). Pop from the Bottom (i.e from the front of the list) First In First Out (FIFO) Head Push End Front Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7 restp restp 5 restp restp 3 restp X Pop 33 Queue Basics • A queue is similar to a checkout line in a grocery store—the first person in line is serviced first, and other customers enter the line only at the end and wait to be serviced. • Queue nodes are removed only from the head of the queue and are inserted only at the tail of the queue. • For this reason, a queue is referred to as a first-in, first-out (FIFO) data structure. • The insert and remove operations are known as enqueue and dequeue, respectively. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 34 Queue Applications (1/3) • Queues have many applications in computer systems. • For computers that have only a single processor, only one user at a time may be serviced. • Each entry gradually advances to the front of the queue as users receive service. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 35 Queue Applications (2/3) • Queues are also used to support print spooling. • A multiuser environment may have only a single printer. • If the printer is busy, requests are spooled to disk where they wait in a queue until the printer becomes available. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 36 Queue Applications (3/3) • Information packets also wait in queues in computer networks. • Each time a packet arrives at a network node, it must be routed to the next node on the network along the path to its final destination. • The routing node routes one packet at a time, so additional packets are enqueued until the router can route them. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 37 Trees Basics Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 38 Trees Basics • Linked lists, stacks and queues are linear data structures. • A tree is a nonlinear, two-dimensional data structure with special properties. • Tree nodes contain two or more links. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 39 Trees Basics • The root node is the first node in a tree. • Each link in the root node refers to a child. • The left child is the first node in the left subtree, and the right child is the first node in the right subtree. • A node with no children is called a leaf node. • Computer scientists normally draw trees from the root node down—exactly the opposite of trees in nature. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 40 Copyright ©2004 Pearson Addison-Wesley. All rights reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 41 Binary Tree • We can extend the concept of linked list to binary trees which contains two pointer fields. – Leaf node: a node with no successors – Root node: the first node in a binary tree. – Left/right subtree: the subtree pointed by the left/right pointer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 42