https://learnprogramming.in.net/detect-loop-in-linked-list-bash-program/

Loop Detection in Linked List using Bash


Detecting a Loop in a Linked List using Bash

In this guide, we will create a Bash script to detect if a singly linked list contains a loop. A loop in a linked list occurs when a node’s next pointer points back to a previous node, creating a cycle.

Program Explanation

We will use Floyd’s Cycle-Finding Algorithm (also known as the Tortoise and Hare algorithm) to detect the loop. The idea behind this algorithm is that if there is a loop in the linked list, the faster pointer (hare) will eventually meet the slower pointer (tortoise).

Steps:

  1. We will define two pointers, slow and fast.
  2. Initially, both pointers will start at the head of the linked list.
  3. The slow pointer will move one step at a time, while the fast pointer will move two steps at a time.
  4. If there is a loop, the fast pointer will eventually meet the slow pointer within the loop.
  5. If the fast pointer reaches the end of the linked list (NULL), then there is no loop.

Bash Script

Here is the Bash script that implements this logic:


#!/bin/bash

# Node structure
declare -A node1=( ["data"]=1 ["next"]="node2" )
declare -A node2=( ["data"]=2 ["next"]="node3" )
declare -A node3=( ["data"]=3 ["next"]="node4" )
declare -A node4=( ["data"]=4 ["next"]="node5" )
declare -A node5=( ["data"]=5 ["next"]="node3" ) # Creates a loop

# Detect Loop function using Floyd's Cycle-Finding Algorithm
detect_loop() {
local slow=$1
local fast=$1

while true; do
# Move slow pointer by one step
slow=${!slow[next]}
# Move fast pointer by two steps
fast=${!fast[next]}
[ -n "${!fast[next]}" ] && fast=${!fast[next]} || break

# If slow and fast meet, there is a loop
if [ "$slow" == "$fast" ]; then
echo "Loop detected in the linked list."
return
fi

# If fast reaches the end, no loop exists
[ -z "$fast" ] && break
done

echo "No loop detected in the linked list."
}

# Start the detection
detect_loop "node1"

Documentation

This Bash script is structured to simulate a linked list using associative arrays, where each array represents a node.

Node Representation

Each node is defined as an associative array with two keys:

  • data: Holds the value of the node.
  • next: Holds the reference to the next node in the list.

detect_loop Function

The detect_loop function is responsible for checking if there is a loop in the linked list:

  • The function accepts the name of the first node as an argument.
  • It initializes two pointers, slow and fast, both starting at the head of the linked list.
  • The while loop runs until the end of the list is reached or a loop is detected.
  • In each iteration, the slow pointer advances by one node, and the fast pointer advances by two nodes.
  • If the slow pointer and fast pointer meet, a loop is detected.
  • If the fast pointer reaches the end of the list, it breaks out of the loop, indicating no loop is present.

Conclusion

Floyd’s Cycle-Finding Algorithm is an efficient way to detect a loop in a linked list with a time complexity of O(n) and no additional space requirement. This Bash script provides a simple implementation that can be easily adapted for different use cases.


By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)