C Program for Searching in Circular Linked List
#include<conio.h> #include<stdio.h> #include<stdlib.h> struct node{ int data; struct node *next; }; struct node *head=NULL; struct node* createNode(){ struct node *newNode = (struct node *)malloc(sizeof(struct node)); return (newNode); } void insertNode(){ struct node *temp,*ptr; temp=createNode(); printf("enter the data you want to insert:"); scanf("%d",&temp->data); temp->next=NULL; if(head==NULL){ head=temp; head->next=head; } else{ ptr=head; while(ptr->next!=head){ ptr=ptr->next; } ptr->next=temp; temp->next=head; } } void search(){ struct node *temp; int data,i=0,flag; temp=head; if(temp==NULL) printf("Linked List is empty"); else{ printf("Enter the data you want to search:"); scanf("%d",&data); if(temp->next==head){ printf("Value founded at location %d " ,i+1); } while(temp!=NULL){ if(temp->data==data){ printf("Value founded at location %d " ,i+1); flag=0; break; } else if(temp->next==head){ printf("value not found\n"); break; }else{ flag=1; } i++; temp=temp->next; } if(flag==1){ printf("value not found\n"); } } } int menu(){ int choice; printf("\n 1.Add value to the list"); printf("\n 2.Search an element"); printf("\n 3. exit"); printf("\n Please enter your choice: \t"); scanf("%d",&choice); return(choice); } void main(){ while(1){ switch(menu()){ case 1: insertNode(); break; case 2: search(); break; case 3: exit(0); default: printf("invalid choice"); } } getch(); }
Output:
Share On :