Insert a node at last nth position of linked list in c
C program for insert node in nth last position in linked list. Here Problem description and explanation.
/*
C program for
Linked list insert node at last nth position
*/
#include <stdio.h>
#include <stdlib.h>
// Linked List LinkNode
struct LinkNode
{
int data;
struct LinkNode * next;
};
// Singly linked list
struct SingleLL
{
struct LinkNode * head;
};
// Returns the new linked list
struct SingleLL * newLinkedList()
{
// Create memory of head and tail Nodes
struct SingleLL * sll = (struct SingleLL * )
malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->head = NULL;
}
return sll;
}
// Returns a new linked list node
struct LinkNode * newLinkedNode(int data)
{
struct LinkNode * node = (struct LinkNode * )
malloc(sizeof(struct LinkNode));
if (node == NULL)
{
printf("Memory overflow to create node\n");
}
else
{
// Set node values
node->data = data;
node->next = NULL;
}
return node;
}
// Display linked list element
void display(struct LinkNode * node)
{
if (node == NULL)
{
return;
}
struct LinkNode * temp = node;
// iterating linked list elements
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}
// Handles the request of adding new node at end of linked list
void addNode(struct SingleLL * sll, int data)
{
// Create dynamic node
struct LinkNode * node = newLinkedNode(data);
if (node == NULL)
{
// Handle memory overflow
printf("Memory overflow to Create LinkNode\n");
return;
}
if (sll->head == NULL)
{
// First node in linked list
sll->head = node;
}
else
{
struct LinkNode * temp = sll->head;
// Find the last node
while (temp != NULL && temp->next != NULL)
{
temp = temp->next;
}
// Add node at last position
temp->next = node;
}
}
// Add node at the last nth possition
void add_nth_last(struct SingleLL * sll, int nth, int value)
{
if (sll->head == NULL)
{
printf("\nEmpty Linked List");
}
else if (nth <= 0)
{
printf("\nPlease give of a valid node position");
}
else
{
struct LinkNode * temp = sll->head;
struct LinkNode * location = NULL;
while (temp != NULL)
{
nth--;
if (nth <= 0)
{
if (location == NULL)
{
location = sll->head;
}
else
{
location = location->next;
}
}
temp = temp->next;
}
if (nth > 1)
{
// When not possible to add node
printf("\nInvalid position");
}
else
{
// Create new node
struct LinkNode * node = newLinkedNode(value);
if (node != NULL)
{
if (location == NULL)
{
// insert node at begining
node->next = sll->head;
sll->head = node;
}
else
{
node->next = location->next;
location->next = node;
}
// Display all node
display(sll->head);
}
}
}
}
int main(int argc, char
const * argv[])
{
struct SingleLL * sll = newLinkedList();
// Linked list
// 5 → 4 → 3 → 2 → 1 → NULL
addNode(sll, 5);
addNode(sll, 4);
addNode(sll, 3);
addNode(sll, 2);
addNode(sll, 1);
printf("Before Linked List node : \n");
display(sll->head);
// Assume that position is positive integer
int nth = 2, data = 10;
printf("After insert node at %d last position : \n", nth);
add_nth_last(sll, nth, data);
return 0;
}
Before Linked List node :
5 → 4 → 3 → 2 → 1 → NULL
After insert node at 2 last position :
5 → 4 → 3 → 2 → 10 → 1 → NULL
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment