SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Introduction to Linked List
Mohammed Shameer MC
Dept. of Computer Science
Contents
• Structures
• Pointers
• Dynamic Memory Allocation
• Linked List
Structures
Structure is a user defined data type in C
Allows grouping of logically related data items of different types.
Unlike an array that hold homogeneous data items, structure hold heterogeneous
data items.
The keyword struct is used to define a structure.
struct structure_name
{
data_type member_variable1;
data_type member_variable2;
data_type member_variable3;
..............
} structure_variable1, structure_variable2, ....structure_variableN;
Pointers
• Every variables should be declared in C along with its data type.
• When a variable is declared, memory is allocated for the variable.
• So a variable is a named memory location.
• Computer memory is sequentially addressed.
• Pointer is a variable that store the memory address of another
variable in memory.
• The general syntax for pointer declaration is
datatype * ptr;
Initialization
ptr = &var;
Pointers and Structures
struct struct_name{
type member1;
type member2;
type member3;
}v1,v2;
• Pointer variables can be declared and initialized as
struct struct_name *ptr;
ptr= &v1;
• The members can be accessed using arrow (-> ) operator as.
ptr->member1; ptr->member2;
Static and dynamic memory allocation
• For an array, the size has to be fixed at the time of compilation itself.
We allocate memory that satisfies our requirement.
• The memory thus allocated may be much larger than actual
requirement or will be lesser.
Int s[10]
• Dynamic memory allocation allocates memory to variables while the
program is being executed.
• memory is allocated only if it actually required and at run time. So the
list may grow at run time.
Dynamic memory management
•malloc(),
•calloc(),
•realloc() and
•free(). Stack Heap
Malloc
• This method is used to allocate a block of memory of specified size.
• Malloc() does not initialize the memory and returns void type pointer
that can be cast to any data type.
• The syntax of this method is
Data_type * ptr = (castType*) malloc (size);
float* p = (float*) malloc(sizeof(float));
The calloc()
• The calloc() method allocates continuous multiple blocks of memory
from heap area and initializes it into zero.
Data_type * ptr = (castType*)calloc(n, size);
• Int *p =(int *) calloc(10,2);
• Char *p1=(char*)calloc (10, sizeof(char));
The free()
• This method releases the dynamically allocated memory.
• The syntax is
free(ptr) free(p);
The realloc()
• ptr = realloc (p, 20*sizeof(int));
Disadvantages of Arrays
• Arrays are data structures of fixed size and length cannot be altered at
run time.
• Insertion and deletion of elements may require reshuffling of array
elements. Thus, array manipulation is time-consuming and inefficient.
Linked List
• A linked list is a linear collection of data elements, called nodes,
where the linear order is maintained by means of pointers.
• Linked list is otherwise called one way list or singly linked list.
• Self referential strcuture
Node
The basic building block of a list is a node. A node consists of two parts:
• Data part that contain the information about the element, and
• Link or next pointer field contains the address of the next node in the
list
struct node
{
int data;
struct node *link;
} ;
Memory allocation for a node
• Struct node *p, *q, *start;
• P= (strcut node*) malloc( sizeof(strcut node)); p->data = 10;
• Q= (strcut node*) malloc (sizeof(Strcut node)); q->data =20;
• P->link = Q ; q->link = NULL; start =p;
So, A linked list is
• Linear DS
• Heterogeneous
• Dynamic
• Non contigeous
Operations on Linked List
Linked List is a dynamic data structure which supports following
operations.
• Traversal
• Insertion
• Deletion
• Searching
• Inverting
Thank you..

Weitere ähnliche Inhalte

Was ist angesagt?

Struct
StructStruct
StructFahuda E
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structuresunilchute1
 
Dynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDeepam Aggarwal
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Smit Shah
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppteShikshak
 
Basics of data structure
Basics of data structureBasics of data structure
Basics of data structureRajendran
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation ashu awais
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & UnionsRam Sagar Mourya
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture topu93
 
Data structure
Data structureData structure
Data structureGaurav Handge
 
Array in c
Array in cArray in c
Array in cAnIsh Kumar
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languagetanmaymodi4
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisationMuzamil Hussain
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3Prerna Sharma
 

Was ist angesagt? (20)

Struct
StructStruct
Struct
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Dynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDynamic memory allocation and linked lists
Dynamic memory allocation and linked lists
 
Structure
StructureStructure
Structure
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
Basics of data structure
Basics of data structureBasics of data structure
Basics of data structure
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
7.basic array
7.basic array7.basic array
7.basic array
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Structure c
Structure cStructure c
Structure c
 
Data structure
Data structureData structure
Data structure
 
Array in c
Array in cArray in c
Array in c
 
Impquest2
Impquest2Impquest2
Impquest2
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 

Ähnlich wie Introduction linked list

Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxGIRISHKUMARBC1
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationMoniruzzaman _
 
Welcome to our_presentation in c
Welcome to our_presentation in cWelcome to our_presentation in c
Welcome to our_presentation in cimran hasan
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxsabithabanu83
 
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
 
