#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Array-based linked list


    I need help figuring out the algorithm to insert into an array of structs. I'm not allowed to use pointer since next will be pointing to the next node.

    Code:
    struct Inode { int item;int next; }; 
    
    Inode node[10];
    So, I have an array of 10 struct.
    I initialize all the next to 1 to 9, with the last next to -1.

    Code:
    ptr = free; free = node[free].next; 
     if (head == -1) { node[ptr].item = newItem; 
    node[ptr].next = -1; head = 0;}
    That was for the first insertion.
    so, I insert it into a free node, and set next to -1 to indicate that it is the end of the list.

    I'm having trouble with inserting the 2nd item.

    I need to change the next of the previous node to 1 instead of -1, and i'll be setting the next value to -1 on the 2nd node.

    Basically, I would want to linked 'item' in a sorted order.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Please, please, please do not use free for your own identifier.
    It is a very used function from the Standard library and seeing it with a different meaning is very troubling and makes following the code very very hard.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Why would you start off with all the "next"'s connected?
    I'd start with

    node[0].next = -1;
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Want to become a programmer C + +? EBook Cprogramming.com, jumped to C + +, will walk you through it, step by step. Get a jump in C + + today! Learning from sample code written by other programmers

    C + C source code + - complete software
    C + + C source code - model scrapbook

IMN logo majestic logo threadwatch logo seochat tools logo