Sei sulla pagina 1di 27

MODULE 5 Linked list

5.1 Linked List In computer science, a linked list is one of the fundamental data structures used in computer programming. It consists of a sequence of nodes, each containing arbitrar data fields and one or t!o references "#links#$ pointing to the ne%t and&or pre'ious nodes. ( linked list is a self)referential data t pe because it contains a link to another data of the same t pe. Linked lists permit insertion and remo'al of nodes at an point in the list in constant time, but do not allo! random access

Advantages of Linked List: ( linked list is a d namic data structure and therefore the si*e of the linked list can gro! or shrink in si*e during e%ecution of the program. ( linked list does not require an e%tra space therefore it does not !aste e%tra memor . It pro'ides fle%ibilit in rearranging the items efficientl . +he limitation of linked list is that it consumes e%tra space !hen compared to a arra since each node must also contain the address of the ne%t item in the list to search for a single item in a linked list is cumbersome and time consuming. 5.2 Operations on linked list +here are se'eral operations that !e can think of performing on linked lists. +he follo!ing program sho!s ho! to build a linked list b adding ne! nodes at the beginning, at the end or in the middle of the linked list. It also contains a function displa "$ !hich displa s all the nodes present in the linked list and a function delete"$ !hich can delete an node in the linked list. 5. , Program to maintain a linked list
-include .stdio.h/ -include .conio.h/ -include .alloc.h/ &0 structure containing a data part and link part 0& struct node 1 int data 2 struct node 0 link 2 32 'oid append " struct node 00, int $ 2 'oid addatbeg " struct node 00, int $ 2 'oid addafter " struct node 0, int, int $ 2 'oid displa " struct node 0 $ 2 int count " struct node 0 $ 2

'oid delete " struct node 00, int $ 2 'oid main" $ 1 struct node 0p 2 p 4 5ULL 2 &0 empt linked list 0& printf " #6n5o. of elements in the Linked List 4 7d#, count " p $ $ 2 append " 8p, 9: $ 2 append " 8p, ,; $ 2 append " 8p, <5 $ 2 append " 8p, :< $ 2 append " 8p, 9= $ 2 displa " p $ 2 addatbeg " 8p, >>> $ 2 addatbeg " 8p, ??? $ 2 addatbeg " 8p, === $ 2 displa " p $ 2 addafter " p, =, ; $ 2 addafter " p, <, 9 $ 2 addafter " p, 5, >> $ 2 displa " p $ 2 printf " #6n5o. of elements in the Linked List 4 7d#, count " p $ $ 2 delete " 8p, >> $ 2 delete " 8p, 9 $ 2 delete " 8p, 9; $ 2 displa " p $ 2 printf " #6n5o. of elements in the Linked List 4 7d#, count " p $ $ 2 3 &0 adds a node at the end of a linked list 0& 'oid append " struct node 00q, int num $ 1 struct node 0temp, 0r 2 if " 0q 44 5ULL $ &0 if the list is empt , create first node 0& 1 temp 4 malloc " si*eof " struct node $ $ 2 temp )/ data 4 num 2 temp )/ link 4 5ULL 2 0q 4 temp 2 3 else 1 temp 4 0q 2 &0 go to last node 0&

!hile " temp )/ link @4 5ULL $ temp 4 temp )/ link 2 &0 add node at the end 0& r 4 malloc " si*eof " struct node $ $ 2 r )/ data 4 num 2 r )/ link 4 5ULL 2 temp )/ link 4 r 2 3 3 &0 adds a ne! node at the beginning of the linked list 0& 'oid addatbeg " struct node 00q, int num $ 1 struct node 0temp 2 &0 add ne! node 0& temp 4 malloc " si*eof " struct node $ $ 2 temp )/ data 4 num 2 temp )/ link 4 0q 2 0q 4 temp 2 3 &0 adds a ne! node after the specified number of nodes 0& 'oid addafter " struct node 0q, int loc, int num $ 1 struct node 0temp, 0r 2 int i 2 temp 4 q 2 &0 skip to desired portion 0& for " i 4 ; 2 i . loc 2 iAA $ 1 temp 4 temp )/ link 2 /* if end of linked list is encountered */ if ( temp !"LL # $ printf ( %&n'(ere are less t(an )d elements in list%* loc # + return + , , /* insert ne- node */ r malloc ( si.eof ( struct node # # + r /0 data num + r /0 link temp /0 link + temp /0 link r + , /* displa1s t(e contents of t(e linked list */

void displa1 ( struct node *2 # $ printf ( %&n% # + /* traverse t(e entire linked list */ -(ile ( 2 3 !"LL # $ printf ( %)d %* 2 /0 data # + 2 2 /0 link + , , /* counts t(e num4er of nodes present in t(e linked list */ int count ( struct node * 2 # $ int c 5 + /* traverse t(e entire linked list */ -(ile ( 2 3 !"LL # $ 2 2 /0 link + c66 + , return c + , /* deletes t(e specified node from t(e linked list */ void delete ( struct node **2* int num # $ struct node *old* *temp + temp *2 + -(ile ( temp 3 !"LL # $ if ( temp /0 data num # $ /* if node to 4e deleted is t(e first node in t(e linked list */ if ( temp *2 # *2 temp /0 link + /* deletes t(e intermediate nodes in t(e linked list */ else old /0 link temp /0 link + /* free t(e memor1 occupied 41 t(e node */ free ( temp # + return + , /* traverse t(e linked list till t(e last node is reac(ed */

else $ old temp + /* old points to t(e previous node */ temp temp /0 link + /* go to t(e ne7t node */ , , printf ( %&n8lement )d not found%* num # + ,

5.9 '1pes of Link List 9$ Linearl )linked List <$ Bingl )linked list ,$ Doubl )linked list :$ Circularl )linked list 5.9.1 :ingl1 Linked List In a singl "one)!a $ linear linked list, each node is di'ided into t!o parts. +he first part contains the information of the element. +he second part called the linked field or ne%t pointer field contains the address of the ne%t node in the list. ( head pointer is used to hold the address of the first element in the list. (lso, the last element of the linked list has a 5ULL 'alue in the ne%t pointer field to mark the end of the list

5.9.1 (a# ;eclaration of a Linear Linked List

Buppose !e !ant to store a list of ints, then the linear linked list can be declared asD t pedef struct nodet pe 1 int info2 struct nodet pe 0ne%t2 3 node2 node 0head2 +he abo'e declaration defines a ne! data t pe, Estruct nodet peF, !ith a t pedef of EnodeF. +o insert an element in the list, the first task is to create a ne! node, assign the element to be inserted to the info field of the node, and then place the ne! node at the appropriate position b adGusting the appropriate pointers. Insertion in the list can take place at the follo!ing positionsD

(t the beginning of the list (t the end of the list (fter a gi'en element

a) Insertion at the Beginning of the List Hirst, test !hether the linked list is initiall empt , if es, then the element is inserted as the first and onl one element b performing the follo!ing stepsD

(ssign 5ULL to the ne%t pointer field of the ne! node. (ssign the address of the ne! node to head.

If the list is not empt , then the element is inserted as the first element of the list b performing the follo!ing stepsD

(ssign the 'alue of head to the ne%t pointer field of the ne! node. (ssign the address of the ne! node to head.

Irogrammaticall , both cases are equi'alent. +his is because in both the cases the first step is to assign the 'alue of head "5ULL or other!ise$ to the ne%t pointer field of the ne! node.
'oid insertatbeginning"node 00head, int item$ 1 node 0ne!5ode2 &0 allocate memor for the ne! node and initiali*e the data in it0& ne!5ode4 malloc"si*eof"node$$2 ne!5ode)/info4item2 &0 assign the 'alue of head to the Ene%tF of ne!5ode0& ne!5ode)/ne%t40head2 &0 assign the address of ne!5ode to head 0& 0head4ne!5ode2 3

b) Inserting at the End of the List Hirst test !hether the linked list is initiall empt , if es, then the element is inserted as the first and onl one element b performing the follo!ing stepsD )(ssign 5ULL to the ne%t pointer field of the ne! node. )(ssign the address of the ne! node to head. If the list is not empt , !e tra'erse it to reach the last element, and then the ne! node is inserted as the last element of the list b performing the follo!ing stepsD )(ssign 5ULL to the ne%t pointer field of the ne! node. )(ssign the address of the ne! node to the ne%t pointer field of the last node.
'oid insertatend"node 00head, int item$ 1 node 0ne!5ode2 ne!5ode4malloc"si*eof"node$$2 ne!5ode)/info4item2

ne!5ode)/ne%t45ULL2 if"0head445ULL$ 0head4ne!5ode2 else 1 node 0 pre'40head2 !hile "pre')/ne%t@45ULL$ pre'4pre')/ne%t2 pre')/ne%t4ne!5ode2 3 3 c) Inserting after Given Element

+o insert a ne! element after the gi'en element, first !e find the location, sa loc, of the gi'en element in the list, and then the element is inserted in the list b performing the follo!ing stepsD

(ssign the ne%t pointer field of the node pointed to b loc to the ne%t pointer field of the ne! node. (ssign the address of the ne! node to the ne%t pointer field of the node pointed to b loc.

'oid insertafterelement"node 0head, int item,int after$ 1 node 0ne!5ode, 0loc2 loc4searchunsortedlist"head,after$2 if"loc445ULL$ &0element after not found0& return2 ne!5ode4malloc"si*eof"node$$2 ne!5ode)/info4item2 ne!5ode)/ne%t4loc)/ne%t2 loc)/ne%t4ne!5ode2 3

5.9.2 <ircular Linked List +he linked lists that !e ha'e seen so far are often kno!n as linear linked list. (ll elements of such a linked list can be accessed b first setting up

a pointer pointing to the first node in the list and then tra'ersing the entire list using this pointer. ( circular linked list does not ha'e a first or last node. 5.9.2 (a# <ircular Linked List =mplementation ( linked list in !hich the node at the tail of the list, instead of ha'ing a null pointer, points back to the node at the head of the list. +hus both ends of a list can be accessed using a single pointer.

In a circularl linked list, all nodes are linked in a continuous circle, !ithout using null. Hor lists !ith a front and a back "such as a queue$, one stores a reference to the last node in the list. +he ne%t node after the last node is the first node. Elements can be added to the back of the list and remo'ed from the front in constant time. Circularl )linked lists can be either singl or doubl linked. Joth t pes of circularl )linked lists benefit from the abilit to tra'erse the full list beginning at an gi'en node. +his often allo!s us to a'oid storing first5ode and last5ode, although if the list ma be empt !e need a special representation for the empt list, such as a last5ode 'ariable !hich points to some node in the list or is null if itKs empt 2 !e use such a last5ode here.

+his representation significantl simplifies adding and remo'ing nodes !ith a non)empt list, but empt lists are then a special case. 5.9.2 (4# <ircular Linked List in < -include.stdio.h/ -include.conio.h/ -include.stdlib.h/ -include.alloc.h/ -define null ; struct node 1 int info2 struct node 0link2 30start2 'oid main"$ 1 int ch,n,m,position,i2 last4null2 !hile"9$ 1 printf"#9.create <.addat ,.addbt :.del 5.disp L.e%it #$2 printf"#er ur ch#$2 scanf"#7d#,8ch$2 s!itch"ch$ 1 case 9D printf"#er no of itc#$2 scanf"#7d#,8n$2

for"i4;2i.n2iAA$ 1 printf"#er the element#$2 scanf"#7d#,8m$2 create"m$2 3break2 case <D printf"#er the element#$2 scanf"#7d#,8m$2 addat"m$2 break2 case ,D printf"#er the element#$2 scanf"#7d#,8m$2 printf"#er the position#$2 scanf"#7d#,8position$2 addbt"m,position$2 break2 case :D if"last44null$ 1 printf"#list is empt #$2 continue2 3 printf"#er the element for delete#$2 scanf"#7d#,8m$2 del"m$2 break2 case 5D disp"$2 break2 case LD e%it";$2 break2

defaultD printf"#!rong choice#$2 3 3 3 create"int data$ 1 struct node 0q,0tmp2 tmp4"struct node 0$malloc"si*eof"struct node$$2 tmp)/info4data2 tmp)/link4null2 if"last44null$ 1 last4tmp2 tmp)/link4last2 3 else 1 tmp)/link4last)/link2 last)/link4tmp2 last4tmp2 33 addat"int data$ 1 struct node 0q,0tmp2 tmp4"struct node 0$malloc"si*eof"struct node$$2 tmp)/info4data2 tmp)/link4last)/link2 last)/link4tmp2 3 addbt"int data,int pos$ 1 struct node 0tmp,0q2 int i2

q4last)/link22 for"i4;2i.pos)92iAA$ 1 q4q)/link2 if"q44last)/link$ 1 printf"#there r lessthan 7d elements#,pos$2 return2 3 3 tmp4"struct node 0$malloc"si*eof"struct node$$2 tmp)/link4q)/link2 tmp)/info4data2 q)/link4tmp2 if"q44last$ last4tmp2 3 del"int data$ 1 struct node 0tmp,0q2 if"last)/link44last88last)/info44data$ 1 tmp4last2 last4null2 free"tmp$2 return2 3 q4last)/link2 if"q)/info44data$ 1 tmp4q2 last)/link4q)/link2 free"tmp$2 return2

3 !hile"q)/link@4last$ 1 if"q)/link)/info44data$ 1 tmp4q)/link2 q)/link4tmp)/link2 free"tmp$2 printf"#element 7d is deleted#,data$2 3 if"q)/link)/info4data$ 1 tmp4q)/link2 q)/link4last)/link2 free"tmp$2 last4q2 return23 printf"#element7d is not found#,data$2 3 disp"$ 1 struct node 0q2 if"last44null$ 1 printf"#list isdempt #$2 return2 3q4last)/link2 !hile"q@4last$ 1 printf"#7d#,q)/info$2 q4q)/link2 3 printf"#7d#,last)/info$2