637225564198396290.pdf
637225564198396290.pdf637225564198396290.pdf
637225564198396290.pdfSureshKalirawna
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptxKorbanMaheshwari
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introductionMandeep Singh
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxKorbanMaheshwari
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxssuser688516
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)Dilawar Khan
 
Chapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptChapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptNORSHADILAAHMADBADEL
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptxBilalImran17
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptxAnanthi Palanisamy
 
an introduction to c++ templates-comprehensive guide.ppt
an introduction to c++ templates-comprehensive guide.pptan introduction to c++ templates-comprehensive guide.ppt
an introduction to c++ templates-comprehensive guide.pptAbdullah Yousafzai
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++Yung-Yu Chen
 

Ähnlich wie Introduction linked list (20)

Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
java.pdf
java.pdfjava.pdf
java.pdf
 
aspice
aspiceaspice
aspice
 
Welcome to our_presentation in c
Welcome to our_presentation in cWelcome to our_presentation in c
Welcome to our_presentation in c
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
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
 
637225564198396290.pdf
637225564198396290.pdf637225564198396290.pdf
637225564198396290.pdf
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptx
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptx
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
Chapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptChapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.ppt
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptx
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
an introduction to c++ templates-comprehensive guide.ppt
an introduction to c++ templates-comprehensive guide.pptan introduction to c++ templates-comprehensive guide.ppt
an introduction to c++ templates-comprehensive guide.ppt
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++
 

KĂźrzlich hochgeladen

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 

KĂźrzlich hochgeladen (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Introduction linked list

  • 1. Introduction to Linked List Mohammed Shameer MC Dept. of Computer Science
  • 2. Contents • Structures • Pointers • Dynamic Memory Allocation • Linked List
  • 3. Structures Structure is a user defined data type in C Allows grouping of logically related data items of different types. Unlike an array that hold homogeneous data items, structure hold heterogeneous data items. The keyword struct is used to define a structure. struct structure_name { data_type member_variable1; data_type member_variable2; data_type member_variable3; .............. } structure_variable1, structure_variable2, ....structure_variableN;
  • 4. Pointers • Every variables should be declared in C along with its data type. • When a variable is declared, memory is allocated for the variable. • So a variable is a named memory location. • Computer memory is sequentially addressed. • Pointer is a variable that store the memory address of another variable in memory.
  • 5. • The general syntax for pointer declaration is datatype * ptr; Initialization ptr = &var;
  • 6. Pointers and Structures struct struct_name{ type member1; type member2; type member3; }v1,v2; • Pointer variables can be declared and initialized as struct struct_name *ptr; ptr= &v1; • The members can be accessed using arrow (-> ) operator as. ptr->member1; ptr->member2;
  • 7. Static and dynamic memory allocation • For an array, the size has to be fixed at the time of compilation itself. We allocate memory that satisfies our requirement. • The memory thus allocated may be much larger than actual requirement or will be lesser. Int s[10] • Dynamic memory allocation allocates memory to variables while the program is being executed. • memory is allocated only if it actually required and at run time. So the list may grow at run time.
  • 9. Malloc • This method is used to allocate a block of memory of specified size. • Malloc() does not initialize the memory and returns void type pointer that can be cast to any data type. • The syntax of this method is Data_type * ptr = (castType*) malloc (size); float* p = (float*) malloc(sizeof(float));
  • 10. The calloc() • The calloc() method allocates continuous multiple blocks of memory from heap area and initializes it into zero. Data_type * ptr = (castType*)calloc(n, size); • Int *p =(int *) calloc(10,2); • Char *p1=(char*)calloc (10, sizeof(char)); The free() • This method releases the dynamically allocated memory. • The syntax is free(ptr) free(p); The realloc() • ptr = realloc (p, 20*sizeof(int));
  • 11. Disadvantages of Arrays • Arrays are data structures of fixed size and length cannot be altered at run time. • Insertion and deletion of elements may require reshuffling of array elements. Thus, array manipulation is time-consuming and inefficient.
  • 12. Linked List • A linked list is a linear collection of data elements, called nodes, where the linear order is maintained by means of pointers. • Linked list is otherwise called one way list or singly linked list. • Self referential strcuture
  • 13. Node The basic building block of a list is a node. A node consists of two parts: • Data part that contain the information about the element, and • Link or next pointer field contains the address of the next node in the list
  • 14. struct node { int data; struct node *link; } ; Memory allocation for a node • Struct node *p, *q, *start; • P= (strcut node*) malloc( sizeof(strcut node)); p->data = 10; • Q= (strcut node*) malloc (sizeof(Strcut node)); q->data =20; • P->link = Q ; q->link = NULL; start =p;
  • 15. So, A linked list is • Linear DS • Heterogeneous • Dynamic • Non contigeous
  • 16. Operations on Linked List Linked List is a dynamic data structure which supports following operations. • Traversal • Insertion • Deletion • Searching • Inverting