SlideShare a Scribd company logo
linked list.pptx
Linked List
 Linked List can be defined as collection of objects called nodes that are
randomly stored in the memory.
 A node contains two fields i.e. data stored at that particular address and the
pointer which contains the address of the next node in the memory.
 The last node of the list contains pointer to the null.
Uses of Linked List
• The list is not required to be contiguously present in the memory. The node
can reside any where in the memory and linked together to make a list. This
achieves optimized utilization of space.
• list size is limited to the memory size and doesn't need to be declared in
advance.
• Empty node can not be present in the linked list.
• We can store values of primitive types or objects in the singly linked list.
Node Creation :
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node *));
Insertion in Linked list
SN Operation Description
1 Insertion at beginning It involves inserting any element at the front of the list. We
just need to a few link adjustments to make the new node
as the head of the list.
2 Insertion at end of the list It involves insertion at the last of the linked list. The new
node can be inserted as the only node in the list or it can
be inserted as the last one. Different logics are
implemented in each scenario.
3 Insertion after specified
node
It involves insertion after the specified node of the linked
list. We need to skip the desired number of nodes in order
to reach the node after which the new node will be
inserted. .
Insertion at the Beginning
struct node *ptr;
int item;
ptr = (struct node *)
malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter valuen");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("nNode inserted");
}
Insertion At Last
struct node *ptr,*temp;
int item;
ptr = (struct
node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter value?n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("nNode inserted");
}
}
}
Random Insert at Position
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("nEnter the location after which you want to insert ");
scanf("n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("ncan't insertn");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("nNode inserted");
}
Begin Delete
struct node *ptr;
if(head == NULL)
{
printf("nList is emptyn");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("nNode deleted from the begining ...n");
}
Delete Last node
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("nOnly node of the list deleted ...n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("nDeleted Node from the last ...n");
}
Random Delete node at Position
struct node *ptr,*ptr1;
int loc,i;
printf("n Enter the location of the node after which you want to perform deletion n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("nDeleted node %d ",loc+1);
Find node in Linked List
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("nEmpty Listn");
}
else
{
printf("nEnter item which you want to
search?n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d
",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not foundn");
}
}
Display Linked List
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("nprinting values . . . . .n");
while (ptr!=NULL)
{
printf("n%d",ptr->data);
ptr = ptr -> next;
}
}
DOUBLY LINKED LIST
What is a doubly-linked list?
A doubly linked list is another type of the linked list. It is called a doubly linked list because it
contains two addresses while a singly linked list contains a single address. It is a list that has
total three parts, one is a data part, and others two are the pointers, i.e., previous and next.
The previous pointer holds the address of the previous node, and the next pointer holds the
address of the next node. Therefore, we can say that list has two references, i.e., forward and
backward reference to traverse in either direction.
In c the structure of a node is describe as :
struct node
{
struct node *prev;
int data;
struct node *next;
}
 Memory Representation of a doubly linked list is shown in the following image.
Generally, doubly linked list consumes more space for every node and therefore,
causes more expansive basic operations such as insertion and deletion. However, we
can easily manipulate the elements of the list since the list maintains pointers in both
the directions (forward and backward).
 In the following image, the first element of the list that is i.e. 13 stored at address 1.
The head pointer points to the starting address 1. Since this is the first element being
added to the list therefore the prev of the list contains null. The next node of the list
resides at address 4 therefore the first node contains 4 in its next pointer.
 We can traverse the list in this way until we find any node containing null or -1 in its
