0% found this document useful (0 votes)
5 views

DS chapter 3

A linked list is a linear data structure consisting of nodes that contain information and a link to the next node, allowing for dynamic memory allocation and efficient insertion and deletion. There are various types of linked lists, including singly, doubly, circular, and header linked lists, each with distinct characteristics and use cases. The document also compares linked lists to arrays, highlighting differences in size, memory allocation, and access methods.

Uploaded by

keerthinagraj6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DS chapter 3

A linked list is a linear data structure consisting of nodes that contain information and a link to the next node, allowing for dynamic memory allocation and efficient insertion and deletion. There are various types of linked lists, including singly, doubly, circular, and header linked lists, each with distinct characteristics and use cases. The document also compares linked lists to arrays, highlighting differences in size, memory allocation, and access methods.

Uploaded by

keerthinagraj6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DATA STRUCTURES

Unit-3
Linked List

Definition: Linked list is a linear data structure used for storing the data in the form of list. The
multiple nodes are connected to each other through pointer. In the list the node contains two
parts.
Info -> this part contains the actual information. Link -> this part contains the next node
address.
Or
Linked List is a Linear collection of nodes containing the information part and the link part.
Or
Linked list is a linear collection of data structure where data are not store sequentially inside the
computer memory but they are link with each other by the help of address.

Representation of linked list:


A linked list is represented by a pointer to the first node of the linked list. The first node is called
head. If the linked list is empty, then value of head is NULL. Each node in a list consists of at
least two parts: A node is represented using structures. The structure consists of two fields, the
data field or info and the pointer field or address or link.
Info -> this part contains the actual information. Link -> this part contains the next node
address.

Ex: start 10 2000 55 3000 36 NULL


1000 2000 3000

Advantage:
 They are dynamic in nature which allocates the memory when required.
 Insertion and deletion can be easily implemented.
 Stacks and queues can be easily executed.
 It reduces the access time.

Disadvantage:
 The memory is wasted as pointer required extra memory for storage.
 Each mode has to access sequentially.
 Reverse traversing is difficult.

Chithra T R Page 1
DATA STRUCTURES

Application:
 Linked List a used to implement stacks, queues, graph etc.
 Linked List you insert element at the beginning and end of the list.
 In Linked List we don’t need to know the size in advance.
 Performing arithmetic operation as long integer.
 Maintaining Directory of names.
 Dynamic Memory Allocations.

Differences between Array and Linked List

Array Linked list


An array contains a collection of similar Linked list contains a collection of unordered
type data elements. elements called nodes.

Arrays are of fixed size. Linked list size is dynamic (expand or contract
its size)
Memory is allocated during compile time Memory is allocated during runtime.
Random access is allowed. Random access not possible.
Elements are at contiguous location. Elements have non-contiguous location.
Memory utilization is less efficient. Memory utilization is efficient.
Insertion of new element is expensive. Insertion/deletion is easier.

Chithra T R Page 2
DATA STRUCTURES
Types of linked lists:
1. Singly linked list (SLL)
2. Doubly linked list (DLL)
3. Circularly linked list (CLL)
4. Header liked list (HLL)

1. Singly linked list


Singly linked list: A singly linked list 2 fields in each node an information field and the linked
field.
 The information field contain the data of that node.
 The link field contain the memory address of the next node.
 There is an only one link field in each node in the linked list is called a single list
 . In a singly linked list, the item navigation is only in forward direction.

EX: A singly linked list with 4 nodes is as shown.

2. Doubly linked list (DLL)


It is a linked list in which each node contains a link to the next as well as the previous node.
 In double linked list each node contains 3 parts:
1.Forward: It is a pointer field that contain the address of the next node.
2.Backward: It is a pointer field that contain the address of the previous node.
3.Information or Data Field: It is containing the actual data.

 The first node, if the Backward contains the NULL, it indicated that it is the first node in the
list.
 The node is which Forward contains, NULL indicates that the node is the last note

Chithra T R Page 3
DATA STRUCTURES
3. Header liked list (HLL):
A header linked list is a special type of linked list that contains a header node at the beginning of
the list. So, in a header linked list START will not point to the first node of the list but START
will contain the address of the header node. There are 2 types of Headers Linked List.
1. Grounded Header Linked List:
It is a list whose last node contains the NULL pointer. In the header linked list the start pointer
always points to the header node. start -> next = NULL indicates that the grounded header linked
list is empty. The operations that are possible on this type of linked list are Insertion, Deletion,
and Traversing.

