Remove every k th node of the linked list in c

C program for Remove every k th node of the linked list. Here problem description and explanation.

/*
    C program for
    Delete every k-th node of the linked list
*/
#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;
}
// 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 = (struct LinkNode *)
	malloc(sizeof(struct LinkNode));
	if (node == NULL)
	{
		printf("Memory overflow to Create LinkNode\n");
		return;
	}
	else
	{
		// Set initial node value
		node->data = data;
		node->next = NULL;
	}
	if (sll->head == NULL)
	{
		sll->head = node;
	}
	else
	{
		struct LinkNode *temp = sll->head;
		// Find last node
		while (temp != NULL && temp->next != NULL)
		{
			temp = temp->next;
		}
		// Add node at last position
		temp->next = 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");
}
void removeKh(struct SingleLL *sll, int kth)
{
	if (sll->head == NULL)
	{
		// When no element
		printf("Empty Linked list");
	}
	else if (kth <= 0)
	{
		printf("\nInvalid position %d", kth);
	}
	else
	{
		struct LinkNode *temp = sll->head;
		struct LinkNode *hold = NULL;
		struct LinkNode *checker = NULL;
		int count = 0;
		if (kth == 1)
		{
			// When delete all element
			sll->head = NULL;
		}
		while (temp != NULL)
		{
			count++;
			if (count % kth == 0)
			{
				// When delete node found
				hold = temp;
			}
			else
			{
				// Get current node
				checker = temp;
			}
			temp = temp->next;
			// When get free node
			if (hold != NULL)
			{
				if (checker != NULL)
				{
					// Unlink node
					checker->next = hold->next;
				}
				free(hold);
				hold = NULL;
			}
		}
	}
}
int main(int argc, char
	const *argv[])
{
	struct SingleLL *sll = newLinkedList();
	// Linked list
	// 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	addNode(sll, 1);
	addNode(sll, 2);
	addNode(sll, 3);
	addNode(sll, 4);
	addNode(sll, 5);
	addNode(sll, 6);
	addNode(sll, 7);
	int position = 2;
	printf(" Position : %d", position);
	printf("\n Before Delete Node  \n");
	display(sll->head);
	removeKh(sll, position);
	printf(" After Delete Node \n");
	display(sll->head);
	return 0;
}

Output

 Position : 2
 Before Delete Node
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After Delete Node
 1 → 3 → 5 → 7 → 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







© 2021, kalkicode.com, All rights reserved