C Program for Circular Linked List

// Program of circular linked list
#include <stdio.h>
#include <malloc.h>

struct node
{
	int info;
	struct node *link;
}*last;

main()
{
	int choice,n,m,po,i;
	last=NULL;
	while(1)
	{
		printf("1.Create List\n");
		printf("2.Add at begining\n");
		printf("3.Add after \n");
		printf("4.Delete\n");
		printf("5.Display\n");
		printf("6.Quit\n");
		printf("Enter your choice : ");
		scanf("%d",&choice);

		switch(choice)
		{
		 case 1:
			printf("How many nodes you want : ");
			scanf("%d",&n);
			for(i=0; i < n;i++)
			{
				printf("Enter the element : ");
				scanf("%d",&m);
				create_list(m);
			}
			break;
		 case 2:
			printf("Enter the element : ");
			scanf("%d",&m);
			addatbeg(m);
			break;
		 case 3:
			printf("Enter the element : ");
			scanf("%d",&m);
			printf("Enter the position after which this element is inserted : ");
			scanf("%d",&po);
			addafter(m,po);
			break;
		 case 4:
			if(last == NULL)
			{
				printf("List underflow\n");
				continue;
			}
			printf("Enter the number for deletion : ");
			scanf("%d",&m);
			del(m);
			break;
		 case 5:
			display();
			break;
		 case 6:
			exit();
		 default:
			printf("Wrong choice\n");
		}/*End of switch*/
	}/*End of while*/
}/*End of main()*/

create_list(int num)
{
	struct node *q,*tmp;
	tmp= malloc(sizeof(struct node));
	tmp->info = num;

	if(last == NULL)
	{
		last = tmp;
		tmp->link = last;
	}
	else
	{
		tmp->link = last->link; /*added at the end of list*/
		last->link = tmp;
		last = tmp;
	}
}/*End of create_list()*/

addatbeg(int num)
{
	struct node *tmp;
	tmp = malloc(sizeof(struct node));
	tmp->info = num;
	tmp->link = last->link;
	last->link = tmp;
}/*End of addatbeg()*/

addafter(int num,int pos)
{

	struct node *tmp,*q;
	int i;
	q = last->link;
	for(i=0; i < pos-1; i++)
	{
		q = q->link;
		if(q == last->link)
		{
			printf("There are less than %d elements\n",pos);
			return;
		}
	}/*End of for*/
	tmp = malloc(sizeof(struct node) );
	tmp->link = q->link;
	tmp->info = num;
	q->link = tmp;
	if(q==last)    /*Element inserted at the end*/
		last=tmp;
}/*End of addafter()*/

del(int num)
{
	struct node *tmp,*q;
	if( last->link == last && last->info == num)  /*Only one element*/
	{
		tmp = last;
		last = NULL;
		free(tmp);
		return;
	}
	q = last->link;
	if(q->info == num)
	{
		tmp = q;
		last->link = q->link;
		free(tmp);
		return;
	}
	while(q->link != last)
	{
		if(q->link->info == num)     /*Element deleted in between*/
		{
			tmp = q->link;
			q->link = tmp->link;
			free(tmp);
			printf("%d deleted\n",num);
			return;
		}
		q = q->link;
	}/*End of while*/
	if(q->link->info == num)    /*Last element deleted q->link=last*/
	{
		tmp = q->link;
		q->link = last->link;
		free(tmp);
		last = q;
		return;
	}
	printf("Element %d not found\n",num);
}/*End of del()*/

display()
{
	struct node *q;
	if(last == NULL)
	{
		printf("List is empty\n");
		return;
	}
	q = last->link;
	printf("List is :\n");
	while(q != last)
	{
		printf("%d ", q->info);
		q = q->link;
	}
	printf("%d\n",last->info);
}/*End of display()*/
VN:F [1.9.22_1171]
Rating: 7.6/10 (178 votes cast)
VN:F [1.9.22_1171]
Rating: +49 (from 97 votes)
C Program for Circular Linked List, 7.6 out of 10 based on 178 ratings

19. June 2010 by Jishnu
Categories: C Programming, Data Structures | Tags: | 10 comments

Comments (10)

  1. Great work for students
    Thank you

  2. The code is really a good one!! But,there is a mistake in this…

    tmp= malloc(sizeof(struct node));

    Here when malloc() fails to allocate memory still the program runs and it crashes!! So you need to check each time when you allocate memory so that your program will not be abnormally terminated and you will come to know when and where malloc() failed to create memory!!

    So here is the simple way to check….

    tmp= malloc(sizeof(struct node));
    if( tmp = = NULL )
    {
    printf(“\n malloc() failed to allocate memory!!\n”);
    exit(-1);
    }

    Here exit() calls the default exit handlers which which will deallocate all the allocated nodes which are not used!!

  3. There are some errors in the code..
    here is the program free of errors…

    // Program of circular linked list
    #include
    #include
    #include
    #include

    struct node
    {
    int info;
    struct node *link;
    }*last;

    void create_list(int);
    void addatbeg(int);
    void addafter(int,int);
    void del(int);
    void display();

    void main()
    {
    int choice,n,m,po,i;
    last=NULL;
    while(1)
    {
    printf(“1.Create List\n”);
    printf(“2.Add at begining\n”);
    printf(“3.Add after \n”);
    printf(“4.Delete\n”);
    printf(“5.Display\n”);
    printf(“Enter your choice : “);
    scanf(“%d”,&choice);

    switch(choice)
    {
    case 1:
    printf(“How many nodes you want : “);
    scanf(“%d”,&n);
    for(i=0; i info = num;

    if(last == NULL)
    {
    last = tmp;
    tmp->link = last;
    }
    else
    {
    tmp->link = last->link; /*added at the end of list*/
    last->link = tmp;
    last = tmp;
    }
    }/*End of create_list()*/

    void addatbeg(int num)
    {
    struct node *tmp;
    tmp = (node*)malloc(sizeof(struct node));
    tmp->info = num;
    tmp->link = last->link;
    last->link = tmp;
    }/*End of addatbeg()*/

    void addafter(int num,int pos)
    {

    struct node *tmp,*q;
    int i;
    q = last->link;
    for(i=0; i link;
    if(q == last->link)
    {
    printf(“There are less than %d elements\n”,pos);
    return;
    }
    }/*End of for*/
    tmp = (node*)malloc(sizeof(struct node) );
    tmp->link = q->link;
    tmp->info = num;
    q->link = tmp;
    if(q==last) /*Element inserted at the end*/
    last=tmp;
    }/*End of addafter()*/

    void del(int num)
    {
    struct node *tmp,*q;
    if( last->link == last && last->info == num) /*Only one element*/
    {
    tmp = last;
    last = NULL;
    free(tmp);
    return;
    }
    q = last->link;
    if(q->info == num)
    {
    tmp = q;
    last->link = q->link;
    free(tmp);
    return;
    }
    while(q->link != last)
    {
    if(q->link->info == num) /*Element deleted in between*/
    {
    tmp = q->link;
    q->link = tmp->link;
    free(tmp);
    printf(“%d deleted\n”,num);
    return;
    }
    q = q->link;
    }/*End of while*/
    if(q->link->info == num) /*Last element deleted q->link=last*/
    {
    tmp = q->link;
    q->link = last->link;
    free(tmp);
    last = q;
    return;
    }
    printf(“Element %d not found\n”,num);
    }/*End of del()*/

    void display()
    {
    struct node *q;
    if(last == NULL)
    {
    printf(“List is empty\n”);
    return;
    }
    q = last->link;
    printf(“List is :\n”);
    while(q != last)
    {
    printf(“%d “, q->info);
    q = q->link;
    }
    printf(“%d\n”,last->info);
    }/*End of display()*/

  4. excellent code. minor changes are required – indentation is missing, data variables need to be named properly (including function names), documentation and for exit use exit(0) if you are using turbo C or ansi C else exit(EXIT_SUCCESS) or exit(EXIT_FAILURE) with header file #include for linux.

  5. Hi Jishnu,

    Thanks for Such a nice programming.It is really helpful to gues.

  6. @Sanju Uthaiah,
    Thanks, It is my mistake, I didnt check error conditions when I wrote this code.

  7. Pingback: Circular Linked list in C with Append and display options | The Code Cracker

  8. god program but to display list, usless using while we can also use do..while….coz it will save one line of code n will also print contain of list is there is only one node in list….

  9. In the del(), I want the user to give a particular position as an input where the node deletion will take place, instead of giving an element as an input to delete, like the case here …..how can I do that? can any1 help please with respect to the above code? Thanks..

Leave a Reply