3 5.9.> ;ou4l1 Linked List In a doubl linked list, also called a t!o)!a list, each node is di'ided into three partsD 9$ +he first part, called the pre'ious pointer field, contains the address of the preceding element in the list. <$ +he second part contains the information of the list. ,$ +he third part, called the ne%t pointer field, contains the address of the succeeding element in the list. In addition, t!o pointer 'ariables, named head and tail, are used that contain the address of first element and the address of last element of the list.

5.9.> (a#=mplementation of a ;ou4l1 Linked List in < Buppose !e !ant to store list of integers. +hen, !e define the follo!ing self)referential structureD

t pedef struct nodet pe 1 struct nodet pe 0pre'2 int info2 struct nodet pe 0ne%t2 3node2 node 0head,0tail2 +he abo'e declaration defines a ne! data t pe called Estruct nodet peF !ith a t pedef of EnodeF. +!o node pointers are also declaredD head and tail. 5.9.> (4#=nserting an 8lement in dou4l1 Linked List +o insert an element in the list, the first task is to allocate memor for a ne! node, assign the element to be inserted to the info field of the node, and then the ne! node is placed at the appropriate position b adGusting appropriate pointers. Insertion in the list can take place at the follo!ing positionsD

(t the beginning of the list (t the end of the list (fter a gi'en element Jefore a gi'en element

a) Insertion at the Beginning of the List Hirst, test !hether the linked list is empt , if es, then the element is inserted as the first and onl one element b performing the follo!ing stepsD

(ssign 5ULL to the ne%t pointer and pre' pointer fields of the ne! node (ssign the address of the ne! node to head and tail pointer 'ariables.

If the list is not empt , then the element is inserted as the first element of the list b performing the follo!ing stepsD

(ssign 5ULL to the pre' pointer field of the ne! node. (ssign the 'alue of the head 'ariable "the address of the first element of the e%isting list$ to the ne%t pointer field of the ne! node. (ssign the address of the ne! node to pre' pointer field of the node currentl pointed b head 'ariable, i.e. first element of the e%isting list. Hinall assign the address of the ne! node to the head 'ariable

b)Inserting at the End of the List Hirst test !hether the linked list is initiall empt , if es, then the element is inserted as the first and onl one element b performing the follo!ing stepsD

(ssign 5ULL 'alue to the ne%t pointer and pre' pointer field of the ne! node (ssign address of ne! node to head and tail pointer 'ariable.

If the list is not empt , then element is inserted as the last element of the list b performing the follo!ing stepsD

(ssign 5ULL 'alue to the ne%t pointer field of the ne! node. (ssign 'alue of the tail 'ariable "the address of the last element of the e%isting list$ to the pre' pointer field of the ne! node. (ssign address of the ne! node to the ne%t pointer field of the node currentl pointed b tail 'ariable i.e. last element of the e%isting list. Hinall assign the address of the ne! node to tail 'ariable.

5.9.9 < <O;8 'oid insertatend "node 00head, node 00tail, int item$

1 node 0ptr2 ptr 4 malloc"si*eof"node$$2 ptr)/info 4 item2 if "0head 44 5ULL$ 1 ptr)/ne%t 4 ptr)/pre'45ULL2 0head 4 0tail 4 ptr2 3 else 1 ptr)/ne%t45ULL2 ptr)/pre'40tail2 "0tail$)/ne%t4ptr2 0tail4ptr2 3 3 =nserting 4efore a ?iven 8lement 'oid insertbeforeelement "node 00head, int item, int before$ 1 node 0ptr, 0loc2 ptr40head2 loc4search"ptr,before$2 if"loc445ULL$ return2 ptr4malloc"si*eof"node$$2 ptr)/info4item2 if"loc)/pre'445ULL$ 1 ptr)/pre'45ULL2 loc)/pre'4ptr2 ptr)/ne%t40head2 0head4ptr2

3 else 1 ptr)/pre'4loc)/pre'2 ptr)/ne%t4loc2 "loc)/pre'$)/ne%t4ptr2 loc)/pre'4ptr2 3 3 =nserting after a ?iven 8lement 'oid insertafterelement "node 00head, node 00tail, int item, int after$ 1 node 0ptr, 0loc2 ptr 4 0head2 loc 4 search"ptr,after$2 if"loc 44 5ULL$ return2 ptr4malloc"si*eof"node$$2 ptr)/info 4 item2 if"loc)/ne%t 44 5ULL$ 1 ptr)/ne%t 4 5ULL2 loc)/ne%t 4 ptr2 ptr)/pre' 4 0tail2 0tail 4 ptr2 3 else 1 ptr)/pre' 4 loc2 ptr)/ne%t 4 loc)/ne%t2 "loc)/ne%t$)/pre' 4 ptr2 loc)/ne%t 4 ptr2

