Thread: Been looking at this linked list i made but can't see where it goes wrong

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    20

    Been looking at this linked list i made but can't see where it goes wrong

    I wanted to create some sort of linked list pointing to the array position rather than using pointers, since i was going for only allocating memory statically.


    Code:
    int object_index()
    {
        int index;
    
    
        // Gets a unique index number
    
    
        // First it looks for a recycled number
        if (obj_recycle_size > 0) {
            obj_recycle_size--;
            index = obj_recycled[obj_recycle_size];
        }
        // Otherwise it generates a new one
        else {
            index = objIndex_size++;
        }
    
    
        // Links the new index into the chain
    
    
        // Add first
        if (objHead == UNDEFINED) {
            objHead = index;
            objHead = index;
            printf("has no head!\n");
            if (objTail == UNDEFINED) {
                printf("has no tail!\n");
                objTail = index;
            }
            object[index].prev = UNDEFINED;    // define previous node
        }
        // Add after
        else {
            object[objTail].next = index;
            object[index].prev = objTail;    // define previous node
            objTail = index;
        }
        object[index].next = UNDEFINED;        // define next node
    
    
        int i = objHead;
    
        // Debug -iterates through the current list after each added object
        while (i != UNDEFINED) {
            int prev = object[i].prev;
            int next = object[i].next;
            printf("%d->%d->%d || %d %d\n", prev, i, next, objHead, objTail);
            i = object[i].next;
            if (i == UNDEFINED)
                printf("-----\n");
        }
    
    
        return index;
    }
    However if i create 3 objects the output is the following:

    as no head!
    has no tail!
    9999->0->9999 || 0 0 ----------- so far so good.
    9999->0->1 || 0 1
    0->1->9999 || 0 1 ----------- this looks proper too
    9999->0->1 || 0 2
    9999->1->2 || 0 2 ----------- I can't understand why it would lose it's previous value here......
    1->2->9999 || 0 2

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The "add first" part could just be
    Code:
        if (objHead == UNDEFINED) {
            objHead = objTail = index;
            object[index].prev = UNDEFINED;
        }
    But it basically looks okay. You'd have to show some complete, runnable code for any more help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to figure where I went wrong on this linked list
    By sammythesp3rmy in forum C Programming
    Replies: 9
    Last Post: 11-14-2013, 05:20 PM
  2. Sorted Linked List...What am i doing wrong!
    By S15_88 in forum C Programming
    Replies: 9
    Last Post: 03-20-2008, 03:29 PM
  3. Memory Allocation for self made Linked List
    By jsimpson in forum C Programming
    Replies: 5
    Last Post: 03-10-2006, 02:14 AM
  4. wrong linked list value
    By hannibar in forum C Programming
    Replies: 3
    Last Post: 02-21-2005, 01:59 PM
  5. What's wrong with this linked-list code?
    By helplz in forum C Programming
    Replies: 4
    Last Post: 06-15-2003, 04:22 PM

Tags for this Thread