Following program to find the loop in the list and correct the loop
- Algorithm : loop through the list and return any node from the list
- Remove the loop :
Traverse the head and check if the looped node matches
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void CreateLoopedList (struct node **head)
{
struct node *temp = *head;
struct node *merge = *head;
while (temp->next != NULL) {
temp = temp->next;
}
printf ("Created Loop Between Node\r\n == %d -> %d == \r\n", temp->data, (merge->next->next->next)->data);
temp->next = merge->next->next->next;
return;
}
void FindLoopInList (struct node **head, struct node **loopNode)
{
struct node *slow = NULL, *fast = NULL;
slow = fast = *head;
/* alternatively use visit flag
* to find loop node
* */
while (slow && fast) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
/* return any node in the loop
* where slow and fast ptr are met
**/
*loopNode = slow;
break;
}
}
return;
}
void RemoveLoopFromList (struct node **head, struct node *loopNode)
{
struct node *temp1 = *head;
struct node *temp2 = NULL;
while (*head) {
temp2 = loopNode;
/* start from head move temp1 for each looped node not found case
* keep move temp2 , unless looping node found
* temp1 traverse from head
* temp2 loop from looped one side node
* any point of time temp1 == temp2 then break
* **/
while (temp2->next != loopNode && temp2->next != temp1) {
temp2 = temp2->next;
}
if (temp2->next == temp1) {
/* found the loop, make next to null
**/
temp2->next = NULL;
return;
}
temp1 = temp1->next;
}
return;
}
int main(void)
{
int i;
struct node *head = NULL;
struct node *loopNode = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("Head List \r\n");
PrintList(&head);
CreateLoopedList(&head);
FindLoopInList(&head, &loopNode);
RemoveLoopFromList(&head, loopNode);
printf("After removing looped List \r\n");
PrintList(&head);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: mergenode findAndRemoveLoop
mergenode: mergenode.o
${CC} ${CFLAGS} -o $@ mergenode.o
findAndRemoveLoop: findAndRemoveLoop.o
${CC} ${CFLAGS} -o $@ findAndRemoveLoop.o
clean:
-@rm *.o
distclean: clean
-@rm mergenode
-@rm findAndRemoveLoop
- Algorithm : loop through the list and return any node from the list
- Remove the loop :
Traverse the head and check if the looped node matches
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void CreateLoopedList (struct node **head)
{
struct node *temp = *head;
struct node *merge = *head;
while (temp->next != NULL) {
temp = temp->next;
}
printf ("Created Loop Between Node\r\n == %d -> %d == \r\n", temp->data, (merge->next->next->next)->data);
temp->next = merge->next->next->next;
return;
}
void FindLoopInList (struct node **head, struct node **loopNode)
{
struct node *slow = NULL, *fast = NULL;
slow = fast = *head;
/* alternatively use visit flag
* to find loop node
* */
while (slow && fast) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
/* return any node in the loop
* where slow and fast ptr are met
**/
*loopNode = slow;
break;
}
}
return;
}
void RemoveLoopFromList (struct node **head, struct node *loopNode)
{
struct node *temp1 = *head;
struct node *temp2 = NULL;
while (*head) {
temp2 = loopNode;
/* start from head move temp1 for each looped node not found case
* keep move temp2 , unless looping node found
* temp1 traverse from head
* temp2 loop from looped one side node
* any point of time temp1 == temp2 then break
* **/
while (temp2->next != loopNode && temp2->next != temp1) {
temp2 = temp2->next;
}
if (temp2->next == temp1) {
/* found the loop, make next to null
**/
temp2->next = NULL;
return;
}
temp1 = temp1->next;
}
return;
}
int main(void)
{
int i;
struct node *head = NULL;
struct node *loopNode = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("Head List \r\n");
PrintList(&head);
CreateLoopedList(&head);
FindLoopInList(&head, &loopNode);
RemoveLoopFromList(&head, loopNode);
printf("After removing looped List \r\n");
PrintList(&head);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: mergenode findAndRemoveLoop
mergenode: mergenode.o
${CC} ${CFLAGS} -o $@ mergenode.o
findAndRemoveLoop: findAndRemoveLoop.o
${CC} ${CFLAGS} -o $@ findAndRemoveLoop.o
clean:
-@rm *.o
distclean: clean
-@rm mergenode
-@rm findAndRemoveLoop
Output:
Head List
10
9
8
7
6
5
4
3
2
1
Created Loop Between Node
== 1 -> 7 ==
After removing looped List
10
9
8
7
6
5
4
3
2
1
No comments:
Post a Comment