3 3 5.5 ;eleting an element from a dou4l1 Linked list. +o delete an element from the list, first the pointers are set properl and then the memor occupied b the node to be deleted is deallocated "freed$. Deletion in the list can take place at the follo!ing positions. a$ (t the beginning of the list b$ (t the end of the list c$ (fter a gi'en element d$ Jefore a gi'en element a) Deleting from the Beginning of the List (n element from the beginning of the list can be deleted b performing the follo!ing steps (ssign the 'alue of head "address of the first element of the list$ to a temporar 'ariable "sa temp$ +here are t!o further casesD )If there is onl one element in the e%isting list, both head and tail are set to 5ULL. )If there is more than one element in the list then )(ssign 5ULL to the pre' pointer field of the second node. )(ssign the address of the second node to head. )Deallocate the memor occupied b the node pointed to b temp.
< <ode 'oid deletefrombeginning" node 00head, node 00tail$ 1 node 0temp2

if"0head445ULL$ return2 temp40head2 if"0head440tail$ &0one element onl 0& 0head40tail45ULL2 else 1 "temp)/ne%t$)/pre'45ULL2 0head4temp)/ne%t2 3 free"temp$2 3

b) Deleting from the End of the List (n element from the end of the list can be deleted b performing the follo!ing stepsD (ssign the 'alue of tail "address of the last element of the list$ to a temporar 'ariable "sa temp$ Hurther there are t!o casesD ) If there is onl one element in the e%isting list, set both head and tail to 5ULL. )If there is more than one element in the list then )(ssign 5ULL to the ne%t pointer field of the second last node. )(ssign the address of the second last node to tail. )Deallocate the memor occupied b the node pointed to b temp.

< <ode 'oid deletefromend" node 00head, node 00tail$ 1 node 0temp2 if"0head445ULL$ return2

temp40tail2 if"0head440tail$ &0one element onl 0& 0head40tail45ULL2 else 1 temp)/pre')/ne%t45ULL2 0tail4temp)/pre'2 3 free"temp$2 3

c) Deleting after a Given Element


'oid deleteafterelement "node 00head, node 00tail, int after$ 1 node 0temp, 0loc2 temp 4 0head2 loc 4 search"temp,after$2 if "loc 44 5ULL$ &0search item not found0& return2 temp 4 loc)/ne%t2 loc)/ne%t 4 temp)/ne%t2 if"temp)/ne%t 44 5ULL$ 0tail 4 loc2 else "temp)/ne%t$)/pre' 4 loc2 free"temp$2 3

d) Deleting before a Given Element


