Thread: Inserting in linked list

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Inserting in linked list

    I'm trying to make a function that lets me pass an index and a string and will insert the string at that index in the linkedlist... any help would be appreciated
    Code:
    typedef struct node {
      char* value;
      struct node* next;
    } LinkedList;
      
      
    void llAddAtIndex(LinkedList** ll, char* value, int index) {
      
      LinkedList* newNode =(LinkedList*)malloc(sizeof(LinkedList));
      newNode->value = value;
      LinkedList* prevNode = NULL;
      LinkedList p;
      p = ?????
        
      for(int i = 0; i < index; i++){
          prevNode = p;
        p = p->next;
      }
        if (prevNode) { 
          prevNode->next = newNode;
          newNode->next = p;
        } else { 
            head = newNode;
            newNode->next = p;
          }
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Double-posting your question doesn't make answers come any quicker.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Efficiency

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quite the opposite.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    I oppose you

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Do you have a question?

    You have a variable called prevNode. So what is p for?
    Where should prevNode point to start iterating through the list?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by johngoodman View Post
    I oppose you
    How To Ask Questions The Smart Way
    Try to imagine a board where everyone behaved like you do, posted every question twice - and then someone upped the ante by posting 3 times.
    How quickly do you think the board would become a spam-fest and all the people who help would bugger off and find something better to do with their time.

    Oh, and this is what happens to duplicate threads - CLOSED!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inserting into a linked list
    By christianB in forum C Programming
    Replies: 3
    Last Post: 06-06-2012, 03:54 PM
  2. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  3. Inserting into linked list
    By mikeman in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2010, 07:46 PM
  4. inserting into linked list
    By MiroMage in forum C Programming
    Replies: 4
    Last Post: 09-16-2008, 07:55 PM
  5. linked list inserting
    By comar in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:01 AM