Chap IV Linked List Part -1
Chap IV Linked List Part -1
A B C
Circular linked list
The pointer from the last element in the list points
A B C
Doubly linked list
Pointers exist between adjacent nodes in both
directions.
The list can be traversed either forward or
backward.
head tail
Usually two pointers are maintained to keep track
}
//Structure Pointer example
#include <stdio.h>
struct person
{
int age;
float weight;
};
void main()
{
struct person p;
struct person *ptr;//structure pointer declaration
ptr=&p; // structure pointer initialization
printf(“Enter age and weight of a person1”);
scanf(“%d%f”,&ptr->age,&ptr->weight);
printf(“%d%f”,ptr->age,ptr->weight);
}
Creating a Linked List
Now we need to allocate the memory for
node dynamically as :
For head node:
HEAD=(node*)malloc(sizeof(node));
getch();
TRAVERSING A LIST
if(P->data>x)
return(0);
P=P->next;
}
Return(0);
}
Memory Allocation and
De-allocation for a Linked
List and Garbage
Collection
Memory Allocation and De-allocation for
a Linked List
We have seen how a linked list is represented in the memory. If
we want to add a node to an already existing linked list in the
memory, we first find free space in the memory and then use it to
store the information.
For example, consider the linked list shown in Fig. The linked list
contains the roll number of students, marks obtained by them in
Biology, and finally a NEXT field which stores the address of the
next node in sequence.
Now, if a new student joins the class and is asked to appear for
the same test that the other students had taken, then the new
student’s marks should also be recorded in the linked list.
For this purpose, we find a free space and store the information
there. In Fig. 6.5 the grey shaded portion shows free space, and
thus we have 4 memory locations available. We can use any one
of them to store our data.
Memory Allocation and De-allocation for
a Linked List
Now, the question is which part of the memory
is available and which part is occupied? When
we delete a node from a linked list, then who
changes the status of the memory occupied by
it from occupied to available?
The answer is the operating system.
However, let us briefly discuss the basic
concept behind it. The computer maintains a
list of all free memory cells. This list of
available space is called the free pool.
Memory Allocation and De-allocation for
a Linked List
We have seen that every linked list has a pointer
variable START which stores the address of the
first node of the list. Likewise, for the free pool
(which is a linked list of all free memory cells), we
have a pointer variable AVAIL which stores the
address of the first free space.
Now, when a new student’s record has to be
added, the memory address pointed by AVAIL will
be taken and used to store the desired
information. After the insertion, the next available
free space’s address will be stored in AVAIL.
For example, in Fig. 6.6, when the first free memory space is
utilized for inserting the new node, AVAIL will be set to contain address 6.
Memory Allocation and De-allocation for
a Linked List
This was all about inserting a new node in an already
existing linked list. Now, we will discuss deleting a
node or the entire linked list. When we delete a
particular node from an existing linked list or delete
the entire linked list, the space occupied by it must be
given back to the free pool so that the memory can be
reused by some other program that needs memory
space.
The operating system does this task of adding the
freed memory to the free pool. The operating system
will perform this operation whenever it finds the CPU
idle or whenever the programs are falling short of
memory space.
Memory Allocation and De-allocation for
a Linked List
The operating system scans through all the
memory cells and marks those cells that are
being used by some program.
Then it collects all the cells which are not
being used and adds their address to the free
pool, so that these cells can be reused by
other programs.
This process is called garbage collection.
INSERTING INTO LINKED LIST
How to add elements to linked list
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; // Link data part
newNode->next = head; // Link address part
tmp X Item to be
inserted
A B C
curr
X
Pseudo-code for insertion
typedef struct nd {
struct item data;
struct nd * next;
} node;
tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
Add to middle
Allocate memory and store data for new node
Traverse to node just before the required
position of new node
Change next pointers to include new node in
between
struct node *newNode; newNode =
malloc(sizeof(struct node));
newNode->data = 4;
struct node *temp = head;
for(int i=2; i < position; i++)
{
if(temp->next != NULL)
{
temp = temp->next;
}
}
newNode->next = temp->next;
temp->next
How to delete from a linked list
You can delete either from beginning, end or
from a particular position.
Illustration: Deletion
Item to be deleted
A B C
tmp
curr
A B C
Pseudo-code for deletion
typedef struct nd {
struct item data;
struct nd * next;
} node;
head = head->next;
Delete from end