所以我正在制作一个异或链表,每当我试图为新节点分配内存时,程序就会崩溃。这是我的插入功能代码:
void add(DataType value, XORList ** xlist)
{
XORListNode * node;
node = ( XORListNode* )malloc( sizeof (XORListNode) );
printf("node memory allocated\n");
if ((*xlist)->head->prevPNext == NULL && (*xlist)->tail->prevPNext == NULL) //XOR linked list is empty: [ H - T ] + N => [ H<->T<->N ] => [ H<->A<->T ]
{
node->prevPNext = (*xlist)->tail; //new element points to tail
(*xlist)->tail->prevPNext = ( XORListNode * )( (uintptr_t)(*xlist)->head ^ (uintptr_t)(*xlist)->tail ); //tail points to head and new value
(*xlist)->head->prevPNext = (*xlist)->tail;
(*xlist)->tail->value = value;
(*xlist)->tail = node;
}
else //Otherwise: [ H<->A<->B<-...->X<->T ] + N => [ H<->A<->B<-...->X<->T<->N ] => [ H<->A<->B<-...->X<->Y<->T ]
{
node->prevPNext = (*xlist)->tail;
(*xlist)->tail->prevPNext = ( XORListNode * )( (uintptr_t)(*xlist)->tail->prevPNext ^ (uintptr_t)node );
(*xlist)->tail->value = value;
(*xlist)->tail = node;
}
}
这是 XORList 的定义:
typedef struct XORList
{
XORListNode * head, * tail;
} XORList;
这是 XORListNode 的定义:
typedef struct XORListNode
{
DataType value;
struct XORListNode * prevPNext;
} XORListNode;
我也很感激有关代码的任何其他评论,因为我还没有使用指针的经验。
感谢您的帮助。