Searching in linked list problem!

This is a discussion on Searching in linked list problem! within the C++ Programming forums, part of the General Programming Boards category; Hi, I wanted to search an element (char) in the list S, but everytime it said that 02x is not ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9

    Arrow Searching in linked list problem!

    Hi,
    I wanted to search an element (char) in the list S, but everytime it said that 02x is not in S
    How to declare char target in a manner way? i think its my mistake for this code.
    Thanks.

    Code:
    struct Node;typedef Node* NodePtr;
    typedef void* VoidPtr;
    typedef char* Ptr;
    typedef int* Pi;
    struct Node
    {
    	VoidPtr data;
    	NodePtr next;
    };

    Code:
    bool Search(NodePtr& head, VoidPtr& target)	
    {
    	bool found = false;
    	
    	NodePtr here = head;   
    	
    	while (here != NULL && !found) 
    	{
    		if (here->next ==target)
    			found =true;
    		else
    			here = here ->next;
    			return found;
    	}
    	
    	
    }
    
    
    
    
    void Search_Menu (NodePtr& head)
    {
    	VoidPtr target;
    	cout <<setw(70)<<setfill('=')<<endl;
    	PrintList(head);
    	
    	cout <<"\nWhich element?: ";
    	cin >>target;
    	
    	if((Search(head, target)==true))
    	{
    		cout <<target<<" is in S ."<<endl;
    	}
    	else
    	{
    		cout <<target <<" is not in S."<<endl;
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Fix your indentation and you'll see at least one problem :

    Code:
        while (here != NULL && !found)
        {
            if (here->next ==target)
                found =true;
            else
                here = here ->next;
            return found;
        }
    Also, are you sure that you're supposed to be searching for the address a node is stored at rather than the data stored in it?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9
    I moved return found out of the loop , but still got error for the line if (here->next ==target).

    Because I declare target as VoidPointer as requirement , This is what I'm stucking...

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    You'll need to compare the data elemenet of each node to the target, not the next element. However you'll need to know what type was actually stored there in order to be able to compare them. Normally one doesn't use void pointers in C++. We use templates instead.
    If you insist on using void pointers then your best option is to have the called provide a comparison function as an argument to the Search routine.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9
    Code:
    int compare(VoidPtr vp1, VoidPtr vp2){
    	char i = *(static_cast <char*>(vp1));
    	char j = *(static_cast <char*>(vp2));
    	
    	if (i==j)
    		return 0;
    
    
    	else if( i<j)
    		return -1;
    	else 
    		return 1;	 	 	 
    		
    }
    bool Search(NodePtr& head, VoidPtr target)
    	
    {
    	
    	bool found = false;
    	
    	NodePtr here = head;   
    	
    	while (here != NULL && !found) 
    	{
    		if (compare(here->data,target)==0)
    			found =true;
    		else
    			here = here ->next;
    			
    	}
    	
    		return found;
    }
    
    
    
    
    void Search_Menu (NodePtr& head)
    {
    	VoidPtr target;
    	cout <<setw(70)<<setfill('=')<<endl;
    	PrintList(head);
    	
    	cout <<"\nWhich element?: ";
    	cin >>*(static_cast <char*>(target));
    	
    	if(Search(head, target)==true)
    	{
    		cout <<*(static_cast <char*>(target))<<" is in S ."<<endl;
    	}
    	else
    	{
    		cout <<*(static_cast <char*>(target)) <<" is not in S."<<endl;
    	}
    }
    I changed to comparison of void pointer ... but it still does not work ...Any advise? Thanks

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    cin >>*(static_cast <char*>(target));
    What is this supposed to mean? If you want to read a string, you need a string to put the input into. Not some random place in memory that doesn't even belong to you.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9
    Thanks I changed VoidPtr target to char terget , worked...

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    Ummm... please post your code. Your latest code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching Linked List
    By JamesD in forum C Programming
    Replies: 7
    Last Post: 03-30-2011, 03:34 PM
  2. Searching/counting in a linked list
    By thoseion in forum C Programming
    Replies: 1
    Last Post: 11-11-2006, 07:21 PM
  3. searching a linked list
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-22-2003, 05:08 PM
  4. Searching Linked List
    By csmatheng in forum C Programming
    Replies: 1
    Last Post: 04-25-2002, 03:45 PM
  5. Searching a Linked-List
    By hyaline in forum C Programming
    Replies: 2
    Last Post: 09-12-2001, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21