C Program to perform Searching in a linked list

C Program for Search in 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 an element to insert in list: ");
    scanf("%d",&temp->data);
    temp->next=NULL;
    if(head==NULL)
        head=temp;
    else{
        ptr=head;
        while(ptr->next!=NULL){
            ptr=ptr->next;
        }
        ptr->next=temp;
    }
}
 
void search(){
    struct node *temp;
    int data,i=0,flag;
    temp=head;
    if(temp==NULL)
        printf("Linked List is empty");
    else{
        printf("Enter an element to search:");
        scanf("%d",&data);
        while(temp!=NULL){
            if(temp->data==data){
                printf("Value founded at location %d " ,i+1);
                flag=0;
                break;
            }
            else{
                flag=1;
            }
            i++;
            temp=temp->next;
        }
        if(flag==1){
            printf("Value not found in Linked List\n");
        }
 
    }
}

void viewList(){
    struct node* temp=head;
    if(temp==NULL){
        printf("list is empty");
    }
    else{
        printf("Elements of Linked List: ");
        while(temp->next!=NULL)
        {
            printf("%d\t",temp->data);
            temp=temp->next;
        }
        printf("%d \t",temp->data);
    }
}

int menu(){
    int choice;
    printf("\n 1.Insert at end of Linked List");
    printf("\n 2.Search an element in List");
    printf("\n 3.View/Traverse linked List");
    printf("\n 4.Exit");
    printf("\n Please enter your choice: \t");
    scanf("%d",&choice);
    return(choice);
}
void main(){
        printf("**C Program To Perform Search in Linked List**");
        while(1){
        switch(menu()){
            case 1:
                insertNode();
                break;
            case 2:
                search();
                    break;
            case 3:
                viewList();
                    break;
            case 4:
                exit(0);
            default:
                printf("invalid choice");
              }
        getch();
     }
}

Output:

**C Program To Perform Search in Linked List**
 1.Insert at end of Linked List
 2.Search an element in List
 3.View/Traverse linked List
 4.Exit
 Please enter your choice:      1
Enter an element to insert in list: 4

 1.Insert at end of Linked List
 2.Search an element in List
 3.View/Traverse linked List
 4.Exit
 Please enter your choice:      1
Enter an element to insert in list: 6

 1.Insert at end of Linked List
 2.Search an element in List
 3.View/Traverse linked List
 4.Exit
 Please enter your choice:      1
Enter an element to insert in list: 2

 1.Insert at end of Linked List
 2.Search an element in List
 3.View/Traverse linked List
 4.Exit
 Please enter your choice:      1
Enter an element to insert in list: 9

 1.Insert at end of Linked List
 2.Search an element in List
 3.View/Traverse linked List
 4.Exit
 Please enter your choice:      3
Elements of Linked List: 4      6       2       9 
 1.Insert at end of Linked List
 2.Search an element in List
 3.View/Traverse linked List
 4.Exit
 Please enter your choice:      2
Enter an element to search:6
Value founded at location 2 
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now