SlideShare a Scribd company logo
1 of 8
Dynamic Memory Allocation
&
Linked Lists
Dynamic memory location:
The process of allocating memory at run time is known as dynamic memory
allocation.
C does not inherently have this facility. There are four library routines known as
memory management functions that can be used for allocating and freeing memory
during execution.
malloc(), calloc(), free(), realloc()
malloc: Allocating a block of memory
General form
ptr = (cast-type *) malloc(byte-size);
ptr is a pointer of type cast-type
The malloc() returns a pointer (of cast-type) to an area of memory with size byte-
size.
If there is not enough space a NULL pointer is returned.
Example
x=(int *) malloc(100*sizeof(int));
cptr=(char *) malloc(10);
st= (struct *) malloc(sizeof(struct store));
Storage space allocated dynamically has no name and therefore its contents can be accessed only through
a pointer.
calloc: Allocating multiple block of memory
While malloc() allocates a single block of storage space, calloc() allocates multiple blocks of storage, each
of the same size, and then set all bytes to zero. If there is not enough space a NULL pointer is returned.
General form
ptr=(cast-type *) calloc(n, element-size);
free( ); Releasing the used space
With dynamic run-time allocation, we need to release the space when it is not required
free(ptr);
Frees previously allocated space created by malloc() or calloc()
realloc( ); Altering the size of a block
It is likely that the previous allocated memory is not sufficient and we need
additional space for more elements.
It is also possible that the memory allocated is much larger than necessary and
we want to reduce it.
ptr=realloc(ptr, newsize);
Modifies the size of previously allocated space by malloc() or calloc().
Linked List
A list refers to a set of items organized sequentially
Linked list is a completely different way to represent a list is to make each item
in the list part of a structure that also contains a “link” to the structure
containing the next item.
A linked list is a dynamic data structures.
Dynamic Memory Allocation
Basic It is a consistent set of a fixed number of data
items.
It is an ordered set comprising a variable number
of data items.
Size Specified during declaration. No need to specify; grow and shrink during
execution
Storage
Allocation
Element location is allocated during compile time. Element position is assigned during run time.
Order of the
elements
Stored consecutively Stored randomly
Accessing the
element
Direct or randomly accessed, i.e., Specify the array
index or subscript.
Sequentially accessed, i.e., Traverse starting from
the first node in the list by the pointer.
Difference between array and linked list
Array Linked List
Insertion and
deletion of
element
Slow relatively as shifting is required. Easier, fast and efficient.
Searching Binary search and linear search linear search
Memory
required
less More
Memory
Utilization
Ineffective Efficient
Difference between array and linked list
Array Linked List
Advantages of linked list
• Can grow or shrink in size during the execution of a program
• Does not waste memory space
• Here it is not necessary to specify the number of nodes to be used in the list
• It provides flexibility is allowing the items to be rearranged efficiently
• It is easier to insert or delete items by rearranging the links
Limitation of Linked List
• The access to any arbitrary item is little cumbersome and time consuming.
• Use more storage than an array with the same number of items. This is because each item
has an additional link field.
• Linked lists are traversed in unidirection
• Complex to implement
• Sequential access to elements
• Leads to memory problems if not taken care about the pointer manipulations properly
That is, Whenever we deal with a fixed length list, it would be better to use an array rather
than a linked list.
What we can do with Linked List:-
1. Creating a list.
2. Traversing the list.
3. Counting the items in the list.
4. Printing the list (or sub list).
5. Looking up an item for editing or printing.
6. Inserting an item.
7. Deleting an item.
8. Concatenating two lists.
9. Replace() : replaces a value stored at a position with some other value.
10.Swap() : swap the values of the nodes specified by two positions.
11. Merging two linked lists into a larger list.
12.Searching for an element in a linked list.
13.Reversing a linked list.

More Related Content

What's hot

