Dynamic Data Structures

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Informática II Prof. Dr. Gustavo Patiño MJ
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic memory allocation in C and C++ 程式設計 潘仁義 CCU COMM.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Chapter 14 Dynamic Data Structures Instructor: Alkar & Demirer.
CS Data Structures Chapter 4 Lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Problem Solving & Program Design in C Seventh Edition By Jeri R. Hanly.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dynamic Allocation in C
Pointers and Dynamic Arrays
Stack and Heap Memory Stack resident variables include:
Dynamic Allocation Review Structure and list processing
© 2016 Pearson Education, Ltd. All rights reserved.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure and Algorithms
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Checking Memory Management
Chapter 14: Dynamic Data Structures
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
Dynamic memory allocation in C and C++
Chapter 17 Linked Lists.
Chapter 14 Graphs and Paths.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Hassan Khosravi / Geoffrey Tien
Review & Lab assignments
CS111 Computer Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic memory allocation in C and C++
Chapter 10-1: Dynamic Memory Allocation
Chapter 9: Pointers and String
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Dynamic memory allocation in C and C++
Pointers, Dynamic Data, and Reference Types
Chapter 2 Reference Types.
Chapter 11 Structure and Union Types.
Module 13 Dynamic Memory.
Presentation transcript:

Dynamic Data Structures 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.

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.

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.

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

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

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

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.

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.

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

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.

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

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

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.

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

Deallocating (free)

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.

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

calloc example

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

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.

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.

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

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

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

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.

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.

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.

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.

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.