SlideShare a Scribd company logo
1 of 18
Download to read offline
SELF-REFERENTIAL STRUCTURES
Consider the structure declaration below,
struct NODE {
struct NODE new; /* 'new' declared variable */
int value;
};
As we know that structure template tells compiler how to allocate storage to its members and
makes computer not to allocate memory to them. When compiler reaches the line
struct NODE new;
template struct NODE is not fully defined. Further, its member ‘new’ of type struct NODE
contains a member ‘new’ of type struct NODE which in turn contains a member ‘new’ again of
type struct NODE and so on indefinitely. In such an instance, compiler can’t evaluate correctly
how much storage to allocate to ‘new’ member of template struct NODE. So we observed here
that a member of struct NODE can’t be a variable of type struct NODE. Then, how can we
make structure struct NODE self-referential? Let’s take one more try, this time we declare a
‘pointer-to-struct NODE’ as a member of template struct NODE,
struct NODE {
struct NODE *new; /* 'new' a pointer-to-struct NODE */
int value;
};
As compiler starts compiling the template struct NODE and reaches line
struct NODE *new;
it finds ‘new’, a ‘pointer-to-struct NODE’, and also member of struct NODE template, it
evaluates correctly how much bytes of storage to be allocated to ‘new’. On linux system, any
pointer type takes 8 bytes of storage. There’s no problem in using ‘pointer-to-struct NODE’ as a
member of struct NODE. Because ‘new’ is a ‘pointer-to-struct NODE’, structure struct NODE is
called self-referential structure.
typedef struct NODE {
struct NODE *new;
int value;
}Node;
int main(void)
{
Node previous, current;
/* accessing members of 'previous' */
previous.new = &current;
/* previous.new is a 'pointer-to-struct NODE' */
previous.value = 100;
}
In above fragment of code, ‘previous.new’ is pointing to ‘current’ Node.
Self-referential structures have their applications in Advanced Data Structures like, Linked
Lists, Binary Trees etc..
C Dynamic Memory Allocation
In C, the exact size of array is unknown until compile time, i.e., the time when a compiler
compiles your code into a computer understandable language. So, sometimes the size of the
array can be insufficient or more than required.
Dynamic memory allocation allows your program to obtain more memory space while running,
or to release it if it's not required.
In simple terms, Dynamic memory allocation allows you to manually handle memory space for
your program.
Although, C language inherently does not have any technique to allocate memory dynamically,
there are 4 library functions under "stdlib.h" for dynamic memory allocation.
Function Use of Function
malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space
calloc()
Allocates space for an array elements, initializes to zero and then returns a pointer
to memory
free() deallocate the previously allocated space
realloc() Change the size of previously allocated space
C malloc()
The name malloc stands for "memory allocation".
The function malloc() reserves a block of memory of specified size and return a pointer of
type void which can be casted into pointer of any form.
Syntax of malloc()
ptr = (cast-type*) malloc(byte-size)
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of
memory with size of byte size. If the space is insufficient, allocation fails and returns NULL
pointer.
ptr = (int*) malloc(100 * sizeof(int));
This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively
and the pointer points to the address of first byte of memory.
C calloc()
The name calloc stands for "contiguous allocation".
The only difference between malloc() and calloc() is that, malloc() allocates single block of
memory whereas calloc() allocates multiple blocks of memory each of same size and sets all
bytes to zero.
Syntax of calloc()
ptr = (cast-type*)calloc(n, element-size);
This statement will allocate contiguous space in memory for an array of n elements. For
example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for an array of 25 elements each of size of
float, i.e, 4 bytes.
C free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its
own. You must explicitly use free() to release the space.
syntax of free()
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
Example #1: Using C malloc() and free()
Write a C program to find sum of n elements entered by user. To perform this program, allocate
memory dynamically using malloc() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Example #2: Using C calloc() and free()
Write a C program to find sum of n elements entered by user. To perform this program, allocate
memory dynamically using calloc() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
C realloc()
If the previously allocated memory is insufficient or more than required, you can change the
previously allocated memory size using realloc().
Syntax of realloc()
ptr = realloc(ptr, newsize);
Here, ptr is reallocated with size of newsize.
Example #3: Using realloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size of array: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Address of previously allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%ut",ptr + i);
printf("nEnter new size of array: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2);
for(i = 0; i < n2; ++i)
printf("%ut", ptr + i);
return 0;
}
Linked lists in C (Singly linked list)
Linked list is one of the most important data structures. We often
face situations, where the data is dynamic in nature and number of
data can’t be predicted or the number of data keeps changing during
program execution. Linked lists are very useful in this type of
situations.
Dynamic Memory Allocation
SELF-REFERENTIAL STRUCTURES
Linked lists
A linked list is made up of many nodes which are connected in
nature. Every node is mainly divided into two parts, one part holds
the data and the other part is connected to a different node. It is
similar to the picture given below.
Here, each node contains a data member (the upper part of the
picture) and link to another node(lower part of the picture).
Notice that the last node doesn’t point to any other node and just
stores NULL.
In C, we achieve this functionality by using structures and pointers.
Each structure represents a node having some data and also a pointer
to another structure of the same kind. This pointer holds the address
of the next node and creates the link between two nodes. So, the
structure is something like:
struct node
{
int data;
struct node *next;
};
The first data member of the structure (named node) is an integer to
hold an integer and the second data member is the pointer to a node
(same structure). This means that the second data member holds the
address of the next node and in this way, every node is connected as
represented in the picture above.
The picture representing the above structure is given below.
And the picture representing the linked list is:
So, if we have access to the first node then we can access any node of
the linked list. For example, if ‘a’ is a node then a->next is the node
next to the ‘a’ (the pointer storing the address of the next node is
named ‘next’).
One thing you should notice here is that we can easily access the next
node but there is no way of accessing the previous node and this is the
limitation of singly linked list.
Coding up a linked list
The first part is to create a node (structure).
struct node
{
int data;
struct node *next;
};
The second and the most important part of a linked list is to always
keep the track of the first node because access to the first node means
access to the entire list. So, let’s call our first node as ‘ head’.
int main()
{
struct node *prev,*head,*p;
return 0;
}
We have made three nodes – head, prev and p. You will see the
function of prev in the explanation of the next block of code.
Now, let’s create a node ‘p’.
int main()
{
struct node *p;
p=malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
return 0;
}
p=malloc(sizeof(struct node)) – We are allocating the space required
for a node by the malloc function. Now, ‘p’ points to a node (or space
allocated for the node).
scanf("%d",&p->data) – We are giving a value to the ‘data’ of ‘p’ after
taking the input from the user.
p->next=NULL – We have given the value to ‘data’ in the previous line
and a value of the pointer ‘next’ (NULL) in this line and thus making
our node ‘p’ complete.
Let’s create our linked list by joining the nodes.
int main()
{
struct node *prev,*head,*p;
int n,i;
printf ("number of elements:");
scanf("%d",&n);
head=NULL;
for(i=0;i<n;i++)
{
p=malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
if(head==NULL) head=p;
else prev->next=p;
prev=p;
}
return 0; }
We are storing n number of elements in our linked list.
if(head==NULL) – If the ‘head’ is NULL, then our linked list is not created.
head=p – We have given the value to the ‘head’ and thus made the first
node of our linked list.
else – The linked list is already there and we just have to add a node in
this linked list.
prev->next=p – We have used the prev to store the record of the previous
node to the current node (the last node from the previous iteration)
(you will see in the next line). The ‘next’ of this prev is holding NULL
till now. We pointed it to the node ‘p’ and hence added a node to our
linked list.
prev=p – We made the last node ‘prev’ for the next iteration.
Code with all operations:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
display(struct node *head)
{
if(head == NULL)
{
printf("NULLn");
}
else
{
printf("|%d||Link|->", head -> data);
display(head->next);
}
}
struct node* front(struct node *head)
{
struct node *p;
p=malloc(sizeof(struct node));
printf("Enter the value to be inserted n");
scanf("%d",&p->data);
p->next=head;
return (p);
}
struct node* deletf(struct node *a)
{
struct node *temp;
if(a==NULL)
printf("NO elements in list");
else
{
temp=a;
a=a->next;
free(temp);
}
return a;
}
struct node* end(struct node *head)
{
struct node *p,*q;
p=malloc(sizeof(struct node));
printf("Enter the value to be inserted n");
scanf("%d",&p->data);
p->next=NULL;
if(head==NULL)
{
head=p;
}
else{
q=head;
while(q->next!=NULL)
{
q = q->next;
}
q->next = p;
}
return head;
}
struct node* deletr(struct node *head)
{
struct node *p,*q;
q=head;
if(q->next==NULL)
{
return NULL;
}
while(q->next->next!=NULL)
{
q=q->next;
}
p=q->next;
q->next=NULL;
return head;
}
after(struct node *a, int key)
{
struct node *p,*temp;
temp=a;
if(a==NULL)
{
printf("NO nodes in list");
return;
}
while(temp!=NULL)
{
if(temp->data==key)
{
p=malloc(sizeof(struct node));
printf("Enter the value to be insertedn");
scanf("%d",&p->data);
p->next=temp->next;
temp->next=p;
}
temp=temp->next;
}
return;
}
struct node* deletat(struct node *a,int key)
{
struct node *p,*temp;
temp=a;
if(a==NULL)
{
printf("NO nodes in list");
return;
}
printf("%d",a->data);
if(a->data==key)
{
return(deletf(a));
}
while(temp!=NULL)
{
if(temp->next==NULL)
{
return NULL;
}
if(temp->next->data==key)
{
p=temp->next;
temp->next=p->next;
//
free(p);
}
temp=temp->next;
}
return a;
}
int main()
{
struct node *prev,*head, *p;
int n,i,key;
head=NULL;
for(;;)
{
printf("Enter the option n1.displayn2.insertn3.delete");
scanf("%d",&key);
switch (key)
{
case 2: printf("Enter the option n1.Ifrontn2.Iend n3.Ibetween");
scanf("%d",&key);
switch(key)
{
case 1: head = front(head);break;
case 2: head=end(head);break;
case 3:printf("Enter the value to be searched");
scanf("%d",&key);
after(head,key);break;
}
break;
case 1:display(head);break;
case 3:
printf("Enter the option n1.Dfrontn2.Dend n3.Dbetween");
scanf("%d",&key);
switch(key)
{
case 1:head=deletf(head);break;
case 2:head=deletr(head);break;
case 3:printf("Enter the value to be searched");
scanf("%d",&key);
deletat(head,key);break;
}
break;
default:return 0;
}
}
}