2. Circular Header Linked List:


A list in which last node points back to the header node is called circular header linked list. The
chains do not indicate first or last nodes. In this case, external pointers provide a frame of
reference because last node of a circular linked list does not contain the NULL pointer. The
possible operations on this type of linked list are Insertion, Deletion and Traversing.

Chithra T R Page 4
DATA STRUCTURES
4. Circularly linked list (CLL):
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular
linked list, the first node and the last node are connected to each other which forms a circle.
There is no NULL at the end. The link field of the last node contains the memory address of the
first node, such a linked list is called circular linked list.

There are generally two types of circular linked lists:


1. Circular singly 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 traverse the circular singly linked list until we reach the
same node where we started. The circular singly linked list has no beginning or end. No null
value is present in the next part of any of the nodes.

2. Circular Doubly linked list:


A circular doubly linked list is defined as a circular linked list in which each node has two links
connecting it to the previous node and the next node.
or
Circular Doubly Linked List has properties of both doubly linked list and circular linked list in
which two consecutive elements are linked or connected by the previous and next pointer and the
last node points to the first node by the next pointer and also the first node points to the last node
by the previous pointer.

Representation of Linked list in Memory:


A linked list is a linear date collection of a data elements, whose order is not given by their
physical placement in a memory.
 Linked list can be represent in memory by using two arrays respectively known as Info
and Link, such that Info[k] and Link[k] a contains information of element and next node
address respectively.
 The list also required variable ‘Name’ or ‘Start’, which contains address of the first
node. Pointer field of a last node denoted by NULL Which indicates the end of the list.
 There are two ways to represent a linked list in memory:
1. Static representation using array
2. Dynamic representation using free pool of storage
 The Linked List can be represented in memory as-
 Below figure shows linked list. It indicates that the node of a list need not occupy
adjacent elements in the array Info and Link.

Chithra T R Page 5
DATA STRUCTURES

Operations on Singly or linear linked lists: Basic operations that can be performed on linked
list are
1. Creating a linked list:
The node of a linked list can be created by the following structure declaration.
Struct node
{
int data;
Struct node*next;
} *new, *head, *temp;
Where as
Node: name of the structure.
Data: Data or info it contains values for the data type like integer, char, float, etc...
Next: It used to store the address of the next node.
 Next also contain address or pointer variable of the next node but the node is belonged to
