Search Element in Single Circular Linked List in C

Description

This program is used to Search Element in Single Circular Linked List Using C

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

struct node
{
 int data;
 struct node * next;
};
struct node *head=NULL;
void display();
void create();
void search();

void main()
{
  int choice;
  do
  {
   printf("\n__MENU__\n1.Create List\n2.Search Element\n3...EXIT...\n");
   printf("\nENTER your choice\n");
   scanf("%d",&choice);
   switch(choice)
   {
    case 1 :create();break;
    case 2 :search();break;
    case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break;
   default:printf("INVALID input !!!");break;   
  }
  }while(choice!=3);
}

void create()
{
 
 int x, i,n;
 head=NULL;
 struct node* newnode,*temp;
 temp=head;
 printf("Enter no. of elements in the LIST \n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  temp=head; 
  newnode=(struct node*)malloc(sizeof(struct node*)); 
  scanf("%d",&newnode->data);
     if(head==NULL)
     {
      head=newnode;
      newnode->next=head;
  }
     else
  {
         while(temp->next!=head)
   temp=temp->next;
   temp->next=newnode;
   newnode->next=head; 
  }
 }
 display();
}

void display()
{
 struct node* temp;
 temp=head;
 if(head==NULL)
  printf("List is empty !!!");
 else 
 {
  printf("List elements are :\t");
  while(temp->next!=head)
  {
   printf("%d\t",temp->data);
   temp=temp->next;
  }
  printf("%d\n",temp->data);
 }
}

void search()
{
 int a,i=1,value;
 struct node* temp;
 temp=head;
 if(head==NULL)
 printf("\nlist is empty !!!!!\n");
 else
 {
  printf("Enter element to be searched\n");
  scanf("%d",&value);
     if(temp->data==value)
     printf("\n%d is at position 1.\n ",value);
     else
     {
      while(temp->next!=head)
      {
       temp=temp->next;
       i++;
       if(temp->data==value)
       break;
       
   }
   if(temp->data==value)
   printf("\n%d is at position %d.\n ",value,i);
   else if(temp->next==head)
   printf("\nELEMENT NOT FOUND !!!\n");
  }
 }
 
}

OUTPUT

Search List Output 1 Search List Output 2

Labels: