Thread: Search the data in the linked list

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    Question Search the data in the linked list

    How can I search the data? I want to get the output like this. Anyone can explain to me? Thanks in advance.

    ==============================
    Enter a data to search: 44
    DATA FOUND!
    ==============================

    Code:
    #include <iostream>
    using namespace std;
    
    
    struct node
    {
        int data;
        node *next;
    };
    
    
    int main ()
    {
        node *head, *temp, *prev;
        int i,x, target;
        //Creating the first node
        head = new node;
    
    
        for (int a=0; a<1; a++)
        {
            cout<<"Enter data " << a + 1 << ": ";
            cin>>x;
        }
    
    
    
    
        head->data = x;
        head->next = NULL;
        temp = head;
    
    
        for(i=0; i<4; i++)
        {
            temp -> next =new node;
            temp = temp -> next;
            cout<<"Enter data " << i +2 << ": ";
            cin>>x;
            temp->data = x;
            temp->next = NULL;
        }
    
    
        //Printing out the linked list
        temp = head;
    
    
        cout << "The Current List:" <<endl;
    
    
        while (temp !=NULL)
        {
            cout<<temp->data << "\t";
            temp = temp->next;
        }
    
    
    
    
        cout << endl;
        cout << endl;
        cout << "Deleting the first node" << endl;
        cout << "The list after deletion:" << endl;
    
    
        //Deleting a node
        temp = head;
        head = head->next;
        delete temp;
    
    
        //Printing out the linked list
    
    
        temp = head;
    
    
        while (temp != NULL)
        {
            cout<<temp->data<< "\t";
            temp = temp->next;
        };
    
    
        cout << endl;
        cout << endl;
        cout << "Enter a data to search: ";
        cin >> target;
    
    
        int found = 0;
    
    
        for(int i=0; i<4; i++)
        {
            if(array[i] == target)
            found = 1;
        }
    }
    
    


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I presume that other than those last few lines, you did not write the code you posted.

    I suggest that you go over the code that you did not write. What do you understand of it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list search
    By RounakJain in forum C Programming
    Replies: 1
    Last Post: 05-07-2013, 10:07 PM
  2. Help on Linked List Search
    By sivapc in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2009, 12:54 AM
  3. linked list search
    By rocketman03 in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 06:48 AM
  4. linked list search question..
    By transgalactic2 in forum C Programming
    Replies: 12
    Last Post: 10-29-2008, 04:40 PM
  5. Linked list search??
    By ren in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 08:49 AM

Tags for this Thread