the same structure is called self-referential structure or self-addressing pointer (EX :.
 In this we use 3 pointer variables.
1. *New: It used for allocation of memory for node.
2. *Head: Head is always pointed to beginning of the node or 1st node.
3. *Temp: It is used for traversing all the node or traversing one node to another node.
2. Traversing a linked list:
Traversing is the process of visiting each node of the linked list exactly once to perform some
operation.
Algorithm: Travers (Head, Temp) Head contains the address of the first node. Another pointer
Temp is temporarily used to visit all the nodes from the beginning to the end of the linked list.
Step1: temp=head [Assign]
Step2: While (temp! =NULL) [ Being loop]
Step3: temp->data [ print data]
Step4: temp=temp->next [Move or Linking the next node] Step5: end of while Step6: stop.

Chithra T R Page 6
DATA STRUCTURES
Program to implement Creating and Traversing (or Display) linear or singly linked list.
#include <stdio.h>
#include<stdlib.h> Output:
void main() { Enter a data value for the node:8
struct node Do u want to add one more node to the list
{ [n/y]: y
int data; Enter a data value for the node:4
struct node *next; Do u want to add one more node to the list
}*new,*head,*temp; [n/y]: y
int value; Enter a data value for the node:7
char choice; Do u want to add one more node to the list
do{ [n/y]: n
new=(struct node*) malloc (sizeof(struct node)); 8-> 4-> 7->
printf("Enter a data value for the node:");
scanf("%d",&value);
new->data=value; Creating SLL
new->next=NULL;
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("\n Do u want to add one more node to the list [n/y]:\n");
fflush(stdin);
scanf("\n%c",&choice);
}while(choice=='y');
temp=head;
while(temp!=NULL)
{
printf("%d->\t",temp->data); Traversing Singly Linked List(SLL)
temp=temp->next;
}
getch();
}
3. Inserting an item into a linked list: Inserting an element into the existing Linked list.
Inserting element is divided in to 3 types.
1. Inserting a node at the beginning of the linked list: Steps to Insert a node at the Beginning
of the Linked List are,
1. Create a new node.
2. Initialize the data of this new node.
3. Update the next variable of the new node as head.
4. Update the head as the new node.

Chithra T R Page 7
DATA STRUCTURES

2. Inserting a node at the given position: Steps to Insert a node at the End of the Linked
List are,
1. Create a new node and initialize the data of this new node.
2. Create another node, temp with the value head.
3. Iterate over the Linked List while temp's next variable is not equal to NULL.
4. Now temp is at the temp of next node of the Linked List and we have to just update
the next variable of temp to the new node.
5. Also make sure to update the next variable of the new node as NULL.

3. Inserting a node at the end of the linked list: Steps to Insert a node at the End of the Linked
List are,
1. Create a new node and initialize the data of this new node.
2. Create another node, temp with the value head.
3. Iterate over the Linked List while temp's next variable is not equal to NULL.
4. Now temp is at the tail of the Linked List and we have to just update the next variable of
temp to the new node.
5. Also make sure to update the next variable of the new node as NULL.

Chithra T R Page 8
DATA STRUCTURES
..........MENU......
1. Create
2. Insert at Beginning
Program to implement Inserting element into 3.Insert at Ending
singly linked list at Beginning, Ending and given 4.Insert at specific position
Specific Position. 5.Display
# include <stdio.h> 6.Exit
# include <stdlib.h> Enter your choice:1
void create(); Enter a data value for the node:1
void display(); Do u want to add one more node to the list [n/y]: y
Enter a data value for the node:2
void IAB(); void IAE(); void IAP();
Do u want to add one more node to the list [n/y]: n
struct node ..........MENU......
{ 1. Create
int data; 2. Insert at Beginning
struct node *next; 3.Insert at Ending
}*new,*head,*temp; 4.Insert at specific position
void main() { 5.Display
int choice; clrscr(); 6.Exit
while(1) Enter your choice:2
{ Enter a value:0
printf("..........MENU. .... \n"); 0->1->2->NULL
Total number of nodes = 3
printf("1.Create \n");
..........MENU......
printf("2.Insert at Beginning \n"); 1. Create
printf("3.Insert at Ending \n"); 2. Insert at Beginning
printf("4.Insert at specific position\n"); 3.Insert at Ending
printf("5.Display\n"); 4.Insert at specific position
printf("6.Exit\n"); 5.Display
printf("Enter your choice:"); scanf("%d", &choice); 6.Exit
switch(choice) Enter your choice:3
{ Enter a value:4
case 1 : create();break; 0->1->2->4->NULL
case 2 : IAB();break; Total number of nodes = 4
..........MENU......
case 3 : IAE();break;
1. Create
case 4 : IAP();break; 2. Insert at Beginning
case 5 : display();break; 3.Insert at Ending
case 6 : exit(0); 4.Insert at specific position
default : printf("wrong choice\n"); break; 5.Display
} 6.Exit
} getch(); Enter your choice:4
} Enter the position at which element is inserted:4
void create() Enter an element:3
{ 0->1-> 2->3->4->NULL
int value; Total number of nodes = 5
..........MENU......
char choice;
1. Create
do{ 2. Insert at Beginning
new=(struct node*) malloc (sizeof (struct node)); 3.Insert at Ending
printf("Enter a data value for the node:"); 4.Insert at specific position
scanf("%d", &value); 5.Display
new->data=value; 6.Exit
Enter your choice:5
0->1->2->3->4->NULL
Total number of nodes = 5 8
DATA STRUCTURES
new->next=NULL;
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("Do u want to add one more node to the list [n/y]:");
fflush (stdin); temp->next=new;
scanf("\n %c", &choice); printf("Enter a value:");
}while(choice=='y'); scanf("%d", &value);
} new->data=value;
void display() new->next=NULL;
{ temp=new;
temp=head; display();
int count=0,value; }
while(temp!=NULL) void IAP()
{ {
printf("%d->", temp->data); int pos, i, value;
temp=temp->next; temp=head;
count=count+1; new=(struct node*)malloc(sizeof (struct node));
} printf("Enter the position at which element is
printf("NULL\n"); inserted:");
printf("Total number of nodes = %d \n", count); scanf("%d", &pos);
} printf("Enter an element:");
void IAB() scanf("%d", &value);
{ for(i=1;i<pos-1;i++)
int value; temp=temp->next;
new=(struct node*)malloc(sizeof (struct node)); new->data=value;
printf("Enter a value:"); new->next=temp->next;
scanf("%d", &value); temp->next=new;
new->data=value; display();
new->next=head; }
head=new;
display();
}
void IAE()
{
int value;
temp=head;
new=(struct node*)malloc(sizeof (struct node));
while(temp->next!=NULL)
temp=temp->next;

9
DATA STRUCTURES
4. Deleting an item from the linked list: Deleting an existing node from the Linked list. It
involves following steps.
1. If the linked list is empty then deletion is not possible & this condition is called underflow.
2. To delete a node, loop through the nodes until the node to be deleted is found.
3. Adjust the link address of the previous node.
4. Free the memory space utilized by the deleted node.
Deleting element is divided in to 3 types.
1. Deleting a node at the beginning of the linked list: First check whether the list is empty or not
(underflow), move the start pointer to the 2nd node and remove the previous node.

2. Deleting a node at the given position: To delete the last node of a linked list, traverse from the
beginning to the end of the list and locate the last node and its previous.
1. Check the list is empty or not.
2. Traverse the pointer variable p1or D to the end of the list.
3. Locate the last node and its previous.

3. Deleting a node at the end of the linked list: This involves following steps.
1) Check the list is empty or not.
2) If the list is not empty then count the number of nodes.
3) Check the position is valid or not.
4) If the position is valid then check two more conditions
a) If pos=1, call DAB function.
b) If pos = count call DAE function.
5) To find the position, locate p1 or D to position and previous node by temp.
6) Disconnect the detected node and remove.

