Program of Circular Linked List

 

Circular Linked List is another remedy for the drawbacks of the Single Linked List besides Doubly Linked List. A Slight change to the structure of a linear list is made to convert it to circular linked list; link field in the last node contains a pointer back to the first not Null.

 

 

/* Program of circular linked list*/

# include <stdio.h>

# include <malloc.h>

struct nodee

{

                int infoo;

                struct nodee *linkk;

}*lastt;

main()

{

                int choicee,no,mm,poo,ii;

                lastt=NULL;

                while(1)

                {

                    printf("Press 1...Create List\n");

               printf("Press 2..Add at begining\n");

                           printf("Press 3...Add after \n");

                           printf("Press 4...Delete\n");

                           printf("Press 5...Display\n");

                           printf("Press 6...Quit\n");

                           printf("Enter your choicee : ");

                                scanf("%d",&choicee);

                                switch(choicee)

                                {

                                 case 1:

                                                printf("Enter the node......> : "); /* to be created*/

                                                     scanf("%d",&no);

                                                for(ii=0; ii < no;ii++)

                                                {

                                                                printf("Enter the data(number) value.......> : \n");

                                                                scanf("%d",&mm);

                                                                create_list(mm);

                                                }

                                                break;

                                               

                                 case 2:

                                                printf("Enter the number(for beginning) .......> : ");

                                                     scanf("%d",&mm);

                                                addatbeg(mm);

                                                break;

                                               

                                 case 3:

                                                printf("Enter the number(for any position)........> : ");

                                                     scanf("%d",&mm);

                                                printf("Enter the poosition......>\n ");

                                                scanf("%d",&poo);

                                                addafter(mm,poo);

                                                break;

                                               

                                 case 4:

                                                if(lastt == NULL)

                                                {

                                                                printf("Oops! List underflow\n");

                                                                continue;

                                                }

                                                printf("Enter the any(for deletion) one number......>:\n ");

                                                    

                                                    

                                                scanf("%d",&mm);

                                                del(mm);

                                                break;

                                 case 5:

                                                display();

                                                break;

                                 case 6:

                                                exit();

                                 default:

                                                printf("Wrong choicee\n");

                                }/* switch End*/

                }/* while End*/

}/* main() End*/

create_list(int numm)

{

                struct nodee *qq,*tmpp;

                tmpp= malloc(sizeof(struct nodee));

                tmpp->infoo = numm;

                if(lastt == NULL)

                {

                                lastt = tmpp;

                                tmpp->linkk = lastt;

                }

                else

                {

                                tmpp->linkk = lastt->linkk; /*added at the end of list*/

                                lastt->linkk = tmpp;

                                lastt = tmpp;

                }

}/* create_list() End*/

addatbeg(int numm)

{

                struct nodee *tmpp;

                tmpp = malloc(sizeof(struct nodee));

                tmpp->infoo = numm;

                tmpp->linkk = lastt->linkk;

                lastt->linkk = tmpp;

}/* addatbeg() End*/

addafter(int numm,int poos)

{

                struct nodee *tmpp,*qq;

                int ii;

                qq = lastt->linkk;

                for(ii=0; ii < poos-1; ii++)

                {

                                qq = qq->linkk;

                                if(qq == lastt->linkk)

                                {

                                                printf("There are less than %d elements\n",poos);

                                                return;

                                }

                }/* for End*/

                tmpp = malloc(sizeof(struct nodee) );

                tmpp->linkk = qq->linkk;

                tmpp->infoo = numm;

                qq->linkk = tmpp;

                if(qq==lastt)    /*Element inserted at the end*/

                                lastt=tmpp;

}/* addafter() End*/

del(int numm)

{

                struct nodee *tmpp,*qq;

                if( lastt->linkk == lastt && lastt->infoo == numm)  /*Only one element*/

                {

                                tmpp = lastt;

                                lastt = NULL;

                                free(tmpp);

                                return;

                }

                qq = lastt->linkk;

                if(qq->infoo == numm)

                {

                                tmpp = qq;

                                lastt->linkk = qq->linkk;

                                free(tmpp);

                                return;

                }

                while(qq->linkk != lastt)

                {

                                if(qq->linkk->infoo == numm)     /*Element deleted in between*/

                                {

                                                tmpp = qq->linkk;

                                                qq->linkk = tmpp->linkk;

                                                free(tmpp);

                                                printf("%d deleted\n",numm);

                                                return;

                                }

                                qq = qq->linkk;

                }/* while End*/

                if(qq->linkk->infoo == numm)    /*lastt element deleted qq->linkk=lastt*/

                {

                                tmpp = qq->linkk;

                                qq->linkk = lastt->linkk;

                                free(tmpp);

                                lastt = qq;

                                return;

                }

                printf("Element %d not found\n",numm);

}/* del() End*/

display()

{

                struct nodee *qq;

                if(lastt == NULL)

                {

                                printf("List is empty\n");

                                return;

                }

                qq = lastt->linkk;

                printf("List is :\n");

                while(qq != lastt)

                {

                                printf("%d ", qq->infoo);

                                qq = qq->linkk;

                }

                printf("%d\n",lastt->infoo);

}/* display() End*/

Output.....

Circular Linked List

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @