Thread: Problem with Linked list ()

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    10

    Problem with Linked list ()

    Hi,
    i'm new in c, I have a problem with a Linked list maybe you can help me
    I made a linked list, and it's working fine, but i have a problem trying to access to an item using an index
    This is my structure and functions (i'll put a red note where i think the error is):
    Code:
    typedef struct node{
    	int a;
    	int b;
    	struct nodo *next;
    }List;
    
    //insert function:
    List *insert(List **lst,    int a,     int b){
    	assert(lst!=NULL);
    	List *node=malloc(sizeof(List));
    	node->next=*lst;
    	*lst=node;
    	node->a=a;
    	node->b=b;
    	return *lst;
    }
    
    List **get(List **lst, int index){
    	int i=0;
    	while ( (*lst != NULL) && (i< index) ){
    		lst=&(*lst)->next;
    		i++;
    	}
    	if (i==index){
                 return lst;
            }else{
    	     return NULL;
            }
    }
    
    //im calling the get from other function using this:
    int valuea=get(&alist,10)->a;
    the error i'm receiving is:
    error: request for member 'a' in something not a structure or union

    I'll appreciate your help!
    Thank you!
    Last edited by caraie; 08-29-2010 at 11:43 PM. Reason: I forgot the error note.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that get returns a pointer to a pointer to a List, but you are treating it as if it returned a pointer to a List. Since you do not need get to return a pointer to a pointer to List, you could just change it to return a pointer to a List. However, since you designed it to return a null pointer if the index does not exist, you may want to check that a null pointer was not returned.
    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

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    1
    how can i post a Tc like that an error always occur

    Please use code tags: insert
    Code:
     before your source code and
    after it in order to post.

    I also need help for my program

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    Hi LaserLight!, thank you for your response!
    I tried what you said, but now i'm receiving a warning (warning: return from incompatible pointer type)

    Code:
    List *get(List **lst, int index){
    	int i=0;
    	while ( (*lst != NULL) && (i< index) ){
    		lst=&(*lst)->next;
    		i++;
    	}
    	if (i==index){
                 return lst;
            }else{
    	     return NULL;
            }
    }
    I know it's a newbie question, but i'm new with c and it's making me crazy.
    Thank you again!


    Quote Originally Posted by laserlight View Post
    The problem is that get returns a pointer to a pointer to a List, but you are treating it as if it returned a pointer to a List. Since you do not need get to return a pointer to a pointer to List, you could just change it to return a pointer to a List. However, since you designed it to return a null pointer if the index does not exist, you may want to check that a null pointer was not returned.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I mean is something like this:
    Code:
    List *get(List *list, int index) {
        int i = 0;
        while (list != NULL && i < index) {
            list = list->next;
            ++i;
        }
        return list;
    }
    Your use would then be along the lines of:
    Code:
    /* Assume that alist is a pointer to List */
    List *found = get(alist, 10);
    int valuea = found ? found->a : 0;
    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

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    Thank you thank you thank you, you save my day!!!

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The idea of writing a linked-list function that obtains an item given its index is a fundamentally wrong thing to ever do.
    The moment you introduce an O(n) operation to a container, and that is obviously the kind of thing people will commonly want to use within a loop, you're virtually guaranteed to have a program full of hideously inefficient O(n*n) behaviour (and needlessly so).

    That is the reason why you will never find std::list or any other serious list implementation in any programming language exposing such a feature, unless the underlying structure is an array rather than a linked-list (which has a similar yet different problem if it includes erase or insert methods).
    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"

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    Hi iMalc, thank you for your suggestion.
    I know i'm doing a bad use of this std creating high order functions, I tried to make a Vector structure (with getat, push, etc.) functions but i lost all the day and i couldn't, I'm experimented with Java, C# and other programming languages but not with c (is my first week programming in this language). If you can recommend me an apropiated std implementation to use i'll try to use it.
    Thank you!

    Quote Originally Posted by iMalc View Post
    The idea of writing a linked-list function that obtains an item given its index is a fundamentally wrong thing to ever do.
    The moment you introduce an O(n) operation to a container, and that is obviously the kind of thing people will commonly want to use within a loop, you're virtually guaranteed to have a program full of hideously inefficient O(n*n) behaviour (and needlessly so).

    That is the reason why you will never find std::list or any other serious list implementation in any programming language exposing such a feature, unless the underlying structure is an array rather than a linked-list (which has a similar yet different problem if it includes erase or insert methods).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM