CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Linked Lists.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
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.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
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.
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:
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
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.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Pointers OVERVIEW.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Introduction to Data Structures Systems Programming Concepts.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
ECE Application Programming
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linked List (II) CGS 3460, Lecture 37 Apr 12, 2006 Hen-I Yang.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
© 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.
Chapter 12 – Data Structures
Linked List :: Basic Concepts
5.13 Recursion Recursive functions Functions that call themselves
Review Deleting an Element from a Linked List Deletion involves:
malloc(): Dynamic Memory Management
Introduction to Data Structures
Lists.
Dynamic Memory Allocation Reference Variables
Lists.
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Review & Lab assignments
Linked List.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Presentation transcript:

CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr = &k;

CGS 3460 Pointers and Structures n Declaration struct date *datePtr; n Initialization datePtr = &today; n Access members l(*datePtr).year ldatePtr->year struct date{ int day; char month[10]; int year; }; struct date today;

CGS 3460 Structures contains pointers n Pointers can be members in a structure struct date{ int day; char* month; int year; }; n Special case: struct date{ int day; char* month; int year; struct date *next; }; day month Year next day month Year next

CGS 3460 Linked List n What is a list? lNode lLink n An alternative to Array n Pros/Cons l+ More flexible l+ Can grow and shrink as necessary l+ Insert and delete node with particular index l- Cannot be randomly accessed l+ sorted (ordered list)

CGS 3460 Linked List n Operations lStart a list lCreate a node lInsert lDelete lSearch lPrint

CGS 3460 Memory Allocation n malloc is by far most frequently used Defined in void *malloc(size_t size); lAllocate memory space with size bytes lWhy does it return a void pointer? Because it doesn't matter to malloc to what type this memory will be used for If no memory of the size available, will return null Be sure to check whether it is null;

CGS 3460 Function sizeof n Helpful when dynamically allocating memory lsizeof(data type) returns a size_t of the data type in byte(s) For a typical 32-bit machine sizeof(int) returns 4

CGS 3460 Free Allocated Space n Very important lSystem won’t automatically take back memory space allocated through malloc lIf not free, a memory leak n How? lUse function void free(void *ptr); lIt release the memory space referenced by ptr Note that free can take in NULL, as specified by ANSI

CGS 3460 Build a Linked List n Build a structure representing a node struct studentRecord { int idNum; struct studentRecord *next; }; n Initialize the node struct studentRecord *first = NULL; first NULL

CGS 3460 Create a node n Follow these steps: lAllocate memory for the node lSet data into the node struct studentRecord *new_student; new_student = malloc(sizeof(struct studentRecord )); (* new_student).idNum = 1; new_student  next = NULL; Id: 1 Next: NULL new_student

CGS 3460 Insert a node: insert in the front(empty) n Follow these steps: lCreate a node lSet the node pointing to the front of the list lSet it as the starting node of this list new_student = malloc(sizeof(struct studentRecord )); new_student  idNum = 2; new_student  next = first; first = new_student ; Id: 2 Next: new_student first NULL

CGS 3460 Insert a node: insert in the front(not empty) n Follow these steps: lCreate a node lSet the node pointing to the front of the list lSet it as the starting node of this list new_student = malloc(sizeof(struct studentRecord )); new_student  idNum = 2; new_student  next = first; first = new_student ; Id: 2 Next: new_student first NULL Id: 5 Next:

CGS 3460 Insert a node: insert in middle n To insert a new node after node called pt, follow these steps: lCreate a node lSet the node pointing to the next node after pt in the list lSet it as the next node of this list after pt new_student = malloc(sizeof(struct studentRecord )); new_student  idNum = 2; new_student  next = pt  next; pt  next = new_student ; Id: 2 Next: new_student first NULL Id: 5 Next: pt

CGS 3460 Traversing along the List for (p = first; p != NULL; p = p  next) { …. } int Length(struct studentRecord* first) { int count = 0; struct studentRecord* current = first; while (current != NULL) { count++; current=current->next; } return(count); }

CGS 3460 Get nth Element in List int GetNth(struct studentRecord * first, int index) { struct studentRecord * current = first; int count = 0; // the index of the node while (current != NULL) { if (count == index) return(current  idNum ); count++; current = current  next; } return(-1); // if we get to this line, the caller was asking // for a non-existent element }

CGS 3460 Delete n Delete (almost reverse of insertion) llocating the node to be deleted (see search a linked list) laltering the previous node to bypass the deleted node lcalling free to reclaim the space occupied by the deleted node

CGS 3460 Example for deleting n To delete a node p from a linked list, you should also know the node which is in front of p in the list(q) if (p != NULL) { q  next = p  next; free(p); } NULL Id: 5 Next: Id: 2 Next: first Id: 2 Next: p q

CGS 3460 Example for deleting n If deleting the first node in the list if( p == first ) { first = p->next; free(p); } NULL Id: 5 Next: Id: 2 Next: first Id: 2 Next: p

CGS 3460 Demo n Appl.html Appl.html