0

i need to implement insert after key in a xor linked list but i have a problem and i can't find any solution the insert doesn't work well but insert first works fine.

Example:

list=1 2 3 4 5

insertafk(list,2,99)

result=1,2,99, and some random values not 3,4,5,the insert works but i dont have the proper elements after the new key, i have random elements

xorlist.h

typedef struct nodetype4
{
    int key;
    struct nodetype4* np;
}XorNode;

typedef struct
{
    XorNode* first;
    XorNode* last;
}xlist;

XorNode* XOR(XorNode* a, XorNode* b);
void initXor(xlist* x);
void insertf(xlist* x,int k);
void afisxor(xlist* x);
void insertafk(xlist* x,int akey,int k);

xorlist.c

 #include <stdio.h>
 #include <stdlib.h>
 #include "xorlist.h"

XorNode* XOR(XorNode* a, XorNode* b)
{
    return (XorNode*)((unsigned int)a ^ (unsigned int)b);
}
void initXor(xlist* x)
{
    x->first=NULL;
    x->last=NULL;
}
void insertf(xlist* x,int k)
{
    XorNode* p=malloc(sizeof(XorNode));
    XorNode* fnext;
    p->key=k;
    if(x->first==NULL)
    {
        p->np=NULL;
        x->first=p;
        x->last=p;
    }
    else
    {
        p->np=XOR(x->first,NULL);
        fnext=XOR(x->first->np,NULL);
        (x->first)->np=XOR(p,fnext);
        x->first=p;
    }

}

void insertafk(xlist* x,int akey,int k)
{
    XorNode* p=malloc(sizeof(XorNode));
    p->key=k;

    XorNode* curr;
    curr=x->first;
    XorNode* prev=NULL;
    XorNode* next=XOR(NULL,curr->np);

    //Cautam nodul cu cheia akey
    while(curr!=NULL)
    {
        if(curr->key==akey)
            break;
        prev=curr;
        curr=next;
        next=XOR(curr->np,prev);

    }

    //Inserare cheie dupa nodul gasit
    if(curr==NULL)
        printf("Nu s-a gasit cheia");
    else
    {
        p->np=XOR(curr,next);
        curr->np=XOR(prev,p);
        next->np=XOR(p,curr);

    }
}
//print list
void afisxor(xlist* x)
{
    XorNode* curr;
    XorNode* prev;
    XorNode* next;
    curr=x->first;
    prev=NULL;
    while(curr!=NULL)
    {
        printf("%d ",curr->key);
        next=XOR(curr->np,prev);
        prev=curr;
        curr=next;
    }
    printf("\n");
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "xorlist.h"

int main()
{

    printf("For xor list:\n");
    xlist* x;
    initXor(&x);
    insertf(&x,1);
    insertf(&x,2);
    insertf(&x,3);
    insertl(&x,10);
    insertl(&x,13);
    afisxor(&x);
    insertafk(&x,2,20);
    afisxor(&x);

    return 0;
}

the result after insert

  • @Jean-FrançoisFabre https://stackoverflow.com/questions/16138998/how-exactly-does-a-xor-linked-list-work – Craig Estey Mar 27 '18 at 19:34
  • okay... learning something tonight. Well, it's still a very vague question. – Jean-François Fabre Mar 27 '18 at 19:39
  • 1
    Your compiler should be generating a ton of warnings when you compile this code. What do those warnings say? – user3386109 Mar 27 '18 at 20:17
  • The only warning is expected 'struct xlist ' but argument is of type 'struct xlist *' – Sîrb Sebastian Mar 27 '18 at 20:36
  • 1
    Yup, and you need to fix that before your code even has a chance of working. The other place you should get a warning is the `XOR` function. There's no guarantee that `unsigned int` is the same size as a pointer, and the `XOR` function won't work correctly unless they are. You can use `uintptr_t` from ``, or the code should verify that `sizeof(unsigned int) == sizeof(xlist *)` at the beginning. That's one of the reasons **not** to use xor lists. You're doing stuff with pointers that you really shouldn't. – user3386109 Mar 27 '18 at 20:49
  • yes, i know, xor list is very complicated and others, but i have a project for school and i need to implements this,but other things like insert first and last work – Sîrb Sebastian Mar 27 '18 at 20:58

0 Answers0