Write a C program for Circular Linked List.

Create a structure definition for a node with two members: an integer 'num' and a pointer to the next node 'nextptr'
Declare a global variable 'stnode' of type 'struct node*' and initialize it to NULL
Define a function ClListcreation that takes an integer parameter 'n':
    Declare local variables: 'i', 'num'
    If 'n' is greater than or equal to 1, then do the following:
        Allocate memory for 'stnode' using 'malloc' and assign it to 'stnode'
        Prompt the user to input data for the first node and store it in 'num'
        Set the 'num' of 'stnode' to the input value
        Set 'nextptr' of 'stnode' to NULL
        Set 'preptr' to 'stnode'
        Iterate from 'i' = 2 to 'n':
            Allocate memory for a new node using 'malloc' and assign it to 'newnode'
            Prompt the user to input data for the 'i'th node and store it in 'num'
            Set the 'num' of 'newnode' to the input value
            Set 'nextptr' of 'newnode' to NULL
            Set 'nextptr' of 'preptr' to 'newnode'
            Set 'preptr' to 'newnode'
        Set 'nextptr' of 'preptr' to 'stnode'

Define a function displayClList:
    Declare a local variable 'tmp' of type 'struct node*'
    Declare a local variable 'n' and initialize it to 1
    If 'stnode' is NULL, then do the following:
        Print "No data found in the List yet."
    Else, do the following:
        Set 'tmp' to 'stnode'
        Print "Data entered in the list are :"
        Do the following:
            Print "Data 'n' = 'tmp->num'"
            Set 'tmp' to 'tmp->nextptr'
            Increment 'n' by 1
        While 'tmp' is not equal to 'stnode'
        
In the main function:
    Declare a local variable 'n'
    Set 'stnode' to NULL
    Print "Circular Linked List : Create and display a circular linked list :"
    Prompt the user to input the number of nodes and store it in 'n'
    Call the function ClListcreation with 'n' as the argument
    Call the function displayClList
    Return 0

Figure: Linked List Operations

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

struct node {
    int num;
    struct node * nextptr;
}*stnode;
 

void ClListcreation(int n);
void displayClList();

int main()
{
    int n;
    stnode = NULL;
	printf("\n\n Circular Linked List : Create and display a circular linked list :\n");
	printf("\n");	   	

    printf(" Input the number of nodes : ");
    scanf("%d", &n);
 
    ClListcreation(n); 
    displayClList();
    return 0;
}

void ClListcreation(int n)
{
    int i, num;
    struct node *preptr, *newnode;

    if(n >= 1)
    {
        stnode = (struct node *)malloc(sizeof(struct node));

        printf(" Input data for node 1 : ");
        scanf("%d", &num);
        stnode->num = num;
        stnode->nextptr = NULL;
        preptr = stnode;
        for(i=2; i<=n; i++)
        {
            newnode = (struct node *)malloc(sizeof(struct node));
            printf(" Input data for node %d : ", i);
            scanf("%d", &num);
            newnode->num = num;
            newnode->nextptr = NULL;	// next address of new node set as NULL
            preptr->nextptr = newnode;	// previous node is linking with new node
            preptr = newnode;   		// previous node is advanced
        }
        preptr->nextptr = stnode; 		//last node is linking with first node
    }
}

void displayClList()
{
    struct node *tmp;
    int n = 1;

    if(stnode == NULL)
    {
        printf(" No data found in the List yet.");
    }
    else
    {
        tmp = stnode;
        printf("\n\n Data entered in the list are :\n");

        do {
            printf(" Data %d = %d\n", n, tmp->num);

            tmp = tmp->nextptr;
            n++;
        }while(tmp != stnode);
    }
}

 Circular Linked List : Create and display a circular linked list :                                           
                                      
 Input the number of nodes : 3                                                                                
 Input data for node 1 : 2                                                                                    
 Input data for node 2 : 5                                                                                    
 Input data for node 3 : 8                                                                                    
                                                                                                              
 Data entered in the list are :                                                                               
 Data 1 = 2                                                                                                   
 Data 2 = 5                                                                                                   
 Data 3 = 8

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.