next part.
MEMORY REPRESENTATION OF A DOUBLY LINKED
LIST
Insert at Begining
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("nNode insertedn");
}
Insert at Last
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("nnode insertedn");
Insert at Position
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=1;i<loc-1; i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&ptr->data);
ptr -> prev = temp;
ptr->next = temp->next;
temp->next = ptr;
temp->next->prev=ptr;
printf("nnode insertedn");
}
Deletion at Begining
struct node *ptr;
if(head == NULL)
{
printf("n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("n node deletedn");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("nnode deletedn");
}
Deletion at last
struct node *ptr;
if(head == NULL)
{
printf("n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("nnode deletedn");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("nnode deletedn");
}
Deletion at Position
struct node *ptr, *temp;
int val;
printf("n Enter the data after which the node is to be del
eted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("nCan't deleten");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("nnode deletedn");
}
CIRCULAR LINKED LIST
 In a circular Singly linked list, the last node of the list contains a pointer to the
first node of the list. We can have circular singly linked list as well as circular
doubly linked list.
 We traverse a circular singly linked list until we reach the same node where we
started. The circular singly liked list has no beginning and no ending. There is no
null value present in the next part of any of the nodes.
 The following image shows a circular singly linked list.
Begin Insert
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("nnode insertedn");
}
Last Insert
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOWn");
}
else
{
printf("nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("nnode insertedn");
}
Begin Delete
struct node *ptr;
if(head == NULL)
{
printf("nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("nnode deletedn");
}
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("nnode deletedn");
}
Last Delete
struct node *ptr, *preptr;
if(head==NULL)
{
printf("nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("nnode deletedn");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("nnode deletedn");
}
Search node
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("nEmpty Listn");
}
else
{
printf("nEnter item which you want to
search?n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1);
flag=0;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d
",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not foundn");
}
}
Display linked list
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("nnothing to print");
}
else
{
printf("n printing values ... n");
while(ptr -> next != head)
{
printf("%dn", ptr -> data);
ptr = ptr -> next;
}
printf("%dn", ptr -> data);
}
Dynamic memory allocation in C
The concept of dynamic memory allocation in c language
enables the C programmer to allocate memory at runtime.
Dynamic memory allocation in c language is possible by 4
functions of stdlib.h header file.
malloc()
calloc()
realloc()
free()
Before learning above functions, let's understand the difference
between static memory allocation and dynamic memory
allocation.
static memory allocation dynamic memory allocation
memory is allocated at compile
time.
memory is allocated at run
time.
memory can't be increased
while executing program.
memory can be increased
while executing program.
used in array. used in linked list.
malloc() allocates single block of
requested memory.
calloc() allocates multiple block of
requested memory.
realloc() reallocates the memory
occupied by malloc() or
calloc() functions.
free() frees the dynamically allocated
memory.
malloc
ptr=(cast-type*)malloc(byte-size)
Calloc
ptr=(cast-t ype*)calloc(number, byte-size)
 The calloc() function allocates multiple block of requested
memory.
 It initially initialize all bytes to zero.
 It returns NULL if memory is not sufficient.

More Related Content

PPTX
Recursion in Data Structure
PPT
PPTX
PPT
Two dimensional array
PPTX
Graph in data structure
PPTX
Linked list
PPTX
Linear data structure concepts
Recursion in Data Structure
Two dimensional array
Graph in data structure
Linked list
Linear data structure concepts

What's hot (20)

PPTX
Huffman's algorithm in Data Structure
PDF
Sorting Algorithms
PPTX
Binary Tree in Data Structure
PPTX
Double Linked List (Algorithm)
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Collision in Hashing.pptx
PPTX
heap Sort Algorithm
PPTX
Recursive Function
PPTX
Tree in data structure
PPTX
Pointers in c++
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPSX
Data Structure (Tree)
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPTX
Nested structure (Computer programming and utilization)
PDF
Python list
PDF
linear search and binary search
PDF
Singly linked list
PPTX
PPT
1.1 binary tree
PPTX
Threaded binary tree
Huffman's algorithm in Data Structure
Sorting Algorithms
Binary Tree in Data Structure
Double Linked List (Algorithm)
Array Introduction One-dimensional array Multidimensional array
Collision in Hashing.pptx
heap Sort Algorithm
Recursive Function
Tree in data structure
Pointers in c++
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Data Structure (Tree)
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Nested structure (Computer programming and utilization)
Python list
linear search and binary search
Singly linked list
1.1 binary tree
Threaded binary tree
Ad

Similar to linked list.pptx (20)

