Circular linked list/circular singly linked list

Circular linked list
In this linked list nodes are assessed in circular movement/direction. It does not maintain NULL pointer.
1.Circular singly linked list.
2.Circular doubly linked list.

Circular singly linked list.
In this linked list we can traverse from last node to first node directly,because the linked field the last node contains address of first address.



//circular singly linked list program
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
void create();
void display();
void sinsert();
void pinsert();
void einsert();
void sdelete();
void edelete();
void pdelete();
void serching();
void sorting();
struct node
{
    int info;
    struct node *link;
};

struct node *first=NULL,*last=NULL,*new1;
int main()
{
    int choice;
    do
    {
        system("cls");
        printf("press 1 to insert value in list\npress 2 to display list\npress 3 to insert at first position\npress 4 to insert at perticular position \npress 5 to insert at last position\n press 6 to delete firat value\n press 7 to delete last value\n press 8 to delete perticular node\n press 9 to serching perticular value\n press10 to sort value\n press 11 for exit.->\t");
        scanf("%d",&choice);
        switch (choice)
        {
    case 1:
        create(); break;
    case 2:
        display();break;
    case 3:
        sinsert();break;
    case 4:
        pinsert();break;
    case 5:
        einsert();break;
    case 6:
        sdelete();break;
    case 7:
        edelete();break;
    case 8:
        pdelete();break;
    case 9:
        serching();break;
    case 10:
        sorting();break;
    case 11:
        exit(0);
    default:
        printf("Invalid choice!");
        }

    }
    while(choice>0&&choice<12);

}
//create node in Circular singly linked list
void create()
{
    new1=(struct node*)malloc(sizeof(struct node));
    printf("enter the value of list\n");
    scanf("%d",&new1->info);
    new1->link=NULL;
    if(first==NULL)
        first=new1;
    else
        last->link=new1;

        last=new1;
        last->link=first;
}
//display node in Circular singly linked list
void display()
{
struct node *temp;

    if(first==NULL)
        printf("no node");
    else
    {
        temp=first;
        do
        {
            printf("%d\n",temp->info);
            temp=temp->link;
        }
        while(temp!=first);
    }

 system("pause");
}
//insert node in Circular singly linked list at first position
void sinsert()
{
    new1=(struct node*)malloc(sizeof(struct node));
    printf("Enter the value for inserting 1st position");
    scanf("%d",&new1->info);
    new1->link=NULL;
    new1->link=first;
    first=new1;
    last->link=first;
}
//insert node in Circular singly linked list at particular position
void pinsert()
{
    new1=(struct node*)malloc(sizeof(struct node));
    struct node *temp;
    int count1=0,p;
    temp=first;
    do
    {
        temp=temp->link;
        count1++;
    }
    while(temp!=first);
  printf("enter the position-");
  scanf("%d",&p);
  if(p>count1||p<1)
    printf("invalid position");
  else
  {
      printf("enter the value of list-");
      scanf("%d",&new1->info);
      count1=1;
      while(count1!=p-1)
      {

          temp=temp->link;
          count1++;
      }
      new1->link=temp->link;
      temp->link=new1;
  }
   system("pause");
}
//insert node in Circular singly linked list at last position
void einsert()
{
    new1=(struct node*)malloc(sizeof(struct node));
    printf("enter the value of list-");
    scanf("%d",&new1->info);
    last->link=new1;
    last=new1;
    last->link=first;

}
//delete node in Circular singly linked list at first position
void sdelete()
{
    printf("deleted recode=%d",first->info);
    first=first->link;
    last->link=first;

}
//delete node in Circular singly linked list at last position
void edelete()
{
    struct node *temp,*pre;
    temp=first;
    printf("deleted recode=%d",last->info);

    while(temp!=last)
    {
        pre=temp;
        temp=temp->link;
    }

    pre->link=first;
    last=pre;
    system("pause");
}

//delete node in Circular singly linked list at particular position
void pdelete()
{
    struct node *temp,*pre;
    int count1=0,position;
    printf("\nenter the position you want to delete-");
    scanf("%d",&position);
    temp=first;
    do
    {

        temp=temp->link;
        count1++;
    }
    while(temp!=first);

    if(position>count1||position<1)
        printf("invalid position!\n");
    else
    {
     temp=first;
     count1=1;
     do
     {
        pre=temp;
         temp=temp->link;
         count1++;
     }
     while(count1!=position);
     pre->link=temp->link;
  }
}
//searching node in Circular singly linked 
void serching()
{
    struct node *temp;
    int serch,flag=0;
    if(first==NULL)
        printf("no value available!");
    else
    {
        printf("enter the value for serching-");
        scanf("%d",&serch);
          temp=first;
        do
        {

            if(serch==temp->info)
            {
                flag=1;
                break;
            }
            temp=temp->link;

        }
        while(temp!=first);
        if(flag==1)
            printf("serching %d is found\n",temp->info);
        else
            printf("serching is not found!\n");

    }
    system("pause");
}

//delete node in Circular singly linked list at last position
void sorting()
{
    struct node *temp,*temp1;
    int pre;
    if(first==NULL)
        printf("no value available!");
    else
    {
        temp=first;

    do
    {
     for(temp1=temp->link;temp1!=first;temp1=temp1->link)
     {
         if(temp->info > temp1->info)
         {
             pre=temp->info;
             temp->info=temp1->info;
             temp1->info=pre;
         }
     }
     temp=temp->link;


    }
    while(temp!=first);

    }

do
{
    printf("%d\t",temp->info);
    temp=temp->link;
}
while(temp!=first);



system("pause");
}

I hope this post useful for you....
plzzz...follow me on facebook


Next topic__Circular doubly linked list


























Comments

Popular posts from this blog

python pattern programming

Decision making statement in python

javaScript Calculator.