Download presentation
Presentation is loading. Please wait.
Published byEverett Barnett Modified over 9 years ago
1
20.3.2001. Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture 18 26.3.2001.
2
20.3.2001. 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.
3
20.3.2001. 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.
4
20.3.2001. Sudeshna Sarkar, CSE, IIT Kharagpur4 Example pointer code. int * numPtr; int num = 42; numPtr = # *numPtr = 73; numPtr = malloc (sizeof (int)); *numPtr = 73;
5
20.3.2001. Sudeshna Sarkar, CSE, IIT Kharagpur5 int a=1, b=2, c=3; int *p, *q; 1 a 3 c 2 b xxx p q
6
20.3.2001. Sudeshna Sarkar, CSE, IIT Kharagpur6 p = &a ; q = &b ; 1 a 3 c 2 b p q
7
20.3.2001. Sudeshna Sarkar, CSE, IIT Kharagpur7 c = *p ; p = q ; *p = 13 ; 1 a 1 c 13 b p q
8
20.3.2001. 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
9
20.3.2001. 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.
10
20.3.2001. 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) ; }
11
20.3.2001. 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.
12
20.3.2001. 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.
13
20.3.2001. 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.
14
20.3.2001. 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
15
20.3.2001. 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
16
20.3.2001. 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.
17
20.3.2001. 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;
18
20.3.2001. 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
19
20.3.2001. 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
20
20.3.2001. 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
21
20.3.2001. 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
22
20.3.2001. 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; }
23
20.3.2001. 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)
24
20.3.2001. 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
25
20.3.2001. 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; }
26
20.3.2001. 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); }
27
20.3.2001. 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); }
28
20.3.2001. 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
29
20.3.2001. 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
30
20.3.2001. Sudeshna Sarkar, CSE, IIT Kharagpur30 Deletion Before deletion 123 p p->next = p->next->next; After deletion 123 p garbage
31
20.3.2001. 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) ;
32
20.3.2001. 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 */ }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.