Circular Linked List

In a Circular Linked List the first and the last elements are adjacent. A linked list can be made circular by storing the address of the first node in the next field of the last node.

In a circular linked list, last node is connected back to the first node. In some applications, it is convenient to use circular linked list. A queue data structure can be implemented using a circular linked list, with a single pointer "rear" as the front node can be accessed through the rear node.


Implementation of Circular Linked List

  • Circular Linked List Creation and Traversal

Algorithm for traversing a circular list is slightly different than the same algorithm for a singly linked list because "NULL" is not encountered.

Source Code (Circular Linked List Creation and Traversal)

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

struct Node
{
    int data;
    struct Node *next;
};

void CircularLinkedListTraversal(struct Node *head)
{
    struct Node *p = head;
    do
    {
        printf("Element : %d\n", p->data);
        p = p->next;
    } while (p != head);
}

void main()
{
    struct Node *head;
    struct Node *second;
    struct Node *third;
    struct Node *fourth;

    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));
    fourth = (struct Node *)malloc(sizeof(struct Node));

    head->data = 31;
    head->next = second;

    second->data = 25;
    second->next = third;

    third->data = 02;
    third->next = fourth;

    fourth->data = 69;
    fourth->next = head;

    printf("\nCircular Linked List Traversal : \n");
    CircularLinkedListTraversal(head);
    printf("\n");
}
Output

Circular Linked List Traversal :
Element : 31
Element : 25
Element : 2
Element : 69

  • Circular Linked List Insertion
Source Code (Circular Linked List Insertion)

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

struct Node
{
    int data;
    struct Node *next;
};

void CircularLinkedListTraversal(struct Node *head)
{
    struct Node *p = head;
    do
    {
        printf("Element : %d\n", p->data);
        p = p->next;
    } while (p != head);
}

struct Node *CLL_InsertionAtBeginning(struct Node *head, int data)
{
    struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
    ptr->data = data;

    struct Node *p = head;

    while (p->next != head)
    {
        p = p->next;
    }
    p->next = ptr;
    ptr->next = head;
    head = ptr;
    return (head);
}

struct Node *CLL_InsertionAtEnd(struct Node *head, int data)
{
    struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
    ptr->data = data;

    struct Node *p = head;

    while (p->next != head)
    {
        p = p->next;
    }
    p->next = ptr;
    ptr->next = head;
    return (head);
}

struct Node *CLL_InsertionAtIndex(struct Node *head, int index, int data)
{
    if (index == 1)
    {
        head = CLL_InsertionAtBeginning(head, data);
    }
    else
    {
        struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
        ptr->data = data;
        struct Node *p = head;
        int i = 1;
        while (i < index - 1)
        {
            p = p->next;
            i++;
        }
        ptr->next = p->next;
        p->next = ptr;
    }
    return (head);
}

void main()
{
    struct Node *head;
    struct Node *second;
    struct Node *third;
    struct Node *fourth;

    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));
    fourth = (struct Node *)malloc(sizeof(struct Node));

    head->data = 31;
    head->next = second;

    second->data = 25;
    second->next = third;

    third->data = 02;
    third->next = fourth;

    fourth->data = 69;
    fourth->next = head;

    // Circular Linked List Traversal : Printing all data of Circular Linked List
    printf("\nCircular Linked List Traversal : \n");
    CircularLinkedListTraversal(head);
    printf("\n");

    // Insertion at beginning
    printf("\nCircular Linked List Insertion at the beginning : \n");
    head = CLL_InsertionAtBeginning(head, 100);
    CircularLinkedListTraversal(head);
    printf("\n");

    // Insertion at the end of Linked List
    printf("\nCircular Linked List Insertion at the End : \n");
    head = CLL_InsertionAtEnd(head, 500);
    CircularLinkedListTraversal(head);
    printf("\n");

    // Insertion at the given index
    int index = 4;
    printf("\nCircular Linked List Insertion at the Index : %d\n", index);
    head = CLL_InsertionAtIndex(head, index, 800);
    CircularLinkedListTraversal(head);
    printf("\n");
}

Output

Circular Linked List Traversal :
Element : 31
Element : 25
Element : 2
Element : 69


Circular Linked List Insertion at the beginning :
Element : 100
Element : 31
Element : 25
Element : 2
Element : 69


Circular Linked List Insertion at the End :
Element : 100
Element : 31
Element : 25
Element : 2
Element : 69
Element : 500


Circular Linked List Insertion at the Index : 4
Element : 100
Element : 31
Element : 25
Element : 800
Element : 2
Element : 69
Element : 500

  • Circular Linked List Deletion
Source Code (Circular Linked List Deletion)

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

struct Node
{
    int data;
    struct Node *next;
};

void CircularLinkedListTraversal(struct Node *head)
{
    struct Node *p = head;
    do
    {
        printf("Element : %d\n", p->data);
        p = p->next;
    } while (p != head);
}

struct Node *CLL_DeletionAtBeginning(struct Node *head)
{
    struct Node *p = head;
    struct Node *q = head;

    while (p->next != head)
    {
        p = p->next;
    }
    head = q->next;
    p->next = head;
    free(q);
    return (head);
}

struct Node *CLL_DeletionAtEnd(struct Node *head)
{
    struct Node *p, *q;
    p = head;

    while (p->next != head)
    {
        q = p;
        p = p->next;
    }
    q->next = head;
    free(p);
    return (head);
}

struct Node *CLL_DeletionAtIndex(struct Node *head, int index)
{
    if (index == 1)
    {
        head = CLL_DeletionAtBeginning(head);
    }
    else
    {
        struct Node *p = head->next;
        struct Node *q = head;
        int i = 1;
        while (i < index - 1)
        {
            p = p->next;
            q = q->next;
            i++;
        }
        q->next = p->next;
        free(p);
    }
    return (head);
}

void main()
{
    struct Node *head;
    struct Node *second;
    struct Node *third;
    struct Node *fourth;

    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));
    fourth = (struct Node *)malloc(sizeof(struct Node));

    head->data = 31;
    head->next = second;

    second->data = 25;
    second->next = third;

    third->data = 02;
    third->next = fourth;

    fourth->data = 69;
    fourth->next = head;

    // Circular Linked List Traversal : Printing all data of Circular Linked List
    printf("\nCircular Linked List Traversal : \n");
    CircularLinkedListTraversal(head);
    printf("\n");

    // Deletion at beginning
    printf("\nCircular Linked List Deletion at the beginning : \n");
    head = CLL_DeletionAtBeginning(head);
    CircularLinkedListTraversal(head);
    printf("\n");

    // Deletion at the end of Linked List
    printf("\nCircular Linked List Deletion at the End : \n");
    head = CLL_DeletionAtEnd(head);
    CircularLinkedListTraversal(head);
    printf("\n");

    // Deletion at the given index
    int index = 1;
    printf("\nCircular Linked List Deletion at the Index : %d\n", index);
    head = CLL_DeletionAtIndex(head, index);
    CircularLinkedListTraversal(head);
    printf("\n");
}

Output

Circular Linked List Traversal : 
Element : 31
Element : 25
Element : 2
Element : 69


Circular Linked List Deletion at the beginning :
Element : 25
Element : 2
Element : 69


Circular Linked List Deletion at the End :
Element : 25
Element : 2


Circular Linked List Deletion at the Index : 1
Element : 2

Comments

Popular posts from this blog

Searching Algorithms in Data Structure

Sorting Algorithms in Data Structure