0% found this document useful (0 votes)
10 views6 pages

Indian Institute of Information Technology Sonepat: Dr. Mukesh Mann

The document describes algorithms to insert nodes into a singly linked list at different positions - the front, end, and at a specified index. It includes the pseudocode for the algorithms and a C program implementing them to insert nodes into a linked list at different positions based on user input.
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)
10 views6 pages

Indian Institute of Information Technology Sonepat: Dr. Mukesh Mann

The document describes algorithms to insert nodes into a singly linked list at different positions - the front, end, and at a specified index. It includes the pseudocode for the algorithms and a C program implementing them to insert nodes into a linked list at different positions based on user input.
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/ 6

INDIAN INSTITUTE OF INFORMATION

TECHNOLOGY SONEPAT

Data Structure Lab

Submitted To – Submitted By –
Dr. Mukesh Mann Tanishq Verma
Roll No: 12112001
Branch: IT
AIM:
Write Algorithm and program to implement following –
1. Insert a node in front of already created singly link
list
2. Insert a node at the end of already created singly link
list.
3. Insert a node at user speci ed data in already created
singly link list.
ALGORITHM:
1. Algorithm is as follows:
• First, we create a new pointer ‘ptr’ of the struct
node type.

• Assign a dynamic memory to ptr of size of struct


node.

• Assign data of new node to ptr.

• Link ptr to the head node.


fi
• Now we point head to ptr node. 2. Algorithm is as
follows:

• Create a new pointer ptr of struct node type.

• Assign a dynamic memory to ptr of size of struct


node.

• Assign data of new node to ptr.

• Link last node of list to ptr.

• Link ptr to NULL.


3. Algorithm is as follows:

• Create a new pointer ptr of struct node type.

• Assign a dynamic memory to ptr of size of struct


node.

• Assign data of new node to ptr.


• Create an integer index which stores the index at
which new

node is to be inserted.
• Link ptr to node having index index.

• Link Node having index (index-1) to ptr

➢Code: -

#include <stdio.h>
#include <stdlib.h>
struct node {

int data;

struct node *next;


};
struct node *create_list(int n) // Function to create linked list {
struct node *head;
head = (struct node *)malloc(sizeof(struct node)); printf("Enter data of the head
node : "); scanf("%d", &head->data);
struct node *newnode;
struct node *temp;
temp = head;
for (int i = 0; i < n - 1; i++)
{

newnode = (struct node *)malloc(sizeof(struct node)); printf("Enter data of the


next node : "); scanf("%d", &newnode->data);
temp->next = newnode;

newnode->next = NULL;
temp = newnode;
}
return head;
}
struct node *insertatbegin(struct node *head) // Inserting At The Beginning {

struct node *ptr;


ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter the data of the new node to be inserted at rst : "); scanf("%d",
&ptr->data);
ptr->next = head;
head = ptr;

return head;
}
struct node *insertatlast(struct node *head) {

struct node *p;


p = (struct node *)malloc(sizeof(struct node)); scanf("%d", &p->data);
struct node *temp;
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = p;
p->next = NULL;
return head;
fi
scanf("%d", &p->data);
struct node *temp;
temp = head;
if (index == 0)
{
p->next = temp;
head = p;

return head;
}
for (int i = 0; i < index - 1; i++)
{
temp = temp->next;
}
p->next = temp->next;
temp->next = p;
return head;
}
int main() {

int n;
printf("Enter the size of linked list : "); scanf("%d", &n);
// Creating linked list
struct node *head;
head = create_list(n);
// Calling function to insert new node

int index;
printf("Enter the index at which you want to insert node : "); scanf("%d",
&index);
head = insertatgivenindex(head, index);
// To Print The Linked List
struct node *ptr;
ptr = head;
printf("The value in the linked list are: \n");
while (ptr != NULL)

You might also like