May 8, 2012

C Program to search the linked list.

Data structures using C,
Write c program to search the linked list.
Linked list is a data structure in which the objects are arranged in a linear order. In this program, we sort the list elements in ascending order. Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct l_list
{
    int info;
    struct link *next;
}
start, *node;

int search(int);
void main()
{
    int no,i,item,pos;
    clrscr();
    start.next=NULL;
    node=&start;
    printf("How many nodes, you want in linked list? ");
    scanf("%d",&no);
    printf("");
    for(i=0;i<no;i++)
    {
        node->next=(struct l_list *)malloc(sizeof(struct l_list));
        printf("Enter element in node %d: ",i+1);
        scanf("%d",&node->info);
        node=node->next;
    }
    node->next=NULL;
    printf("Linked list(only with info field) is:");
    
    node=&start;
    while(node->next!=NULL)
    {
        printf("%d    ",node->info);
        node=node->next;
    }
    printf("
    
    Enter item to be searched : ");
    scanf("%d",&item);
    pos=search(item);
    if(pos<=no)
    printf("Your item is at node %d",pos);
    else
    printf("Sorry! item is no in linked list.a");
    getch();
}

int search(int item)
{
    int n=1;
    node=&start;
    while(node->next!=NULL)
    {
        if(node->info==item)
        break;
        else
        n++;
        node=node->next;
    }
    return n;
}
Read more Similar C Programs
Data Structures

Learn C Programming
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link