Presentation on Data Structure
Presentation on Data StructurePresentation on Data Structure
Presentation on Data StructureA. N. M. Jubaer
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
DATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGEDATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGEshubhamrohiwal6
 
Visula C# Programming Lecture 5
Visula C# Programming Lecture 5Visula C# Programming Lecture 5
Visula C# Programming Lecture 5Abou Bakr Ashraf
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
Array c programming
Array c programmingArray c programming
Array c programmingDevan Thakur
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURERohit Rai
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its typesRameesha Sadaqat
 
Presentation on array
Presentation on array Presentation on array
Presentation on array topu93
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
Array in c language
Array in c languageArray in c language
Array in c languageumesh patil
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureZaid Shabbir
 

What's hot (18)

Array in c
Array in cArray in c
Array in c
 
Presentation on Data Structure
Presentation on Data StructurePresentation on Data Structure
Presentation on Data Structure
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
DATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGEDATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGE
 
Visula C# Programming Lecture 5
Visula C# Programming Lecture 5Visula C# Programming Lecture 5
Visula C# Programming Lecture 5
 
The awesome algorithm
The awesome algorithmThe awesome algorithm
The awesome algorithm
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Numeric Arrays [6]
Numeric Arrays [6]Numeric Arrays [6]
Numeric Arrays [6]
 
Array c programming
Array c programmingArray c programming
Array c programming
 
Array
ArrayArray
Array
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Java part 2
Java part  2Java part  2
Java part 2
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Linked list
Linked listLinked list
Linked list
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 

Similar to Dynamic memory allocation and linked lists

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationMoniruzzaman _
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)Sangharsh agarwal
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9dplunkett
 
Standard template library
Standard template libraryStandard template library
Standard template librarySukriti Singh
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxprakashvs7
 
Data Structures_Introduction
Data Structures_IntroductionData Structures_Introduction
Data Structures_IntroductionThenmozhiK5
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxGIRISHKUMARBC1
 
Describes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxDescribes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxearleanp
 
Data structure Assignment Help
Data structure Assignment HelpData structure Assignment Help
Data structure Assignment HelpJosephErin
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Hassan Ahmed
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxUsriDevi1
 