More Related Content

What's hot

Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Gamindu Udayanga
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointerLei Yu
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Ali Aminian
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory ManagementAnil Bapat
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - PointersWingston
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structurelodhran-hayat
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 

What's hot (20)

Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointer
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
This pointer
This pointerThis pointer
This pointer
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
Modern C++
Modern C++Modern C++
Modern C++
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Advanced C - Part 3
Advanced C - Part 3Advanced C - Part 3
Advanced C - Part 3
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 

Similar to Linked list

DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxVeerannaKotagi1
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxssuser688516
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptxjack881
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndEdward Chen
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&aKumaran K
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6Nitay Neeman
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxkrishna50blogging
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.pptshivani366010
 

Similar to Linked list (20)

DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docx
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
dynamic_v1-3.pptx
dynamic_v1-3.pptxdynamic_v1-3.pptx
dynamic_v1-3.pptx
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
Structures
StructuresStructures
Structures
 
Op ps
Op psOp ps
Op ps
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
06 linked list
06 linked list06 linked list
06 linked list
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptx
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 

Recently uploaded

Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationBoston Institute of Analytics
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 

Recently uploaded (20)

Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health Classification
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 

Linked list

  • 1. SELF-REFERENTIAL STRUCTURES Consider the structure declaration below, struct NODE { struct NODE new; /* 'new' declared variable */ int value; }; As we know that structure template tells compiler how to allocate storage to its members and makes computer not to allocate memory to them. When compiler reaches the line struct NODE new; template struct NODE is not fully defined. Further, its member ‘new’ of type struct NODE contains a member ‘new’ of type struct NODE which in turn contains a member ‘new’ again of type struct NODE and so on indefinitely. In such an instance, compiler can’t evaluate correctly how much storage to allocate to ‘new’ member of template struct NODE. So we observed here that a member of struct NODE can’t be a variable of type struct NODE. Then, how can we make structure struct NODE self-referential? Let’s take one more try, this time we declare a ‘pointer-to-struct NODE’ as a member of template struct NODE, struct NODE { struct NODE *new; /* 'new' a pointer-to-struct NODE */ int value; }; As compiler starts compiling the template struct NODE and reaches line struct NODE *new; it finds ‘new’, a ‘pointer-to-struct NODE’, and also member of struct NODE template, it evaluates correctly how much bytes of storage to be allocated to ‘new’. On linux system, any pointer type takes 8 bytes of storage. There’s no problem in using ‘pointer-to-struct NODE’ as a member of struct NODE. Because ‘new’ is a ‘pointer-to-struct NODE’, structure struct NODE is called self-referential structure. typedef struct NODE { struct NODE *new; int value; }Node; int main(void) { Node previous, current; /* accessing members of 'previous' */ previous.new = &current;
  • 2. /* previous.new is a 'pointer-to-struct NODE' */ previous.value = 100; } In above fragment of code, ‘previous.new’ is pointing to ‘current’ Node. Self-referential structures have their applications in Advanced Data Structures like, Linked Lists, Binary Trees etc.. C Dynamic Memory Allocation In C, the exact size of array is unknown until compile time, i.e., the time when a compiler compiles your code into a computer understandable language. So, sometimes the size of the array can be insufficient or more than required. Dynamic memory allocation allows your program to obtain more memory space while running, or to release it if it's not required. In simple terms, Dynamic memory allocation allows you to manually handle memory space for your program. Although, C language inherently does not have any technique to allocate memory dynamically, there are 4 library functions under "stdlib.h" for dynamic memory allocation. Function Use of Function malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory free() deallocate the previously allocated space realloc() Change the size of previously allocated space C malloc() The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.
  • 3. Syntax of malloc() ptr = (cast-type*) malloc(byte-size) Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer. ptr = (int*) malloc(100 * sizeof(int)); This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory. C calloc() The name calloc stands for "contiguous allocation". The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero. Syntax of calloc() ptr = (cast-type*)calloc(n, element-size); This statement will allocate contiguous space in memory for an array of n elements. For example: ptr = (float*) calloc(25, sizeof(float)); This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.
  • 4. C free() Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space. syntax of free() free(ptr); This statement frees the space allocated in the memory pointed by ptr. Example #1: Using C malloc() and free() Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc() function. #include <stdio.h> #include <stdlib.h> int main() { int num, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &num); ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements of array: ");
  • 5. for(i = 0; i < num; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); free(ptr); return 0; } Example #2: Using C calloc() and free() Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using calloc() function. #include <stdio.h> #include <stdlib.h> int main() { int num, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &num); ptr = (int*) calloc(num, sizeof(int)); if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements of array: "); for(i = 0; i < num; ++i)
  • 6. { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); free(ptr); return 0; } C realloc() If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc(). Syntax of realloc() ptr = realloc(ptr, newsize); Here, ptr is reallocated with size of newsize. Example #3: Using realloc() #include <stdio.h> #include <stdlib.h> int main() { int *ptr, i , n1, n2; printf("Enter size of array: "); scanf("%d", &n1); ptr = (int*) malloc(n1 * sizeof(int));
  • 7. printf("Address of previously allocated memory: "); for(i = 0; i < n1; ++i) printf("%ut",ptr + i); printf("nEnter new size of array: "); scanf("%d", &n2); ptr = realloc(ptr, n2); for(i = 0; i < n2; ++i) printf("%ut", ptr + i); return 0; } Linked lists in C (Singly linked list) Linked list is one of the most important data structures. We often face situations, where the data is dynamic in nature and number of data can’t be predicted or the number of data keeps changing during program execution. Linked lists are very useful in this type of situations. Dynamic Memory Allocation SELF-REFERENTIAL STRUCTURES Linked lists
  • 8. A linked list is made up of many nodes which are connected in nature. Every node is mainly divided into two parts, one part holds the data and the other part is connected to a different node. It is similar to the picture given below. Here, each node contains a data member (the upper part of the picture) and link to another node(lower part of the picture). Notice that the last node doesn’t point to any other node and just stores NULL. In C, we achieve this functionality by using structures and pointers. Each structure represents a node having some data and also a pointer to another structure of the same kind. This pointer holds the address of the next node and creates the link between two nodes. So, the structure is something like: struct node { int data; struct node *next; };
  • 9. The first data member of the structure (named node) is an integer to hold an integer and the second data member is the pointer to a node (same structure). This means that the second data member holds the address of the next node and in this way, every node is connected as represented in the picture above. The picture representing the above structure is given below. And the picture representing the linked list is: So, if we have access to the first node then we can access any node of the linked list. For example, if ‘a’ is a node then a->next is the node next to the ‘a’ (the pointer storing the address of the next node is named ‘next’). One thing you should notice here is that we can easily access the next node but there is no way of accessing the previous node and this is the limitation of singly linked list.
  • 10. Coding up a linked list The first part is to create a node (structure). struct node { int data; struct node *next; }; The second and the most important part of a linked list is to always keep the track of the first node because access to the first node means access to the entire list. So, let’s call our first node as ‘ head’. int main() { struct node *prev,*head,*p; return 0; } We have made three nodes – head, prev and p. You will see the function of prev in the explanation of the next block of code. Now, let’s create a node ‘p’. int main() { struct node *p; p=malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; return 0; }
  • 11. p=malloc(sizeof(struct node)) – We are allocating the space required for a node by the malloc function. Now, ‘p’ points to a node (or space allocated for the node). scanf("%d",&p->data) – We are giving a value to the ‘data’ of ‘p’ after taking the input from the user. p->next=NULL – We have given the value to ‘data’ in the previous line and a value of the pointer ‘next’ (NULL) in this line and thus making our node ‘p’ complete. Let’s create our linked list by joining the nodes. int main() { struct node *prev,*head,*p; int n,i; printf ("number of elements:"); scanf("%d",&n); head=NULL; for(i=0;i<n;i++) { p=malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; if(head==NULL) head=p; else prev->next=p; prev=p; } return 0; } We are storing n number of elements in our linked list.
  • 12. if(head==NULL) – If the ‘head’ is NULL, then our linked list is not created. head=p – We have given the value to the ‘head’ and thus made the first node of our linked list. else – The linked list is already there and we just have to add a node in this linked list. prev->next=p – We have used the prev to store the record of the previous node to the current node (the last node from the previous iteration) (you will see in the next line). The ‘next’ of this prev is holding NULL till now. We pointed it to the node ‘p’ and hence added a node to our linked list. prev=p – We made the last node ‘prev’ for the next iteration. Code with all operations: #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; display(struct node *head) { if(head == NULL) { printf("NULLn");
  • 13. } else { printf("|%d||Link|->", head -> data); display(head->next); } } struct node* front(struct node *head) { struct node *p; p=malloc(sizeof(struct node)); printf("Enter the value to be inserted n"); scanf("%d",&p->data); p->next=head; return (p); } struct node* deletf(struct node *a) { struct node *temp; if(a==NULL) printf("NO elements in list"); else { temp=a; a=a->next; free(temp); } return a;
  • 14. } struct node* end(struct node *head) { struct node *p,*q; p=malloc(sizeof(struct node)); printf("Enter the value to be inserted n"); scanf("%d",&p->data); p->next=NULL; if(head==NULL) { head=p; } else{ q=head; while(q->next!=NULL) { q = q->next; } q->next = p; } return head; } struct node* deletr(struct node *head) { struct node *p,*q; q=head; if(q->next==NULL) {
  • 15. return NULL; } while(q->next->next!=NULL) { q=q->next; } p=q->next; q->next=NULL; return head; } after(struct node *a, int key) { struct node *p,*temp; temp=a; if(a==NULL) { printf("NO nodes in list"); return; } while(temp!=NULL) { if(temp->data==key) { p=malloc(sizeof(struct node)); printf("Enter the value to be insertedn"); scanf("%d",&p->data); p->next=temp->next; temp->next=p; }
  • 16. temp=temp->next; } return; } struct node* deletat(struct node *a,int key) { struct node *p,*temp; temp=a; if(a==NULL) { printf("NO nodes in list"); return; } printf("%d",a->data); if(a->data==key) { return(deletf(a)); } while(temp!=NULL) { if(temp->next==NULL) { return NULL; } if(temp->next->data==key) { p=temp->next; temp->next=p->next;
  • 17. // free(p); } temp=temp->next; } return a; } int main() { struct node *prev,*head, *p; int n,i,key; head=NULL; for(;;) { printf("Enter the option n1.displayn2.insertn3.delete"); scanf("%d",&key); switch (key) { case 2: printf("Enter the option n1.Ifrontn2.Iend n3.Ibetween"); scanf("%d",&key); switch(key) { case 1: head = front(head);break; case 2: head=end(head);break; case 3:printf("Enter the value to be searched"); scanf("%d",&key); after(head,key);break; } break;
  • 18. case 1:display(head);break; case 3: printf("Enter the option n1.Dfrontn2.Dend n3.Dbetween"); scanf("%d",&key); switch(key) { case 1:head=deletf(head);break; case 2:head=deletr(head);break; case 3:printf("Enter the value to be searched"); scanf("%d",&key); deletat(head,key);break; } break; default:return 0; } } }