Removing from linked list

This is a discussion on Removing from linked list within the C Programming forums, part of the General Programming Boards category; Here are the typedefs, I thought I had everything planned out on paper and working but when I ran it ...

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

    Removing from linked list

    Here are the typedefs, I thought I had everything planned out on paper and working but when I ran it I keep getting crashed program.
    Just trying to remove element from LinkedList, I am setting the temp 1 behind the index that I am trying to remove then setting that tempo to the second temp that is pointing to what the "Removee" next.


    Code:
    typedef struct NodeT
    {
    	NodeItemT info;
    	struct NodeT *next;
    } NodeT;
    
    typedef struct VectorT
    {
    	NodeT *anchor;
    } VectorT;


    Code:
      
      void removeElement(VectorT *vector, int index)
    {
         NodeT *tempR;
         tempR=malloc(sizeof(NodeT));
         tempR=vector->anchor; // setting temp node to head of list
         int i=0;
         while(i<index)
             {
              tempR=tempR->next;
              i++;
             }
         NodeT *tempR2;
         tempR2=malloc(sizeof(NodeT));
         tempR2=vector->anchor;
         i=0;
              while(i<(index-1))
              {
              tempR=tempR->next;
              i++;
              }
         tempR2->next=tempR->next;
         tempR->next=NULL;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you mallocing in your 'remove' function?
    Code:
    if first node is thing to remove
        ptr = head->next
        free head
        head = ptr
    else
        prev = head
        prt = head->next
        whle ptr is not the one to remove
            cur = ptr
            ptr = ptr->next
        if ptr
            cur->next = ptr->next
            free ptr
    Something like that.


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

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    I thought that I needed to malloc efficient space for the pointer or is that only if I am adding elements?

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    What's with all the linked list questions lately? School time again eh?

    First off you've got memory leaks all over the place. To remove an node you don't need to call malloc(), you need to call free().

    Also you can keep a previous node as you traverse, instead of requiring two separate traversals.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Thanks Quzah Got it working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing odd indicies from linked list
    By Ryan. in forum C Programming
    Replies: 8
    Last Post: 07-11-2010, 02:09 AM
  2. Removing A Node From A Linked List In C
    By Creal Default in forum C Programming
    Replies: 7
    Last Post: 10-08-2007, 02:21 AM
  3. removing duplicates from a linked list
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2003, 10:27 PM
  4. removing dups from a linked list
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2003, 12:09 PM
  5. removing nodes from linked list
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2003, 03:24 PM

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