Thread: xor linked list

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well you've got walking the list down, except for the "falling off the cliff" part (what happens when you reach the end of the list?) I have no idea why "newnode" changed to "tmp" midway through your function.

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by tabstop View Post
    Well you've got walking the list down, except for the "falling off the cliff" part (what happens when you reach the end of the list?) I have no idea why "newnode" changed to "tmp" midway through your function.
    tmp is copied from my teachers powerpoint, guess i didnt make it to changing it yet :P

  3. #18
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    let me clarify my assignment

    my teacher has provided me with a random number generator. we have to tell it to generate 100 numbers between 1 and 9999. this is done through a provided file i compile with mine.

    so upon generating these 100 number, i have to put them in order with the insert method. so only on the first instance of running the insert method will my list be empty. and i do not believe i have to deal with falling off the end because the insert method is only called exactly 100 times.

    so here is some crappy pseudo on how i think it should go

    Code:
    put random # in new node
    check if new nodes data is greater than current nodes data
    if it is, put new node between current node, and next node.
    if its not, walk the list (with the while), till you find a node with data less than the data in the new node.
    is there more to it than that?

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Only if you never ever ever ever ever insert a number will you not have to deal with the empty list.

    Only if the numbers come in reverse order will you never insert a number at the end of the list (if the first is 6, and the second is 8, guess what! That's the new end of the list!)

  5. #20
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Code:
    void insert(node **head,node **tail, int n)
    {
            node *newnode;
            cur = *head;
            newnode = malloc(sizeof(node));
            newnode->data = n;
            while((cur != NULL) && (cur->data < newnode->data))
            {
                    next = (node *)(cur->link ^ (unsigned long)prev);
                    prev = cur;
                    cur = next;
            }
            if(cur->data >= newnode->data)
            {
    
            }
            if(cur == NULL)
            {
    
            }
    
    
    }//end insert
    does that account for all possibilities?

  6. #21
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    http://imgur.com/qAy9U.jpg

    can someone explain to me how this works?

    i dont get how the prev->link and cur-link get their new numbers

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ollie88r View Post
    http://imgur.com/qAy9U.jpg

    can someone explain to me how this works?

    i dont get how the prev->link and cur-link get their new numbers
    I'm assuming you can physically see the assignment statement, which is how prev->link and cur->link get their new numbers. If you don't understand why that gives you the right numbers, you should review what ^ means, and even better, what happens when you do something like x ^ y ^ y (i.e., ^ the same number twice).

  8. #23
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by tabstop View Post
    I'm assuming you can physically see the assignment statement, which is how prev->link and cur->link get their new numbers. If you don't understand why that gives you the right numbers, you should review what ^ means, and even better, what happens when you do something like x ^ y ^ y (i.e., ^ the same number twice).
    tmp->link = (unsigned long) prv ^ (unsigned long) cur;
    prv->link ^= (unsigned long) cur ^ (unsigned long) tmp;
    cur->link ^= (unsigned long) prv ^ (unsigned long) tmp;


    temp->link = 2^3 (that i get)
    prv->link ^= 3^5

    which is

    (1^3) ^= 3^5

    so the 3's cancel out and it becomes 1^5

    cur->link ^= 2^5

    which is

    2^4 ^= 2^5


    so the 2s cancel out and it becomes 4^5

    is this correct with the image i have attached?

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ollie88r View Post
    tmp->link = (unsigned long) prv ^ (unsigned long) cur;
    prv->link ^= (unsigned long) cur ^ (unsigned long) tmp;
    cur->link ^= (unsigned long) prv ^ (unsigned long) tmp;


    temp->link = 2^3 (that i get)
    prv->link ^= 3^5

    which is

    (1^3) ^= 3^5

    so the 3's cancel out and it becomes 1^5

    cur->link ^= 2^5

    which is

    2^4 ^= 2^5


    so the 2s cancel out and it becomes 4^5

    is this correct with the image i have attached?
    That looks right.

  10. #25
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    so to get the next node


    Code:
    next = address of prev ^ link of cur

    if i wanted to get the previous node

    Code:
    prev = address of next ^ link of cur

    is this correct?

    and to find the previous of the previous node

    Code:
    prevofprev = link of prev ^ address of cur
    Last edited by ollie88r; 10-10-2009 at 11:32 PM.

  11. #26
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Gah ive been staring at this code all day

    here is what i got for my insert and main functions

    Code:
    void insert(node **head,node **tail, int n)
    {
            node *cur = *head;
            node *prev = NULL;
            node *newnode = malloc(sizeof(node));
            newnode->data = n;
    
            while((cur != NULL) && (cur->data < newnode->data))
            {
                    node *next = (node *)(cur->link ^ (unsigned long)prev);
                    prev = cur;
                    cur = next;
            }
            if(cur->data >= newnode->data)
            {
                    // if previous is null, the list is empty
                    if (prev == NULL)
                    {
                            *head = newnode;
                    }
                    // if previous is not null, find the previous node to the previous node
                    else
                    {
                            node *prevb4prev = (node *) (prev->link ^ (unsigned long) cur);
                            prev->link ^= (unsigned long) prevb4prev ^ (unsigned long) newnode;
    
                    }
            }
            if(head == NULL)
            {
                    *head = newnode;
                    (*head)->link = 0;
            }
            if(cur == NULL)
            {
                    prev->link = prev->link ^ (unsigned long)newnode;
                    newnode->link = (unsigned long)prev ^ (unsigned long)cur;
                    *tail = newnode;
            }
    
            newnode->link = (unsigned long)prev ^ (unsigned long)cur;
            node *next = (node *) ((unsigned long)prev ^ cur-> link);
            cur->link ^= (unsigned long)newnode ^ (unsigned long)next;
    
    
    }
    
    int main()
    {
            head = NULL;
            tail = head;
            float inputSeed= 0;
            int i, startRange, endRange;
    
            printf("Seed:");
            scanf("%f",&inputSeed);
            init_seed(inputSeed);
    
            printf("Range:");
            scanf("%d %d",&startRange,&endRange);
            set_range(startRange,endRange);
    
            for (i = 0; i < SIZERANGE; ++i )
            {
                    insert( (node**)head, (node**) tail, rndm() );
            }
    }
    I believe my logic is pretty sound, but who knows. I am not getting any compiling errors. But I am getting a segmentation error. Im assuming its a pointer problem. Can you help me find it?

    Thanks

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you typecasting head and tail when you pass them to your function? You also seem to have a terrible fascination with globals. You should get over that as soon as you can. What on earth are you doing XORing pointers? You can't really expect that to give you vaild memory addresses.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by quzah View Post
    What on earth are you doing XORing pointers? You can't really expect that to give you vaild memory addresses.
    Quzah, you've obviously never come across the anchient technique of an XOR doubly-linked list. The XORing of pointers for this is kinda the whole point of it, so yes it does work. I suggest you read up on it.
    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"

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've seen it before, but it's been a long time ago. I suppose if it's trying to teach you XORing it's an OK exercise. But beyond that, it's a pretty pointless exercise.

    It also defeats the purpose of a double linked list, because you cannot just start with any random node and do whatever you like to it. You can't just start in the middle of the list and find your way through it. You also can't just chop out a node whenever you please. Which again, completely defeats the purpose of using a double linked list.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    So besides the fact that it is pointless and ancient, can someone help me find the memory error?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM