Thread: xor linked list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    21

    xor linked list

    i have a delema with my xor linked list program....now i keep getting the bottom error i will also get segmentation errors throughout the insert function!!!


    i have the list commented out at all times....the only thing it will do right now is input numbers!!

    and will output the number being passed in...and then what the head is afterwards...
    this is the source code revised again:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "rndm.h"
    
    #define MAX_NUMBERS 100
    
    typedef struct _node{
            int data;
            unsigned long link;
    } node;//end node
    
    node *tmp, *head, *tail, *next, *prv, *cur;
    
    void  insert(insertNum){
            printf("This is the random number passed in %d\n", insertNum);
            node *nekw = (node *)malloc(sizeof(node));
            nekw->data = insertNum;
            printf("this is the number inputed %d\n", nekw->data );
            cur = head;
            prv = NULL;
            while(cur != NULL && cur->data < insertNum){
                    next = (node *)(cur->link ^ (unsigned long)prv);
                    prv = cur;
                    cur = next;
            }//end while loop
            if(prv == NULL){
                    head = nekw;
            }//end if statement.
            else{
                    nekw->link ^= (unsigned long) prv ^(unsigned long) cur;
                    prv->link ^= (unsigned long) cur ^ (unsigned long) nekw;
                    cur->link ^= (unsigned long) prv ^ (unsigned long) nekw;
            }//end else statement.
    }//end insert.
    
    void delete(int deleteNum){
            printf("I am in the delete function!\n");
            cur = head;
            printf("This is in cur before i start %2.1d\n", cur->data);
            prv = NULL;
            while(cur != NULL && (cur->data != deleteNum)){
                    prv =(node *)(cur->link ^ (unsigned long)next);
                    prv = cur;
                    cur = next;
            }//end while loop
            if(cur != NULL && (cur->data == deleteNum)){
                    printf("this is what was found %2.1d\n", cur->data);
                    prv->link ^= (unsigned long) prv ^ (unsigned long) cur;
                    next->link ^= (unsigned long) cur^ (unsigned long) next;
                    free(cur);
            }//end if statement.
    }//end delete.
    
     void list(int option){
            int cnt = 1;
            if(option == 1){
                    cur = head;
                    prv = NULL;
                    printf("This is the list in acending order: \n");
                    while(cnt <= MAX_NUMBERS){
                            printf("%3.1d", cur->data);
                            cnt++;
                            if(cnt % 10 == 0){
                                    printf("\n");
                            }//end if statement
                            prv =(node *)(cur->link^(unsigned long)next);
                            prv = cur;
                            cur = next;
                    }//end while loop
            }//end if
            else if(option == 2){
                    cur = tail;
                    next = NULL;
                    printf("This is the list in decending order: \n");
                    if(cnt % 10 == 0){
                            printf("\n");
                    }//end if statement.
                    while (cnt <= MAX_NUMBERS){
                            printf("%3.1d", prv->data);
                            cnt++;
                            if(cnt % 10 == 0){
                                    printf("\n");
                            }//end if statement.
                            prv = (node *)(cur->link^(unsigned long) prv);
                            next = cur;
                            cur = prv;
                    }//end while loop.
            }//end else if
    }//end list.
    
    int main(){
            head = NULL;
            tail = head;
            long inputZ;
            int num, option,cnt, beginning, end;
            int numDelete;
            printf("Please enter the seed: ");
            scanf("%l\n", &inputZ);
            init_seed(inputZ);
    
            printf("Please enter the range: ");
            scanf("%d%d\n", &beginning, &end);
            set_range(beginning, end);
    
            for(cnt=0; cnt <= MAX_NUMBERS; cnt++){
                    insert(rndm());
            }//end for loop.
    #if 0
            printf("For ascending press 1, or 2 for decending: ");
            scanf("%d\n", &num);
            list(num);
    
            numDelete = 7004;
            delete(numDelete);
    
            printf("For ascending press 1, or 2 for decending: ");
            scanf("%d\n", &num);
            list(num);
    #endif
            return 0;
    }//end main.
    here is my output:
    Code:
    > gcc linkList.c rndm.o
    >ls
    a.out*  lab1/  lab2/  linkList.c  rndm.h  rndm.o
    > a.out
    Please enter the seed: 12345
    Please enter the range: 1 9999
    This is the random number passed in 3995
    this is the number inputed 3995
    This is the random number passed in 10463
    this is the number inputed 10463
    Segmentation fault
    >
    i don't understand what is going wrong....the only time the input should really brake down is the first one inserted and if a number inputed into a list of nodes is less than the first node then i have a problem....otherwise i should just be inserting nodes that have different numbers that are less than the next one!! but it seems to break down before i even insert a number!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > node *tmp, *head, *tail, *next, *prv, *cur;
    Ewww, so many global variables with poor names

    > printf("This is the random number passed in &#37;d\n", insertNum);
    > node *nekw = (node *)malloc(sizeof(node));
    Are you really using a C compiler?
    Because
    a) mixed declarations and statements (the printf before the node)
    b) casting the result of malloc unnecessarily (see the FAQ)
    Try
    gcc -W -Wall -ansi -pedantic -O2 prog.c
    That'll show some interesting stuff.

    Oh, and you DON'T assign -> link when you add the first node.
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And whilst it may seem clever to use XOR to implement a swap, it is not a good plan really - it is almost certainly slower than a temporary variable on a modern compiler, and with a swap macro, you could do the swap itself without a cast.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    well i was thinking because i am using dynamic variables that would be wanting to use malloc??

    using the stdio.lib size of function...ohh and this is a homework assignment so i don't have much say in the matter of just not doing a xor linked list!!

    i ran this gcc -W -Wall -ansi -pedantic -O2 linkList.c

    what does it do it gave me hella errors!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by adramalech View Post
    well i was thinking because i am using dynamic variables that would be wanting to use malloc??

    using the stdio.lib size of function...ohh and this is a homework assignment so i don't have much say in the matter of just not doing a xor linked list!!

    i ran this gcc -W -Wall -ansi -pedantic -O2 linkList.c

    what does it do it gave me hella errors!
    So fix those (and ask here if you don't understand what it means).

    As to "you don't have a choice", Ok, fine - I just wanted to tell you that the "clever" trick is now so clever that the compiler/processor is getting confused and thus the code is actually worse than a straight/simple swap operation - it is not a good idea to use this in REAL CODE - and thus I doubt that it's a good idea for your teacher to teach you that sort of thing. You may get the impression that it's a good idea to do things this way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by matsp View Post
    So fix those (and ask here if you don't understand what it means).

    As to "you don't have a choice", Ok, fine - I just wanted to tell you that the "clever" trick is now so clever that the compiler/processor is getting confused and thus the code is actually worse than a straight/simple swap operation - it is not a good idea to use this in REAL CODE - and thus I doubt that it's a good idea for your teacher to teach you that sort of thing. You may get the impression that it's a good idea to do things this way.

    --
    Mats
    Matsp made a similar comment that I copied and pasted off another website (I think it was a strrev() function or something). Even though the code wasn't mine and I wasn't disputing the merits of his claims, he did go through the trouble of proving that the xor technique does suffer a performance ding.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by adramalech View Post
    using the stdio.lib size of function
    sizeof is not in stdlib.h. It's a compile-time operator.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > i ran this gcc -W -Wall -ansi -pedantic -O2 linkList.c
    > what does it do it gave me hella errors!
    And aren't you glad you found them now.
    And not several years later when you has a hell of a lot more code, and a new compiler even fussier than the one you have.
    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.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see you changing head anywhere. So that would appear to always and forever be NULL. (You set cur to nekw, but who cares?)

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    thanks...that was an eye opener it took me some time but i figured out that i forgot to set head and some of the others to what was made!!!

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    okay i got the insert function working, i got the delete function working correctly, but i am having problems with the list function...here is what my program looks like with just dumby variables as the data and not rndm()!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_NUMBERS 100
    
    typedef struct _node{
            int data;
            unsigned long link;
    } node;//end node
    
    node *head, *tail;
    
    void  insert(int insertNum){
    
            node *nekw, *tmp, *next, *prv, *cur;
            nekw = malloc(sizeof(node));
            cur = head;
            prv = NULL;
            nekw->data = insertNum;
    
            if(cur == NULL){
                    head = nekw;
                    cur = head;
                    next = tail;
            }//end if statement.
    
            else if(cur->data > nekw->data){
                    tmp = cur;
                    cur = nekw;
                    cur->link = tmp->link;
                    next = (node*)(cur->link ^ (unsigned long)prv);
                    if(next == NULL){
                            next = tmp;
                            tail = next;
                    }//end if statement
                    else{
                            tmp->link = (unsigned long)cur ^ (unsigned long)next;
                            cur->link = (unsigned long)prv ^ (unsigned long)tmp;
                            nekw = (node *)(next->link ^ (unsigned long) cur);
                            next->link = (unsigned long)nekw ^ (unsigned long)tmp;
                            cur = tmp;
                    }//end else statement
            }//end else if statement
            else{
                    while((cur != NULL) && (cur->data < nekw->data)){
                            next = (node *)(cur->link ^ (unsigned long)prv);
                            prv = cur;
                            cur = next;
                    }//end while loop
                    if(next == NULL){
                            cur = nekw;
                            tail = cur;
                    }//end if statement
                    else{
                            nekw->link = (unsigned long) cur ^(unsigned long) next;
                            cur->link = (unsigned long) nekw ^ (unsigned long) prv;
                            tmp = (node *)(next->link ^ (unsigned long)cur);
                            next->link = (unsigned long) nekw ^ (unsigned long) tmp;
                            cur = nekw;
                    }//end else
            }//end else statement
    }//end insert.
    
    void delete(int deleteNum){
            node *cur, *prv, *next;
            cur = head;
            prv = NULL;
            while(cur != NULL && (cur->data != deleteNum)){
                    prv =(node *)(cur->link ^ (unsigned long)next);
                    prv = cur;
                    cur = next;
            }//end while loop
            if(cur != NULL && (cur->data == deleteNum)){
                    prv->link = (unsigned long) prv ^ (unsigned long) cur;
                    next->link = (unsigned long) cur^ (unsigned long) next;
                    free(cur);
            }//end if statement.
    }//end delete.
    
     void list(){ //int option
            int cnt;
            node *cur, *prv, *next, *tmp;
            tmp = malloc(sizeof(node));
            cur = head; //here is where i have a breakpoint for gdb!!!
            prv = NULL;
            printf("This is the list in acending order: \n");
            while(cnt < MAX_NUMBERS){
                       printf("&#37;d", cur->data);
                       cnt++;
                       next = (node *)(cur->link ^ (unsigned long)prv);
                       prv = cur;
                       cur = next;
                       printf("  %d", cur->data);//this is where it will error because cur = NULL
                       if(cnt % 10 == 0){
                                 printf("\n");
                       }//end if statement.
            }//end while loop
    
    }//end list.
    
    int main(){
            head = NULL;
            tail = head;
            static int cnt, numDelete;
    
            for(cnt=1; cnt < MAX_NUMBERS; cnt++){
                    insert(cnt);
            }//end for loop.
    
            list();
    
            numDelete = 99;
            delete(numDelete);
    
            list();
    
            return 0;
    }//end main.
    Last edited by adramalech; 10-08-2008 at 09:34 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why does your walk the list code look different in insert and print? Insert has
    Code:
    next = (node *)(cur->link ^ (unsigned long)prv);
    prv = cur;
    cur = next;
    shouldn't that be where your trouble section is?

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    well see i am incrementing a pointer to next so i can traverse the list....those three lines are how i would traverse the list and i then output what is in cur->data until i get to the end...

    it is very simple but it seems i am screwing something small up!!!

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by adramalech View Post
    well see i am incrementing a pointer to next so i can traverse the list....those three lines are how i would traverse the list and i then output what is in cur->data until i get to the end...
    Exactly: those three lines are how you traverse the list. Since that's not what you have in your "traverse the list' section where the error is, why do you not fix it?

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    well i don't know what i should have....i can just traverse it but without printing out at every time i increment cur i won't have a list function!!!

    i thought that:

    next = (node *)(cur->link ^ (unsigned long) prv);

    was all i needed to make next pointer point to the next node!!

    and then

    prv = cur;

    is just incrementing the prv pointer to where cur was...

    then increments cur:

    cur = next;


    so what i am asking is how could i make the next pointer actually increment to the next node that is actually in the list???

    should i do this???

    next = (node *)(next->link ^ (unsigned long) cur);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 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