20.3.2001. Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture 18 26.3.2001.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Linked Lists: deleting...
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
LIST PROCESSING.
Dynamic memory allocation
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
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.
C Intro.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Kernighan/Ritchie: Kelley/Pohl:
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{
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.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Structures and Lists (and maybe stacks) Chapters 9-10.
1 Review of Class on Nov 30:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
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.
Lecture 25 Self-Referential Structures Linked Lists
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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.
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,
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
CSC2100B Tutorial 2 List and stack implementation.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
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.
Dynamic memory allocation and Pointers Lecture 4.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Chapter 19 Data Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Data Structures A data structure.
Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record.
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.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dynamic Allocation Review Structure and list processing
5.13 Recursion Recursive functions Functions that call themselves
Lectures linked lists Chapter 6 of textbook
Introduction to Data Structures
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Dynamic Memory Allocation
Review & Lab assignments
Linked List.
Dynamic Memory A whole heap of fun….
Structures and List Processing
Pointers, Dynamic Data, and Reference Types
Dynamic Data Structures
Presentation transcript:

Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture

Sudeshna Sarkar, CSE, IIT Kharagpur2 Dynamic allocation: review Variables in C are allocated in one of 3 spots: the run-time stack : variables declared local to functions are allocated during execution the global data section : Global variables are allocated here and are accessible by all parts of a program. the heap : Dynamically allocated data items malloc, calloc, realloc manage the heap region of the mmory. If the allocation is not successful a NULL value is returned.

Sudeshna Sarkar, CSE, IIT Kharagpur3 Bad Pointers When a pointer is first allocated, it does not have a pointee. The pointer is uninitialized or bad. A dereference operation on a bad pointer is a serious runtime error. Each pointer must be assigned a pointee before it can support dereference operations. int * numPtr; Every pointer starts out with a bad value. Correct code overwrites the bad value.

Sudeshna Sarkar, CSE, IIT Kharagpur4 Example pointer code. int * numPtr; int num = 42; numPtr = # *numPtr = 73; numPtr = malloc (sizeof (int)); *numPtr = 73;

Sudeshna Sarkar, CSE, IIT Kharagpur5 int a=1, b=2, c=3; int *p, *q; 1 a 3 c 2 b xxx p q

Sudeshna Sarkar, CSE, IIT Kharagpur6 p = &a ; q = &b ; 1 a 3 c 2 b p q

Sudeshna Sarkar, CSE, IIT Kharagpur7 c = *p ; p = q ; *p = 13 ; 1 a 1 c 13 b p q

Sudeshna Sarkar, CSE, IIT Kharagpur8 Bad pointer Example void BadPointer () { int *p; *p = 42; } int * Bad2 () { int num, *p; num = 42; p = # return p; } x x x p X

Sudeshna Sarkar, CSE, IIT Kharagpur9 A function call malloc(size) allocates a block of mrmory in the heap and returns a pointer to the new block. size is the integer size of the block in bytes. Heap memory is not deallocated when the creating function exits. malloc generates a generic pointer to a generic data item (void *) or NULL if it cannot fulfill the request. Type cast the pointer returned by malloc to the type of variable we are assigning it to. free : takes as its parameter a pointer to an allocated region and de-allocates memory space.

Sudeshna Sarkar, CSE, IIT Kharagpur10 Dynamic memory allocation: review typedef struct { int hiTemp; int loTemp; double precip; } WeatherData; main () { int numdays; WeatherData * days; scanf (“%d”, &numdays) ; days=(WeatherData *)malloc (sizeof(WeatherData)*numdays); if (days == NULL) printf (“Insufficient memory”);... free (days) ; }

Sudeshna Sarkar, CSE, IIT Kharagpur11 Self-referential structures Dynamic data structures : Structures with pointer members that refer to the same structure. Arrays and other simple variables are allocated at block entry. But dynamic data structures require storage management routine to explicitly obtain and release memory.

Sudeshna Sarkar, CSE, IIT Kharagpur12 Self-referential structures struct list { int data ; struct list * next ; } ; The pointer variable next is called a link. Each structure is linked to a succeeding structure by next.

Sudeshna Sarkar, CSE, IIT Kharagpur13 Pictorial representation A structure of type struct list datanext The pointer variable next contains either an address of the location in memory of the successor list element or the special value NULL defined as 0. NULL is used to denote the end of the list.

Sudeshna Sarkar, CSE, IIT Kharagpur14 struct list a, b, c; a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; 1NULL datanext a 2NULL datanext b 3NULL datanext c

Sudeshna Sarkar, CSE, IIT Kharagpur15 Chaining these together a.next = &b; b.next = &c; 1 datanext a 2 datanext b 3 datanext c NULL What are the values of : a.next->data a.next->next->data 2323

Sudeshna Sarkar, CSE, IIT Kharagpur16 Linear Linked Lists A head pointer addresses the first element of the list. Each element points at a successor element. The last element has a link value NULL.

Sudeshna Sarkar, CSE, IIT Kharagpur17 Header file : list.h #include typedef char DATA; struct list { DATA d; struct list * next; }; typedef struct list ELEMENT; typedef ELEMENT * LINK;

Sudeshna Sarkar, CSE, IIT Kharagpur18 Storage allocation LINK head ; head = malloc (sizeof(ELEMENT)); head->d = ‘n’; head->next = NULL; creates a single element list. nNULL head

Sudeshna Sarkar, CSE, IIT Kharagpur19 Storage allocation head->next = malloc (sizeof(ELEMENT)); head->next->d = ‘e’; head->next->next = NULL; A second element is added. n head eNULL

Sudeshna Sarkar, CSE, IIT Kharagpur20 Storage allocation head->next=>next = malloc (sizeof(ELEMENT)); head->next->next->d = ‘e’; head->next->next-> = NULL; We have a 3 element list pointed to by head. The list ends when next has the sentinel value NULL. n head ewNULL

Sudeshna Sarkar, CSE, IIT Kharagpur21 List operations  Create a list  Count the elements  Look up an element  Concatenate two lists  Insert an element  Delete an element

Sudeshna Sarkar, CSE, IIT Kharagpur22 Produce a list from a string (recursive version) #include “list.h” LINK StrToList (char s[]) { LINK head ; if (s[0] == ‘\0’) return NULL ; else { head = malloc (sizeof(ELEMENT)); head->d = s[0]; head->next = StrToList (s+1); return head; }

Sudeshna Sarkar, CSE, IIT Kharagpur23 #include “list.h” LINK SToL (char s[]) { LINK head = NULL, tail; int i; if (s[0] != ‘\0’) { head = malloc (sizeof(ELEMENT)); head->d = s[0]; tail = head; for (i=1; s[i] != ‘\0’; i++) { tail->next = malloc(sizeof(ELEMENT)); tail = tail->next; tail->d = s[i]; } tail->next = NULL; } return head; } list from a string (iterative version)

Sudeshna Sarkar, CSE, IIT Kharagpur24 ?A head tail 1. A one-element list 2. A second element is attached A head tail ?? 3. Updating the tail A head tail ?B 4. after assigning NULL A head tail NULLB

Sudeshna Sarkar, CSE, IIT Kharagpur25 /* Count a list recursively */ int count (LINK head) { if (head == NULL) return 0; return 1+count(head->next); } /* Count a list iteratively */ int count (LINK head) { int cnt = 0; for ( ; head != NULL; head=head->next) ++cnt; return cnt; }

Sudeshna Sarkar, CSE, IIT Kharagpur26 /* Print a List */ void PrintList (LINK head) { if (head == NULL) printf (“NULL”) ; else { printf (“%c --> “, head->d) ; PrintList (head->next); }

Sudeshna Sarkar, CSE, IIT Kharagpur27 /* Concatenate two Lists */ void concatenate (LINK ahead, LINK bhead) { if (ahead->next == NULL) ahead->next = bhead ; else concatenate (ahead->next, bhead); }

Sudeshna Sarkar, CSE, IIT Kharagpur28 Insertion Insertion in a list takes a fixed amount of time once the position in the list is found. A C p2 p1 B q Before Insertion

Sudeshna Sarkar, CSE, IIT Kharagpur29 Insertion /* Inserting an element in a linked list. */ void insert (LINK p1, LINK p2, LINK q) { p1->next = q; q->next = p2; } A C p2 p1 B q After Insertion

Sudeshna Sarkar, CSE, IIT Kharagpur30 Deletion Before deletion 123 p p->next = p->next->next; After deletion 123 p garbage

Sudeshna Sarkar, CSE, IIT Kharagpur31 Deletion Before deletion 123 p q = p->next; p->next = p->next->next; After deletion 123 p q free (q) ;

Sudeshna Sarkar, CSE, IIT Kharagpur32 Delete a list and free memory /* Recursive deletion of a list */ void delete_list (LINK head) { if (head != NULL) { delete_list (head->next) ; free (head) ; /* Release storage */ }