Problem with Linked list ()

This is a discussion on Problem with Linked list () within the C Programming forums, part of the General Programming Boards category; Hi, i'm new in c, I have a problem with a Linked list maybe you can help me I made ...

  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
    21,936
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,936
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,308
    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, 01:45 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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