10
DATA STRUCTURES
Program to implement Deleting element into SLL from Beginning, Ending and Specific
position. Output:
# include <stdio.h> ..........MENU......
# include <stdlib.h> 1. Create
void create(); 2. Delete from Beginning
3.Delete from Ending
void display(); 4.Delete from specific position
void DFB(); void DFE(); void DFP(); 5.Display
struct node 6.Exit
{ Enter your choice:1
int data; Enter a data value for the node:1
struct node *next; Do u want to add one more node to the list [n/y]:y
Enter a data value for the node:2
}*new,*head,*temp,*d; Do u want to add one more node to the list [n/y]:y
void main() { Enter a data value for the node:3
int choice; clrscr(); Do u want to add one more node to the list [n/y]:y
while(1) Enter a data value for the node:4
{ Do u want to add one more node to the list [n/y]:n
..........MENU......
printf("..........MENU. .... \n"); 1. Create
printf("1.Create \n"); 2. Delete from Beginning
printf("2.Delete from Beginning \n"); 3.Delete from Ending
printf("3.Delete from Ending \n"); 4.Delete from specific position
printf("4.Delete from specific position\n"); 5.Display
printf("5.Display\n"); 6.Exit
Enter your choice:5
printf("6.Exit\n"); 1->2->3->4->NULL
printf("Enter your choice:"); scanf("%d", &choice); Total number of nodes =4
switch(choice) ..........MENU......
{ 1. Create
case 1 : create();break; 2. Delete from Beginning
case 2 : DFB();break; 3.Delete from Ending
4.Delete from specific position
case 3 : DFE();break; 5.Display
case 4 : DFP();break; 6.Exit
case 5 : display();break; Enter your choice:2
case 6 : exit(0); Deleted node is 1:2->3->4->NULL
default : printf("wrong choice\n"); break; Total number of nodes =3
..........MENU......
} 1. Create
}getch(); 2. Delete from Beginning
} 3.Delete from Ending
void create() 4.Delete from specific position
{ 5.Display 6.Exit
int value; Enter your choice:3
Deleted node is 3:2->3->NULL
char choice; Total number of nodes =2
do{ ..........MENU......
new=(struct node*) malloc (sizeof(struct node)); 1. Create
printf("Enter a data value for the node:"); 2. Delete from Beginning
scanf("%d", &value); 3.Delete from Ending
new->data=value; 4.Delete from specific position
5.Display 6.Exit
new->next=NULL; Enter your choice:4
Enter the position at which element is to be
deleted:2
deleted node is 3:2->NULL 11
Total number of nodes =1
DATA STRUCTURES
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("Do u want to add one more node to the list [n/y]:");
fflush (stdin);
scanf("\n %c", &choice);
}while(choice=='y');
}
}
void display()
void DFP()
{
{
temp=head;
int pos, i, count;
int count=0,value;
printf("Enter the position at which element is to
while(temp!=NULL)
be deleted:");
{
scanf("%d", &pos);
printf("%d->",temp->data);
if (pos==1)
temp=temp->next;
DFB();
count=count+1;
else if(pos==count)
}
DFE();
printf("NULL\n");
else
printf("Total number of nodes =%d \n", count);
temp=head;
}
for(i=1;i<pos-1;i++)
void DFB()
temp=temp->next;
{
d=temp->next;
temp=head;
printf("deleted node is %d:", d->data);
printf("Deleted node is %d:", temp->data);
temp->next=d->next;
head=head->next;
d->next=NULL;
temp->next=NULL;
free(d);
free(temp);
display();
display();
}
}
void DFE()
{
temp=head;
while(temp->next->next!=NULL)
temp=temp->next;
d=temp->next;
printf("Deleted node is %d:", temp->data);
temp->next=NULL;
free(d);
display();

12
DATA STRUCTURES
6.Searching an item in the linked list: Searching is performed in order to find the location of a
particular element in the list. The most common search used on linked list structures is sequential
search. In this method the given item is searched by traversing through the list and makes the
comparison of every element of the list until it is found. If the element is matched with any of the
list element, then the location of the element is returned.
Algorithm:
Step-1: Initialize the current pointer with the beginning of the List.
Step-2: Compare the KEY value with the Current node value; if they match then exit else go
to step-3.
Step-3: Move the current pointer to point to the next node in the list and go to step-2, till the
list is not over or else quit.

Program to implement Searching element in the Singly or Linear Linked List.


# include <stdio.h> Output:
# include <stdlib.h> ..........MENU......
void create(); 1.Create
void display(); 2.Display
3.Search
void search();
4.Exit
struct node Enter your choice:1
{ Enter a data value for the node:1
int data; Do u want to add one more node to the list [n/y]:y
struct node *next; Enter a data value for the node:2
}*new,*head,*temp,*d; Do u want to add one more node to the list [n/y]:y
Enter a data value for the node:3
void main() { Do u want to add one more node to the list [n/y]:y
int choice; clrscr(); Enter a data value for the node:4
while(1) Do u want to add one more node to the list [n/y]:n
{ ..........MENU......
printf("..........MENU. .... \n"); 1.Create
printf("1.Create \n"); 2.Display
3.Search
printf("2.Display \n"); 4.Exit
printf("3.Search\n"); Enter your choice:2
printf("4.Exit\n"); 1->2->3->4->NULL
printf("Enter your choice:"); Total number of nodes =4
scanf("%d", &choice); ..........MENU......
1.Create
switch(choice)
2.Display
{ 3.Search
case 1 : create();break; 4.Exit
Enter your choice:3
Enter key element:5 13
key element not found
DATA STRUCTURES
case 2 : display();break;
case 3 : search();break;
case 4 : exit(0);
default : printf("wrong choice\n"); break;
}
}getch();
}
void create()
{
int value;
char choice;
do{
new=(struct node*) malloc (sizeof(struct node));
printf("Enter a data value for the node:");
scanf("%d", &value);
new->data=value;
new->next=NULL;
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("Do u want to add one more node to the list [n/y]:");
fflush (stdin);
scanf("\n %c", &choice); int flag=0,key;
}while(choice=='y'); printf("Enter key element:");
} scanf("%d",&key);
void display() temp=head;
{ while(temp!=NULL)
temp=head; {
int count=0,value; if(key==temp->data)
while(temp!=NULL) {
{ flag=1;
printf("%d->",temp->data); break;
temp=temp->next; }
count=count+1; temp=temp->next;
} }
printf("NULL\n"); if(flag==1)
printf("Total number of nodes =%d \n", count); printf("key element found\n");
} else
void search() printf("key element not found\n");
{ display();
}
Chithra T R Page 14
DATA STRUCTURES
{

Program to implement linear linked list.


# include <stdio.h>
# include <stdlib.h>
void create();
void display();
void IAB(); void DFB();
struct node
{
int data;
struct node *next;
}*new,*head,*temp,*d;
void main() {
int choice; clrscr();
while(1)
{
printf("..........MENU. .... \n");
printf("1.Create \n");
printf("2.Display \n");
printf("3.Insert at Beginning \n");
printf("4.Delete from Beginning \n");
printf("5.Exit\n");
printf("Enter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1 : create();break;
case 2 : display();break;
case 3 : IAB();break;
case 4 : DFB();break;
case 5 : exit(0);
default : printf("wrong choice\n"); break;
}
}getch();
}
void create()
{
int value;
char choice;
do{
new=(struct node*) malloc (sizeof(struct node));
printf("Enter a data value for the node:");
scanf("%d", &value);
new->data=value;
new->next=NULL;
if(head==NULL)

Chithra T R Page 15
DATA STRUCTURES
Output:
..........MENU......
1.Create
2.Display
3.Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:1
Enter a data value for the node:1
Do u want to add one more node to the list
[n/y]:y
Enter a data value for the node:2
Do u want to add one more node to the list
[n/y]:y
Enter a data value for the node:3
Do u want to add one more node to the list
[n/y]:n
..........MENU......
1.Create
2.Display
3.Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:21-
>2->3->NULL
Total number of nodes =3
..........MENU......
1.Create
2.Display
3.Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:3
Enter a value:0
0->1->2->3->NULL
Total number of nodes =4
..........MENU......
1.Create
2.Display
3. Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:4
Deleted node is 0:1->2->3->NULL
Total number of nodes =3

Chithra T R Page 16
DATA STRUCTURES
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("Do u want to add one more node to the list [n/y]:");
fflush (stdin);
scanf("\n %c", &choice);
}while(choice=='y');
}
void display()
{
temp=head;
int count=0,value;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
count=count+1;
}
printf("NULL\n");
printf("Total number of nodes =%d \n", count);
}
void IAB()
{
int value;
new=(struct node*)malloc(sizeof (struct node));
printf("Enter a value:");
scanf("%d", &value);
new->data=value;
new->next=head;
head=new;
display();
}
void DFB()
{
temp=head;
printf("Deleted node is %d:", temp->data);
head=head->next;
temp->next=NULL;
free(temp);
display();
}

Chithra T R Page 17
DATA STRUCTURES
Memory allocation:
The library function malloc is used to allocate a block of memory on the heap. The program
accesses this block of memory via a pointer that malloc returns. When the memory is no longer
needed, the pointer is passed to free which deallocates the memory so that it can be used for
other purposes.
There are two types of memory allocations: Compile-time or Static Memory Allocation. Run-
time or Dynamic Memory Allocation. And briefly explain in unit 1 notes
Garbage collection:

Collect all node which are allocated but no longer in use is known as Garbage collection.
Or
Garbage collection is an automated dynamic memory management that identifies dead memory
blocks and reallocates storage for reuse.

The garbage collection takes place in two steps:


1. Garbage collection will sequentially visit all nodes in memory and mark all nodes which
are being used in programs.
2. It will collect all unmarked nodes and place them in free storage are.
Note: Extra Information for Garbage collection:
The operating system of a computer periodically collects all the deleted space on the free storage
list. Any technique which does this collection is called garbage collection.
The garbage collection takes place in two steps.
First the computer runs through all the lists tagging those cells which are currently in use. Then
the computer runs through the memory collecting all untagged space on the program. Generally,
the garbage collection is invisible to the programmer. The free () function in c returns a node to
the free pool i.e., the availability list.
Garbage collection is an ongoing process that requires central processing unit resources, which
can affect an application's general performance or even disrupt its operations. For this reason,
some developers still debate GC's benefits, believing that they can better control memory
deallocation than an automated process.

Chithra T R Page 18

You might also like