You are on page 1of 5

FORMERLY KNOWN AS BENGAL COLLEGE OF ENGIRNEERING AND

TECHNOLOGY, CC324

NAME : DEBAJYOTI SAHOO


UVIVERSITY ROLL NO. : 320401222067
RD
SEMESTER: 3
PAPER NAME : DATA STRUCTURE &
ALGORITHM
PAPER CODE : BCAC 303
YEAR: 2023-2024
TOPIC

Insertion Of Linked List


CONTENT
void insertatbeg(int e)
{
struct node *tmp
tmp=(structnode*)malloc
(sizeof(struct node));
tmp -> data = e;
tmp -> link = start;
start = tmp
}
void insertafter(int e, int pos)
{
struct node *tmp, *q;
int i;
q=start;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
printf("there are less than %d elements",pos);
return;
}
}
tmp=(struct node*)malloc (sizeof(struct node));
tmp->link=q->link;
tmp->data=e;
q->link=tmp;

}
EXPLANATION OF CODE
➢ This refers to a user-defined structure named "node". The structure "node" likely
contains various fields (variables) that store data related to the node. For example:In
this example, the "node" structure has an integer field called "data" and a pointer field
called "next" that can point to the next node in a linked list.
➢ This is the dynamic memory allocation part. ‘sizeof(struct node)’ calculates the size
in bytes needed to store a ‘struct node’. ‘malloc’ then allocates that amount of
memory on the heap and returns a pointer to the beginning of that memory block.
➢expression is used to determine the size in bytes of a struct node type. It calculates the
total memory required to store an instance of the struct node structure, including all
its fields.
➢The line ‘tmp->data = e;’ is used to set the value of the data field in the struct node
that the pointer ‘tmp’ is currently pointing to. Let's break down this line.
The code you've provided appears to be a part of a function that inserts a new node with a
given value e at a specified position pos in a linked list. Let's break down the code step by
step:

struct node *tmp, *q;


int i;
q = start;
// 'start' is assumed to be the pointer to the first node of the linked list
q is used to traverse the linked list to reach the position before the desired insertion point.
start is assumed to be a pointer to the first node of the linked list

for (i = 0; i < pos - 1; i++) {


q = q->link;
if (q == NULL) {
printf("There are less than %d elements", pos);
return;
}

}
➢ This loop iterates through the linked list pos - 1 times, moving q to the
node just before the desired insertion position.
If q reaches the end of the linked list (i.e., q becomes NULL), it means there are fewer elements than the
desired insertion position. In this case, an error message is printed, and the function returns.
tmp = (struct node*)malloc(sizeof(struct node));
Allocates memory for the new node that will be inserted.
tmp->link = q->link;
tmp->data = e;
q->link = tmp;
The new node's link is set to point to the node that was originally after the position of
insertion.

➢ The new node's ‘data’ is set to the value ‘e’.


➢ The link of the node q (node just before the insertion point) is updated to point to the new node, effectively
inserting the new node at the desired position.
➢ This code snippet is a common approach to inserting a new node at a specific position in a linked list.
However, please ensure that you have defined the appropriate struct node structure and initialized the start
pointer properly before using this code. Additionally, consider the edge cases such as inserting at the
beginning, end, or an invalid position in the linked list.

You might also like