So I'm having trouble performing a linear search of a linked list class. The inList() search function is supposed to recieve a value for an element and search for that element, return a bool value based on whether or not the value is in the list or not.

Below is my header for what I'm trying to do. Can someone walk me through what I need to do?

#include<iostream>
using namespace std;

typedef int ElementType;
class LinkedList
{
    private: 
        class Node
        {
            public:
                ElementType data;
                Node * next;
        };
        typedef Node * NodePointer;
        NodePointer ptr;            //pointer to nodes
        NodePointer first;          //points to first node
        NodePointer predptr;        //points to predecessor node
        NodePointer newptr;         //points to a new node
        int mySize;                 //number of nodes
        int getPredecessor(int pos) //get predecessing node from a given position
        {
            if(pos == 0)
            return 0;
            
            else
            {
                predptr = first;
                for(int i = 0; i < pos-1; i++)
                {
                    predptr = predptr->next;
                }
                return predptr->data;     
            }       
        }

    public:
        LinkedList();                                              
        LinkedList(ElementType item);                                      
        void display(ostream & out)const;                            
        int length();                                                                 
        void insert(ElementType item, int pos);                     
        void erase(int pos);
        bool empty();
        bool inList(ElementType item);
        ~LinkedList();                                            
        LinkedList(const LinkedList & orig);                        
                 
};
ostream & operator <<(ostream & out, const LinkedList & aList);

And here is my partially done definition for inList (probably completely incorrect)

//inList definition
bool LinkedList::inList(ElementType item)
{
    bool found = false;
    int index = 0;
    ptr = first;
    
    while(index < mySize)
    {
        if(ptr->data == item)
        {
           cout << "FOUND ITEM" << endl;
           found = true;
           
        }
        index++;
    }
    return found;
}

In your inList function you are not moving your ptr. You do not even need the index and mySize. Here is something to get you stared :

bool isInList(const ElementType& elem){
 Node *curr = head;
 while curr is Not NULL{
   check if curr has the elem, if so return true;
   else advance curr to curr.next
 }
 return false;
}

Ah I gotcha! Worked like a charm. Thanks again firstPerson, you're help is much appreciated.

In your inList function you are not moving your ptr. You do not even need the index and mySize. Here is something to get you stared :

bool isInList(const ElementType& elem){
 Node *curr = head;
 while curr is Not NULL{
   check if curr has the elem, if so return true;
   else advance curr to curr.next
 }
 return false;
}
This article has been dead for over six months. Start a new discussion instead.