Table of Contents
C Program for Searching in Circular Linked List
#include<stdio.h>
#include<conio.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(){
printf("Program for Searching in Circular Linked List");
while(1){
switch(menu()){
case 1:
insertNode();
break;
case 2:
search();
break;
case 3:
exit(0);
default:
printf("invalid choice");
}
}
getch();
}
Output:
Program for Searching in Circular Linked List
1.Add value to the list
2.Search an element
3.Exit
Please enter your choice: 1
enter the data you want to insert:5
1.Add value to the list
2.Search an element
3.Exit
Please enter your choice: 1
enter the data you want to insert:8
1.Add value to the list
2.Search an element
3.Exit
Please enter your choice: 1
enter the data you want to insert:6
1.Add value to the list
2.Search an element
3.Exit
Please enter your choice: 2
Enter the data you want to search:8
Value founded at location 2
1.Add value to the list
2.Search an element
3.Exit
Please enter your choice: 3
What did you think?
Similar Reads
-
Multiplications of Two variable polynomial
Polynomial multiplication is a common operation in algebra, used in areas such as scientific computing, engineering simulations, and symbolic algebra… -
Subtraction of Two-Variable Polynomial in C
Polynomials with two variables are common in engineering, graphics, and scientific computation. While addition is frequently discussed, subtraction is equally… -
Explain the Addition of Two variable polynomial
Polynomials are fundamental in mathematics, and their use extends into computer science, engineering, physics, and more. While single-variable polynomials are… -
Reverses the Doubly Circular Linked List
Algorithm to Reverse a Doubly Circular Linked List Check if the List is Empty or Has Only One Node: If… -
Algorithm and Program in C to traverse Doubly Circular Linked List
A doubly circular linked list is a special type of linked list in which every node is connected to both… -
Polynomial representation Using Array
Polynomials are fundamental mathematical expressions used extensively in various fields. Representing them efficiently in programming is crucial for calculations and… -
Multiplication of Single Variable Polynomial : Algorithm and Program
Multiplication of single-variable polynomials is a fundamental operation in polynomial algebra. The process involves multiplying each term of one polynomial… -
Subtraction of Single variable Polynomial : Algorithm and Program
Subtracting single-variable polynomials involves reducing each corresponding term of the polynomials from each other. This process is similar to addition,… -
Addition of Single variable Polynomial : Program and Algorithm
The addition of single-variable polynomials is a fundamental concept in algebra, often encountered in mathematics and computer science. Let's break… -
Polynomial Representation Using Linked List
Polynomial representation using linked lists is a critical concept in computer science and mathematics. In this guide, we explore how…