Like Tree2Likes
  • 1 Post By Tclausex
  • 1 Post By Tclausex

Inserting in linked list

This is a discussion on Inserting in linked list within the C Programming forums, part of the General Programming Boards category; I'm trying to make a function that lets me pass an index and a string and will insert the string ...

  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.
    Salem likes this.

  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.
    Salem likes this.

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

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06: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

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