PDF
DS Module 03.pdf
PPTX
Implemention of Linked list concept in Data Structures
PPTX
linkedlistforslideshare-210123143943.pptx
PPTX
Linked list
PPTX
Deleting a node from the list(SINGLE LINKED LIST)
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PPT
DS Unit 2.ppt
PDF
Linked list
PPTX
Linked Lists, Single Linked list and its operations
PDF
Document on Linked List as a presentation
PDF
Document on Linked List as a presentation
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
PPT
Unit ii(dsc++)
PPTX
Doubly & Circular Linked Lists
PPTX
DSModule2.pptx
PPTX
Unit 5 linked list
PPTX
linkedlist.pptx
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
PPTX
LINKED LIST.pptx
DS Module 03.pdf
Implemention of Linked list concept in Data Structures
linkedlistforslideshare-210123143943.pptx
Linked list
Deleting a node from the list(SINGLE LINKED LIST)
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
DS Unit 2.ppt
Linked list
Linked Lists, Single Linked list and its operations
Document on Linked List as a presentation
Document on Linked List as a presentation
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
Unit ii(dsc++)
Doubly & Circular Linked Lists
DSModule2.pptx
Unit 5 linked list
linkedlist.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
LINKED LIST.pptx
Ad

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Business Ethics Teaching Materials for college
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
TR - Agricultural Crops Production NC III.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Pharma ospi slides which help in ospi learning
PPH.pptx obstetrics and gynecology in nursing
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Business Ethics Teaching Materials for college
Cardiovascular Pharmacology for pharmacy students.pptx
GDM (1) (1).pptx small presentation for students
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Anesthesia in Laparoscopic Surgery in India
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF

linked list.pptx

  • 2. Linked List  Linked List can be defined as collection of objects called nodes that are randomly stored in the memory.  A node contains two fields i.e. data stored at that particular address and the pointer which contains the address of the next node in the memory.  The last node of the list contains pointer to the null. Uses of Linked List • The list is not required to be contiguously present in the memory. The node can reside any where in the memory and linked together to make a list. This achieves optimized utilization of space. • list size is limited to the memory size and doesn't need to be declared in advance. • Empty node can not be present in the linked list. • We can store values of primitive types or objects in the singly linked list.
  • 3. Node Creation : struct node { int data; struct node *next; }; struct node *head, *ptr; ptr = (struct node *)malloc(sizeof(struct node *)); Insertion in Linked list SN Operation Description 1 Insertion at beginning It involves inserting any element at the front of the list. We just need to a few link adjustments to make the new node as the head of the list. 2 Insertion at end of the list It involves insertion at the last of the linked list. The new node can be inserted as the only node in the list or it can be inserted as the last one. Different logics are implemented in each scenario. 3 Insertion after specified node It involves insertion after the specified node of the linked list. We need to skip the desired number of nodes in order to reach the node after which the new node will be inserted. .
  • 4. Insertion at the Beginning struct node *ptr; int item; ptr = (struct node *) malloc(sizeof(struct node *)); if(ptr == NULL) { printf("nOVERFLOW"); } else { printf("nEnter valuen"); scanf("%d",&item); ptr->data = item; ptr->next = head; head = ptr; printf("nNode inserted"); } Insertion At Last struct node *ptr,*temp; int item; ptr = (struct node*)malloc(sizeof(struct node)); if(ptr == NULL) { printf("nOVERFLOW"); } else { printf("nEnter value?n"); scanf("%d",&item); ptr->data = item; if(head == NULL) { ptr -> next = NULL; head = ptr; printf("nNode inserted"); } else { temp = head; while (temp -> next != NULL) { temp = temp -> next; } temp->next = ptr; ptr->next = NULL; printf("nNode inserted"); } } }
  • 5. Random Insert at Position int i,loc,item; struct node *ptr, *temp; ptr = (struct node *) malloc (sizeof(struct node)); if(ptr == NULL) { printf("nOVERFLOW"); } else { printf("nEnter element value"); scanf("%d",&item); ptr->data = item; printf("nEnter the location after which you want to insert "); scanf("n%d",&loc); temp=head; for(i=0;i<loc;i++) { temp = temp->next; if(temp == NULL) { printf("ncan't insertn"); return; } } ptr ->next = temp ->next; temp ->next = ptr; printf("nNode inserted"); }
  • 6. Begin Delete struct node *ptr; if(head == NULL) { printf("nList is emptyn"); } else { ptr = head; head = ptr->next; free(ptr); printf("nNode deleted from the begining ...n"); } Delete Last node struct node *ptr,*ptr1; if(head == NULL) { printf("nlist is empty"); } else if(head -> next == NULL) { head = NULL; free(head); printf("nOnly node of the list deleted ...n"); } else { ptr = head; while(ptr->next != NULL) { ptr1 = ptr; ptr = ptr ->next; } ptr1->next = NULL; free(ptr); printf("nDeleted Node from the last ...n"); }
  • 7. Random Delete node at Position struct node *ptr,*ptr1; int loc,i; printf("n Enter the location of the node after which you want to perform deletion n"); scanf("%d",&loc); ptr=head; for(i=0;i<loc;i++) { ptr1 = ptr; ptr = ptr->next; if(ptr == NULL) { printf("nCan't delete"); return; } } ptr1 ->next = ptr ->next; free(ptr); printf("nDeleted node %d ",loc+1);
  • 8. Find node in Linked List struct node *ptr; int item,i=0,flag; ptr = head; if(ptr == NULL) { printf("nEmpty Listn"); } else { printf("nEnter item which you want to search?n"); scanf("%d",&item); while (ptr!=NULL) { if(ptr->data == item) { printf("item found at location %d ",i+1); flag=0; } else { flag=1; } i++; ptr = ptr -> next; } if(flag==1) { printf("Item not foundn"); } }
  • 9. Display Linked List struct node *ptr; ptr = head; if(ptr == NULL) { printf("Nothing to print"); } else { printf("nprinting values . . . . .n"); while (ptr!=NULL) { printf("n%d",ptr->data); ptr = ptr -> next; } }
  • 10. DOUBLY LINKED LIST What is a doubly-linked list? A doubly linked list is another type of the linked list. It is called a doubly linked list because it contains two addresses while a singly linked list contains a single address. It is a list that has total three parts, one is a data part, and others two are the pointers, i.e., previous and next. The previous pointer holds the address of the previous node, and the next pointer holds the address of the next node. Therefore, we can say that list has two references, i.e., forward and backward reference to traverse in either direction. In c the structure of a node is describe as : struct node { struct node *prev; int data; struct node *next; }
  • 11.  Memory Representation of a doubly linked list is shown in the following image. Generally, doubly linked list consumes more space for every node and therefore, causes more expansive basic operations such as insertion and deletion. However, we can easily manipulate the elements of the list since the list maintains pointers in both the directions (forward and backward).  In the following image, the first element of the list that is i.e. 13 stored at address 1. The head pointer points to the starting address 1. Since this is the first element being added to the list therefore the prev of the list contains null. The next node of the list resides at address 4 therefore the first node contains 4 in its next pointer.  We can traverse the list in this way until we find any node containing null or -1 in its next part. MEMORY REPRESENTATION OF A DOUBLY LINKED LIST
  • 12. Insert at Begining struct node *ptr; int item; ptr = (struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { printf("nOVERFLOW"); } else { printf("nEnter Item value"); scanf("%d",&item); if(head==NULL) { ptr->next = NULL; ptr->prev=NULL; ptr->data=item; head=ptr; } else { ptr->data=item; ptr->prev=NULL; ptr->next = head; head->prev=ptr; head=ptr; } printf("nNode insertedn"); } Insert at Last struct node *ptr,*temp; int item; ptr = (struct node *) malloc(sizeof(struct node)); if(ptr == NULL) { printf("nOVERFLOW"); } else { printf("nEnter value"); scanf("%d",&item); ptr->data=item; if(head == NULL) { ptr->next = NULL; ptr->prev = NULL; head = ptr; } else { temp = head; while(temp->next!=NULL) { temp = temp->next; } temp->next = ptr; ptr ->prev=temp; ptr->next = NULL; } } printf("nnode insertedn");
  • 13. Insert at Position struct node *ptr,*temp; int item,loc,i; ptr = (struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { printf("n OVERFLOW"); } else { temp=head; printf("Enter the location"); scanf("%d",&loc); for(i=1;i<loc-1; i++) { temp = temp->next; if(temp == NULL) { printf("n There are less than %d elements", loc); return; } } printf("Enter value"); scanf("%d",&ptr->data); ptr -> prev = temp; ptr->next = temp->next; temp->next = ptr; temp->next->prev=ptr; printf("nnode insertedn"); } Deletion at Begining struct node *ptr; if(head == NULL) { printf("n UNDERFLOW"); } else if(head->next == NULL) { head = NULL; free(head); printf("n node deletedn"); } else { ptr = head; head = head -> next; head -> prev = NULL; free(ptr); printf("nnode deletedn"); }
  • 14. Deletion at last struct node *ptr; if(head == NULL) { printf("n UNDERFLOW"); } else if(head->next == NULL) { head = NULL; free(head); printf("nnode deletedn"); } else { ptr = head; if(ptr->next != NULL) { ptr = ptr -> next; } ptr -> prev -> next = NULL; free(ptr); printf("nnode deletedn"); } Deletion at Position struct node *ptr, *temp; int val; printf("n Enter the data after which the node is to be del eted : "); scanf("%d", &val); ptr = head; while(ptr -> data != val) ptr = ptr -> next; if(ptr -> next == NULL) { printf("nCan't deleten"); } else if(ptr -> next -> next == NULL) { ptr ->next = NULL; } else { temp = ptr -> next; ptr -> next = temp -> next; temp -> next -> prev = ptr; free(temp); printf("nnode deletedn"); }
  • 15. CIRCULAR LINKED LIST  In a circular Singly linked list, the last node of the list contains a pointer to the first node of the list. We can have circular singly linked list as well as circular doubly linked list.  We traverse a circular singly linked list until we reach the same node where we started. The circular singly liked list has no beginning and no ending. There is no null value present in the next part of any of the nodes.  The following image shows a circular singly linked list.
  • 16. Begin Insert struct node *ptr,*temp; int item; ptr = (struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { printf("nOVERFLOW"); } else { printf("nEnter the node data?"); scanf("%d",&item); ptr -> data = item; if(head == NULL) { head = ptr; ptr -> next = head; } else { temp = head; while(temp->next != head) temp = temp->next; ptr->next = head; temp -> next = ptr; head = ptr; } printf("nnode insertedn"); } Last Insert struct node *ptr,*temp; int item; ptr = (struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { printf("nOVERFLOWn"); } else { printf("nEnter Data?"); scanf("%d",&item); ptr->data = item; if(head == NULL) { head = ptr; ptr -> next = head; } else { temp = head; while(temp -> next != head) { temp = temp -> next; } temp -> next = ptr; ptr -> next = head; } printf("nnode insertedn"); }
  • 17. Begin Delete struct node *ptr; if(head == NULL) { printf("nUNDERFLOW"); } else if(head->next == head) { head = NULL; free(head); printf("nnode deletedn"); } else { ptr = head; while(ptr -> next != head) ptr = ptr -> next; ptr->next = head->next; free(head); head = ptr->next; printf("nnode deletedn"); } Last Delete struct node *ptr, *preptr; if(head==NULL) { printf("nUNDERFLOW"); } else if (head ->next == head) { head = NULL; free(head); printf("nnode deletedn"); } else { ptr = head; while(ptr ->next != head) { preptr=ptr; ptr = ptr->next; } preptr->next = ptr -> next; free(ptr); printf("nnode deletedn"); }
  • 18. Search node struct node *ptr; int item,i=0,flag=1; ptr = head; if(ptr == NULL) { printf("nEmpty Listn"); } else { printf("nEnter item which you want to search?n"); scanf("%d",&item); if(head ->data == item) { printf("item found at location %d",i+1); flag=0; } else { while (ptr->next != head) { if(ptr->data == item) { printf("item found at location %d ",i+1); flag=0; break; } else { flag=1; } i++; ptr = ptr -> next; } } if(flag != 0) { printf("Item not foundn"); } }
  • 19. Display linked list struct node *ptr; ptr=head; if(head == NULL) { printf("nnothing to print"); } else { printf("n printing values ... n"); while(ptr -> next != head) { printf("%dn", ptr -> data); ptr = ptr -> next; } printf("%dn", ptr -> data); }
  • 20. Dynamic memory allocation in C The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file. malloc() calloc() realloc() free() Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.
  • 21. static memory allocation dynamic memory allocation memory is allocated at compile time. memory is allocated at run time. memory can't be increased while executing program. memory can be increased while executing program. used in array. used in linked list. malloc() allocates single block of requested memory. calloc() allocates multiple block of requested memory. realloc() reallocates the memory occupied by malloc() or calloc() functions. free() frees the dynamically allocated memory.
  • 22. malloc ptr=(cast-type*)malloc(byte-size) Calloc ptr=(cast-t ype*)calloc(number, byte-size)  The calloc() function allocates multiple block of requested memory.  It initially initialize all bytes to zero.  It returns NULL if memory is not sufficient.