Thread: Array List Insertion

  1. #1
    Registered User
    Join Date
    Feb 2013

    Array List Insertion

    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 confused on whether or not im headed in the right direction...any help would be great!

    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
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Nice, France
    Take a piece of paper and a pencil. Draw the list and try to accomplish the same thing that you want your function to do. Figure out what you did on the paper and then do them in the list too
    Code - functions and small libraries I use

    It’s 2014 and I still use printf() for debugging.

    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Starting three threads for the same thing, will only achieve us wanting to say things to you that would probably see us banned for saying them.
    So, do us a favour and imagine we just said such things, then either comform to the rules or find help elsewhere.
    The amount of help you get is inversely proportional to the amount that you erm... 'annoy' people, to put it politely.
    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"

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Some help:

    struct node *insertleft(struct node *lst, int index, struct node *np);

    This way you avoid unnecessary typedefs, you don't have to handle allocations in a function that needn't handle them, the call is more natural since you just pass the value of the list rather than a pointer to it, and the name of the function is more specific (it inserts np to the left of the index'th element in lst) and it doesNotUseThisAnnoyingFormat.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The engines cannae tek it captn
    Array List Insertion-screenshot-2013-02-18-07-23-49-png
    Three new threads in three hours - one subject.

    Oh this isn't going to end well for someone if this keeps going...

    Closed (again).
    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. HELP! Linked List Insertion
    By divisionbyzero in forum C Programming
    Replies: 1
    Last Post: 04-08-2010, 03:43 PM
  2. insertion in an ordered list ?
    By jack_carver in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 11:11 AM
  3. insertion in a sorted linked list
    By sagsriv in forum C Programming
    Replies: 4
    Last Post: 03-15-2008, 02:40 PM
  4. Linked List alpha insertion
    By mouse163 in forum C++ Programming
    Replies: 9
    Last Post: 02-21-2005, 02:24 PM
  5. Insertion Linked List Help
    By 0rion in forum C Programming
    Replies: 4
    Last Post: 05-12-2004, 07:38 AM