Thread: adding to linked list

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

    adding to linked list

    I keep getting a program crash whenever I am adding an element to my linked list but I just can not seem to figure out what is going wrong I think it might have to do with my anchor node.



    Code:
    typedef char NodeItemT;
    
    typedef struct NodeT
    {
    	NodeItemT info;
    	struct NodeT *next;
    } NodeT;
    
    typedef struct VectorT
    {
    	NodeT *anchor;
    } VectorT;
    
    
    VectorT *newVector();
    void addElement(VectorT *vector, NodeItemT item);
    
    
    int main()
    {
        VectorT *head;
        head=newVector();
        
        addElement(head,'A');
        
        
        
    getch();
    return 0;    
    }
    
    VectorT *newVector()
    {
     VectorT *vector;
     vector= (VectorT*) malloc(sizeof(VectorT));
     vector->anchor=(NodeT*)malloc(sizeof(NodeT));
     vector->anchor=NULL;;        
     return vector;       
    }
    
    
    void addElement(VectorT *vector, NodeItemT item)
    {
        NodeT *temp;
        temp=(NodeT*)malloc(sizeof(NodeT));
        temp=vector->anchor; 
        while(temp->next!=NULL)
        {
        temp=temp->next;
        }
        
        NodeT *temp1;
        temp1=(NodeT*)malloc(sizeof(NodeT));
        temp1->info=item;
        temp1->next=NULL;
        temp->next=temp1;
         
       
    }

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    I think it might have to do with my anchor node.
    Code:
    VectorT *newVector()
    {
     VectorT *vector;
     vector= (VectorT*) malloc(sizeof(VectorT));
     vector->anchor=(NodeT*)malloc(sizeof(NodeT));
     vector->anchor=NULL;;     //<---------
     return vector;      
    }
    Consider this post signed

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    yeah that doubly semicolon was a typo but it still crashes whenever I add an element


    Code:
    typedef char NodeItemT;
    
    typedef struct NodeT
    {
    	NodeItemT info;
    	struct NodeT *next;
    } NodeT;
    
    typedef struct VectorT
    {
    	NodeT *anchor;
    } VectorT;
    
    
    VectorT *newVector();
    void addElement(VectorT *vector, NodeItemT item);
    NodeItemT get(const VectorT *vector,int index);
    
    
    int main()
    {
        VectorT *head;
        head=newVector();
        
        addElement(head,'A');
        addElement(head,'B');
        addElement(head,'C');
        NodeItemT retrieve;
        retrieve=get(head,0);
        printf("%c\n",retrieve);
        
        
        
    getch();
    return 0;    
    }
    
    
    VectorT *newVector()
    {
     VectorT *vector;
     vector= (VectorT*) malloc(sizeof(VectorT));
     vector->anchor=(NodeT*)malloc(sizeof(NodeT));  
     vector->anchor->next=NULL;      
     return vector;       
    }
    
    
    void addElement(VectorT *vector, NodeItemT item)
    {
         
        if(vector->anchor->next==NULL)
        vector->anchor->info=item;
        
        else
        {
        NodeT *temp;
        temp=(NodeT*)malloc(sizeof(NodeT));
        temp=vector->anchor; 
        while(temp->next!=NULL)
        {
        temp=temp->next;
        }
        
        NodeT *temp1;
        temp1=(NodeT*)malloc(sizeof(NodeT));
        temp1->info=item;
        temp1->next=NULL;
        temp->next=temp1;
        }
      
    }
    
    
    NodeItemT get(const VectorT *vector,int index)
    {
         int i;
         NodeT *getItem;
         getItem=(NodeT*)malloc(sizeof(NodeT));
         getItem=vector->anchor;
         for(i=0;i<index;i++)
         {
           getItem=getItem->next;                 
         }    
         return getItem->info; 
              
                
    }
    Last edited by camel-man; 10-08-2011 at 04:55 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    As a programmer you're going to need to start looking into debugging this kind of thing. Rather than doing either the "throw your arms up in the air" approach or the "spend hours staring at the code hoping something jumps out at you" approach, it's probably time to start learning to use a debugger. You'll be glad you did.
    Now, what development environment do you have?

    Oh and FYI, you could have 100 secmicolons on that line and it wouldn't make a difference.

    Lastly, make sure you initialise stuff, even dynamically allocated stuff.
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why does newVector make a new node too? Also, if you actually included the right headers, you wouldn't be forced to typecast every return from malloc all the time. Void pointers don't need casts to be assigned to things. Why would addelement ever encounter a vector without an anchor, since when you make a new vector through your function, it automatically makes a node? I suppose it's just good error checking - but your new vector function shouldn't be adding nodes in my opinion. You are over complicating this a bit.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I am using Dev C++ and I will have too look into debugging im not sure quite what it is but if it can help me with these run time errors and segmentation faults then I am all for it.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Camel-Man.... Dev C++ is abandonware (has been for at least 5 years now) and the compiler that comes with it is about 3 generations old.

    I'm thinking it's time to update to C-99 standards... Pelles C ... build 32 or 64 bit executables, full set of resource tools and editors, full set of windows headers (current), complete documentation... and free.

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by CommonTater View Post
    Camel-Man.... Dev C++ is abandonware (has been for at least 5 years now) and the compiler that comes with it is about 3 generations old.

    I'm thinking it's time to update to C-99 standards... Pelles C ... build 32 or 64 bit executables, full set of resource tools and editors, full set of windows headers (current), complete documentation... and free.
    Will C-99 be more sufficient and accurate to GCC then dev-C++ is to gcc?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by camel-man
    Will C-99 be more sufficient and accurate to GCC then dev-C++ is to gcc?
    What do you mean?
    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

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Well I just noticed whenever I would transfer code from Dev_c++ to GCC I would have errors alot, I was just wondering If this new compiler is better

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    dev-c++ is an IDE, not a compiler.

    It comes with GCC. The only difference being it is a version of GCC that is about 5 years older than the version of GCC that comes with say the latest version of code::blocks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by camel-man
    Well I just noticed whenever I would transfer code from Dev_c++ to GCC I would have errors alot
    Dev-C++ comes with gcc by default, so that begs the question of what you mean by "transfer code from Dev_c++ to GCC".

    Quote Originally Posted by camel-man
    I was just wondering If this new compiler is better
    It will probably be worse, if the compiler that comes with Dev-C++ is your measure of "best". As CommonTater has noted, that compiler is not as updated.
    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

  13. #13
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by laserlight View Post
    Dev-C++ comes with gcc by default, so that begs the question of what you mean by "transfer code from Dev_c++ to GCC".


    It will probably be worse, if the compiler that comes with Dev-C++ is your measure of "best". As CommonTater has noted, that compiler is not as updated.
    So do you reccomend pelles C-99 over Dev-C++?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I realize the might come off as mean, but:
    C99 is a language standard, meant to be followed by compiler vendors.
    Pelles C is an IDE and an implementation of C99.

    If you can't write C, switching compilers because its C99 is not going to magically make your code better and I would wager that there are going to be the same amount of errors. I'm not sure what you're expecting.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    There is a newer version for Dev-C++

    Orwell's Engine: Dev-C++ 4.9.9.3 released

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding nodes into linked list
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2007, 04:03 PM
  2. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  3. Me again! This time adding to the linked list
    By Welshy in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2005, 08:10 AM
  4. Adding To The Middle Of A Linked List
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2003, 06:02 PM
  5. Adding a node to a linked list
    By slopeski007 in forum C Programming
    Replies: 2
    Last Post: 02-02-2003, 12:31 AM