'oid deletebeforeelement "node 00head, int before$ 1 node 0temp, 0loc2 temp40head2 loc4search"temp,before$2 if"loc445ULL$ return2 temp4loc)/pre'2

loc)/pre'4temp)/pre'2 if"temp)/pre'445ULL$ 0head4loc2 else "temp)/pre'$)/ne%t4loc2 free"temp$2 3

5.@ Areeing up t(e 8ntire List +he doubl linked list can be deleted either from the beginning or from the end. +o delete from the beginning, use the follo!ing stepsD )(ssign the head pointer to a temporar 'ariable, sa temp. )(d'ance the head pointer to the ne%t node. )Deallocate the memor occupied b the node pointed to b temp. )Mepeat the abo'e steps until the entire list is deleted. )Hinall , set the tail pointer to 5ULL.
< <ode 'oid deletelist"node 00head, node 00tail$ 1 node 0temp2 !hile"0head@45ULL$ 1 temp40head2 0head4"0head$)/ne%t2 free"temp$2 3 0tail45ULL2 3

5.B Allocating and freeing ;1namic Caria4les. In programming !e ma come across situations !here !e ma ha'e to deal !ith data, !hich is d namic in nature. +he number of data items ma change during the e%ecutions of a program. +he number of

customers in a queue can increase or decrease during the process at an time. Nhen the list gro!s !e need to allocate more memor space to accommodate additional data items. Buch situations can be handled mo'e easil b using d namic techniques. D namic data items at run time, thus optimi*ing file usage of storage space. 5.B (a# ;1namic memor1 allocation: +he process of allocating memor at run time is kno!n as d namic memor allocation. (lthough c does not inherentl ha'e this facilit there are four librar routines !hich allo! this function. Man languages permit a programmer to specif an arra si*e at run time. Buch languages ha'e the abilit to calculate and assign during e%ecutions, the memor space required b the 'ariables in the program. Jut c inherentl does not ha'e this facilit but supports !ith memor management functions, !hich can be used to allocate and free memor during the program e%ecution. +he follo!ing functions are used in c for purpose of memor management. Aunction 'ask malloc calloc free realloc (llocates memor requests si*e of b tes and returns a pointer to the Ist b te of allocated space (llocates space for an arra of elements initiali*es them to *ero and returns a pointer to the memor Hrees pre'iousl allocated space Modifies the si*e of pre'iousl allocated space.

5.B (4#Demor1 allocations process: (ccording to the conceptual 'ie! the program instructions and global and static 'ariable in a permanent storage area and local area 'ariables are stored in stacks. +he memor space that is located bet!een these t!o regions in a'ailable for d namic allocation during the e%ecution of the program. +he free memor region is called the heap. +he si*e of heap keeps changing !hen program is e%ecuted due to creation and death of 'ariables that are local for functions and blocks. +herefore it is possible to encounter memor o'erflo! during d namic allocation process. In such situations, the memor allocation functions mentioned abo'e !ill return a null pointer. 5.B ( <# Allocating a 4lock of memor1: ( block mf memor ma be allocated using the function malloc. +he malloc function reser'es a block of memor of specified si*e and returns a pointer of t pe 'oid. +his means that !e can assign it to an t pe of pointer. It takes the follo!ing formD ptr4"cast)t pe0$malloc"b te)si*e$2 ptr is a pointer of t pe cast)t pe the malloc returns a pointer "of cast t pe$ to an area of memor !ith si*e b te)si*e. 87ample: %4"int0$malloc"9;;0si*eof"int$$2 On successful e%ecution of this statement a memor equi'alent to 9;; times the area of int b tes is reser'ed and the address of the first b te of memor allocated is assigned to the pointer % of t pe int

Allocating multiple 4locks of memor1: Calloc is another memor allocation function that is normall used to request multiple blocks of storage each of the same si*e and then sets all b tes to *ero. +he general form of calloc isD ptr4"cast)t pe0$ calloc"n,elem)si*e$2 +he abo'e statement allocates contiguous space for n blocks each si*e of elements si*e b tes. (ll b tes are initiali*ed to *ero and a pointer to the first b te of the allocated region is returned. If there is not enough space a null pointer is returned. 5.B (d# Eeleasing t(e used space: Compile time storage of a 'ariable is allocated and released b the s stem in accordance !ith its storage class. Nith the d namic runtime allocation, it is our responsibilit to release the space !hen it is not required. +he release of storage space becomes important !hen the storage is limited. Nhen !e no longer need the data !e stored in a block of memor and !e do not intend to use that block for storing an other information, !e ma release that block of memor for future use, using the free function. free"ptr$2 ptr is a pointer that has been created b using malloc or calloc. 'o alter t(e si.e of allocated memor1: +he memor allocated b using calloc or malloc might be insufficient or e%cess sometimes in both the situations !e can change the memor si*e alread allocated !ith the help of the function realloc. +his process is called reallocation of memor .

Potrebbero piacerti anche