top of page
Writer's picturecompnomics

Searching the Singly Linked List: A C Program Exploration

Imagine searching for a particular passenger on a train (linked list), where each carriage (node) holds passenger information. You'd start at the first carriage (head) and compare passenger details until you find the one you're looking for. This program implements a search function within a C implementation of a singly linked list.


C Program for Declaring and Searching a Singly Linked List:

#include <stdio.h>
#include <stdlib.h>

// Structure to represent a node in the linked list
typedef struct node {
  int data;
  struct node* next;
} Node;

// Function to create a new node
Node* createNode(int data) {
  Node* newNode = (Node*)malloc(sizeof(Node));
  newNode->data = data;
  newNode->next = NULL;
  return newNode;
}

// Function to declare (initialize) an empty linked list
Node* declareList() {
  return NULL; // An empty list is represented by a NULL head pointer
}

// Function to search for a given element in the linked list
Node* searchList(Node* head, int target) {
  if (head == NULL) {
    return NULL; // Element not found in empty list
  }

  Node* current = head;
  while (current != NULL) {
    if (current->data == target) {
      return current; // Element found!
    }
    current = current->next;
  }

  return NULL; // Element not found in the list
}

int main() {
  // Declare an empty list
  Node* head = declareList();

  // Create some nodes
  Node* node1 = createNode(10);
  Node* node2 = createNode(20);
  Node* node3 = createNode(30);

  // Connect the nodes
  node1->next = node2;
  node2->next = node3;

  // Search for element 20
  Node* foundNode = searchList(head, 20);

  if (foundNode != NULL) {
    printf("Element %d found!\n", foundNode->data);
  } else {
    printf("Element not found in the list.\n");
  }

  return 0;
}

Explanation:

  1. Node structure: Defines the basic building block of the list, containing data and a pointer to the next node.

  2. createNode function: Allocates memory for a new node, initializes it with data and a NULL next pointer, and returns the new node.

  3. declareList function: Initializes the head pointer of the list to NULL, signifying an empty list.

  4. searchList function: Takes the head pointer and the target element as input and iterates through the list using a while loop.

  • Checks for an empty list first.

  • If not empty, starts from the head and compares the data of each node with the target element.

  • If a match is found, returns the corresponding node pointer.

  • If no match is found after iterating through the entire list, returns NULL.

  1. main function:

  • Declares an empty list using declareList.

  • Creates three nodes with data 10, 20, and 30 using createNode.

  • Links the nodes together by setting the next pointer of each node to the next node in the sequence.

  • Calls searchList to find element 20.

  • Prints a message based on the search result.

Remember: This is a basic example for searching a singly linked list. You can modify it to search for multiple elements, return the position of the found element, or handle different data types.

I hope this explanation, along with the image, helps you understand the concept of searching in a singly linked list using C!

36 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page