 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 

Slides:



Advertisements
Similar presentations
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Advertisements

Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Informática II Prof. Dr. Gustavo Patiño MJ
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
1 Writing a Good Program 8. Elementary Data Structure.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
COMP 53 – Week Eight Linked Lists.
Popping Items Off a Stack Using a Function Lesson xx
Lectures linked lists Chapter 6 of textbook
Chapter 4 Linked Lists.
Traversing a Linked List
Linked lists.
Dynamic Memory CSCE 121 J. Michael Moore.
CSCE 210 Data Structures and Algorithms
Programmazione I a.a. 2017/2018.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Dynamic Memory Allocation
Chapter 16-2 Linked Structures
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory Copy Challenge
Programming Abstractions
Dynamic Memory A whole heap of fun….
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
Dynamic Memory.
Dynamic Memory Copy Challenge
Linked Lists.
Linked lists.
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list  Deallocating memory using delete operator  Dynamic linked list variation

Program Stack Operating System Heap

int * ptr; ptr = new int; Program Stack Operating System Heap 9ab int ptr 9ab

struct entry { int value; entry* next; }; int main() { entry* temp, * head = 0; // initially empty list /*allocate the first node*/ head= new entry; temp = head; // create 4 more nodes for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node } temp ‑ >value = 0; /*last node points to nothing*/ temp ‑ >next = 0; /*it's a dummy node*/ (Part 1)

// traverse the list for (entry* p = head; p; p = p->next) { cout value next << endl; } // deallocate memory while (head) { entry* n = head->next; delete head; head = n; } return 0; } (Part 2)

struct entry { int value; entry* next; }; int main() { entry* temp, * head = 0; // initially empty list temp 0 head

/*allocate the first node*/ head= new entry; temp = head; 76c temp 76c head (76c)

76c temp 76c head 42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node }

76c temp 76c head 88b 42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node } (88b)

88b temp 76c head 88b42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node } (88b)

4af temp 76c head 88b42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node } (88b) 97 4af (4af)

head temp

head temp temp ‑ >value = 0; /*last node points to nothing*/ temp ‑ >next = 0; /*it's a dummy node*/

head temp // traverse the list for (entry* p = head; p; p = p->next) { cout value next << endl; } p

a6e temp 76c head (76c) 88b42 97 (88b) 4af 57d35 (4af) a6e1700 node address: 76c value 42 next 88b node address: 88b value 97 next 4af node address: 4af value 35 next 57d node address: 57d value 17 next a6e node address: a6e value 0 next 0 (57d)(a6e) 0 p

head n // deallocate memory while (head) { entry* n = head->next; delete head; head = n; }

head n // deallocate memory while (head) { entry* n = head->next; delete head; head = n; }

// create 4 more nodes for (int i = 0; i >temp ‑ >value; temp ‑ >next = new entry; temp = temp ‑ >next; } int n; cout << “How many nodes ? “; cin >> n; // create n-1 more nodes for (int i = 0; i >temp ‑ >value; temp ‑ >next = new entry; temp = temp ‑ >next; }

Memory from the heap Dynamic memory allocation using the new operator Build a dynamic linked list Another way of traversing a linked list Deallocating memory using delete operator Dynamic linked list variation