Similar to Dynamic memory allocation and linked lists (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptx
 
Data Structures_Introduction
Data Structures_IntroductionData Structures_Introduction
Data Structures_Introduction
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
DS_PPT.pptx
DS_PPT.pptxDS_PPT.pptx
DS_PPT.pptx
 
Describes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxDescribes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docx
 
Data structure Assignment Help
Data structure Assignment HelpData structure Assignment Help
Data structure Assignment Help
 
Data structures
Data structuresData structures
Data structures
 
java.pdf
java.pdfjava.pdf
java.pdf
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptx
 

More from Deepam Aggarwal

Frames and its components
Frames and its components Frames and its components
Frames and its components Deepam Aggarwal
 
Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural ProgrammingDeepam Aggarwal
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Deepam Aggarwal
 
Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security Deepam Aggarwal
 
Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892Deepam Aggarwal
 
Triangle and its properties
Triangle and its propertiesTriangle and its properties
Triangle and its propertiesDeepam Aggarwal
 
probability-game of chances
probability-game of chancesprobability-game of chances
probability-game of chancesDeepam Aggarwal
 
Political and public awareness through media (B.st)
Political and public awareness through media (B.st)Political and public awareness through media (B.st)
Political and public awareness through media (B.st)Deepam Aggarwal
 

More from Deepam Aggarwal (13)

Frames and its components
Frames and its components Frames and its components
Frames and its components
 
Bank managment system
Bank managment systemBank managment system
Bank managment system
 
Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural Programming
 
Inventory Management
Inventory Management Inventory Management
Inventory Management
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security
 
Mdi vb.net
Mdi vb.netMdi vb.net
Mdi vb.net
 
Stress Management
Stress Management Stress Management
Stress Management
 
Impressionism
ImpressionismImpressionism
Impressionism
 
Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892
 
Triangle and its properties
Triangle and its propertiesTriangle and its properties
Triangle and its properties
 
probability-game of chances
probability-game of chancesprobability-game of chances
probability-game of chances
 
Political and public awareness through media (B.st)
Political and public awareness through media (B.st)Political and public awareness through media (B.st)
Political and public awareness through media (B.st)
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

Dynamic memory allocation and linked lists

  • 2. Dynamic memory location: The process of allocating memory at run time is known as dynamic memory allocation. C does not inherently have this facility. There are four library routines known as memory management functions that can be used for allocating and freeing memory during execution. malloc(), calloc(), free(), realloc() malloc: Allocating a block of memory General form ptr = (cast-type *) malloc(byte-size); ptr is a pointer of type cast-type The malloc() returns a pointer (of cast-type) to an area of memory with size byte- size. If there is not enough space a NULL pointer is returned.
  • 3. Example x=(int *) malloc(100*sizeof(int)); cptr=(char *) malloc(10); st= (struct *) malloc(sizeof(struct store)); Storage space allocated dynamically has no name and therefore its contents can be accessed only through a pointer. calloc: Allocating multiple block of memory While malloc() allocates a single block of storage space, calloc() allocates multiple blocks of storage, each of the same size, and then set all bytes to zero. If there is not enough space a NULL pointer is returned. General form ptr=(cast-type *) calloc(n, element-size); free( ); Releasing the used space With dynamic run-time allocation, we need to release the space when it is not required free(ptr); Frees previously allocated space created by malloc() or calloc()
  • 4. realloc( ); Altering the size of a block It is likely that the previous allocated memory is not sufficient and we need additional space for more elements. It is also possible that the memory allocated is much larger than necessary and we want to reduce it. ptr=realloc(ptr, newsize); Modifies the size of previously allocated space by malloc() or calloc(). Linked List A list refers to a set of items organized sequentially Linked list is a completely different way to represent a list is to make each item in the list part of a structure that also contains a “link” to the structure containing the next item. A linked list is a dynamic data structures.
  • 5. Dynamic Memory Allocation Basic It is a consistent set of a fixed number of data items. It is an ordered set comprising a variable number of data items. Size Specified during declaration. No need to specify; grow and shrink during execution Storage Allocation Element location is allocated during compile time. Element position is assigned during run time. Order of the elements Stored consecutively Stored randomly Accessing the element Direct or randomly accessed, i.e., Specify the array index or subscript. Sequentially accessed, i.e., Traverse starting from the first node in the list by the pointer. Difference between array and linked list Array Linked List
  • 6. Insertion and deletion of element Slow relatively as shifting is required. Easier, fast and efficient. Searching Binary search and linear search linear search Memory required less More Memory Utilization Ineffective Efficient Difference between array and linked list Array Linked List
  • 7. Advantages of linked list • Can grow or shrink in size during the execution of a program • Does not waste memory space • Here it is not necessary to specify the number of nodes to be used in the list • It provides flexibility is allowing the items to be rearranged efficiently • It is easier to insert or delete items by rearranging the links Limitation of Linked List • The access to any arbitrary item is little cumbersome and time consuming. • Use more storage than an array with the same number of items. This is because each item has an additional link field. • Linked lists are traversed in unidirection • Complex to implement • Sequential access to elements • Leads to memory problems if not taken care about the pointer manipulations properly That is, Whenever we deal with a fixed length list, it would be better to use an array rather than a linked list.
  • 8. What we can do with Linked List:- 1. Creating a list. 2. Traversing the list. 3. Counting the items in the list. 4. Printing the list (or sub list). 5. Looking up an item for editing or printing. 6. Inserting an item. 7. Deleting an item. 8. Concatenating two lists. 9. Replace() : replaces a value stored at a position with some other value. 10.Swap() : swap the values of the nodes specified by two positions. 11. Merging two linked lists into a larger list. 12.Searching for an element in a linked list. 13.Reversing a linked list.