Solved programming problem

Find a Value From Linked List in C++.

Source code

#include <iostream>

using namespace std;

struct list

{

    int data;

    struct list *next;

};

typedef struct list node;

node *head;

int insert_node(node *start) /// input function

{

    node *temp,*last;

    temp= new node();

    cout<<"Please Enter data: ";

    cin>>temp->data;

    temp->next=NULL;

    if(start==NULL)

    {

        start= temp;

        head=temp;

    }

    else

    {

        last=start;

        while(last->next!=NULL)

        {

            last = last->next;

        }

        last->next = temp;

    }

    return 0;

}

int find_node(node *head, int n)///search node

{

    node *temp=head;

    while(temp!=NULL)

    {

        if(temp->data==n)

        {

            return 1;

        }

        temp = temp->next;

    }

    return 0;

}

int main()

{

    node *start;

    int n,flag;

    start = NULL;

    do  ///input

    {

        cout<<"Please Press 1 To insert a Data: ";

        cin>>flag;

        if(flag !=1) break;

        insert_node(start); ///call insert_node function for scan value

    }

    while(flag==1);

    cout<<"
Please Enter which Element You Want To Search: ";

    cin>>n;

    int f = find_node(head,n); ///call find_node function for find value

    if(f==1)

    {

        cout<<"
Found"<<endl;

    }

    else

    {

        cout<<"
Not Found"<<endl;

    }

    return 0;

}

Output

Please Press 1 To insert a Data: 1
Please Enter data: 3
Please Press 1 To insert a Data: 1
Please Enter data: 6
Please Press 1 To insert a Data: 1
Please Enter data: 9
Please Press 1 To insert a Data: 0

Please Enter which Element You Want To Search: 9

Found

 

Join the discussion

Add your comment