Thread: adding to middle of linked list

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

    adding to middle of 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... here is so function i have so far, but am stuck and don't know where to go from here, 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
    Feb 2003
    Posts
    596
    Aren't you supposed to find a position in the list that's passed into the function?

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by R.Stiltskin View Post
    Aren't you supposed to find a position in the list that's passed into the function?
    Yeah, I was just kind of blindly typing stuff, but thats what I want it to do

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I think the operative words there were "Aren't you supposed ..."

    Start at the head of the list.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What's 'head'?
    You actually have far more problems than just line 13.
    Quite frankly, the existence of a facility to perform an action with an item at a specified index of a linked-list is plain wrong. It leads to code that suffers from the combined disadvantages of boths lists and arrays, whilst also providing few of the benefits of either. It's quite simply an abomination to begin with.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding to linked list
    By camel-man in forum C Programming
    Replies: 15
    Last Post: 10-09-2011, 02:13 PM
  2. Adding a node to linked list
    By laurenlb in forum C Programming
    Replies: 1
    Last Post: 04-14-2011, 07:05 PM
  3. Adding nodes into linked list
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2007, 04:03 PM
  4. Help with deleting node in the middle of a linked list
    By allplay in forum C Programming
    Replies: 2
    Last Post: 11-11-2006, 11:22 AM
  5. Adding To The Middle Of A Linked List
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2003, 06:02 PM