0xC0000005: 读取位置 0x00000030 时发生访问冲突,双向链表中插入将节点插入尾部这样写有错吗?
void insertclosed(Node *closed,Node *node)//将节点插入到双向链表closed的尾部
{
if((closed->next==closed)&&(closed->pre==closed))
{
closed->next=node;
node->pre=closed;
}
else
{
Node *q=(Node *)malloc(sizeof(Node));
q=closed;
while(q->next!=closed)
{
q=q->next;
}
node->pre=q;
node->next=closed;
q->next=node;
closed->pre=node;
}
}
void insertclosed(Node *closed,Node *node)//将节点插入到双向链表closed的尾部
{
ASSERT(closed && node);
if(!closed)return; //由于头节点,不可改变,所以空的链表,无法插入数据。
if(!node)return; //插入空节点,不用做任何事情。
Node *rear = closed;
while( rear->next ){//尾节点的next必为NULL
rear = rear->next;
}
rear->next = node; //尾节点rear的next ,置为新的尾节点 node
node->pre = rear; //新的尾节点 node的pre,置为原尾节点 rear
node->next = NULL; //新的尾节点node的next, 置为 NULL;
}
void mergeclosed(Node *closed,Node *node)
{
Node* p,*q;//closed尾节点p,node尾节点q
ASSERT(closed && node);
if(!closed)return; //由于头节点,不可改变,所以空的链表,无法插入数据。
if(!node)return;
p = closed; //closed尾节点
while( p->next ){//尾节点的next必为NULL
p = p->next;
}
q= node;//node尾节点
while( q->next ){//尾节点的next必为NULL
q = q->next;
}
node->pre = p; // node的头节点的pre ,指向closed尾节点p
p->next = node; // closed尾节点 p的 next ,指向node 的头节点node
}
void insertclosed(Node *closed,Node *node)//将节点插入到双向链表closed的尾部
{
ASSERT(closed && node);
if(!closed)return; //由于头节点,不可改变,所以空的链表,无法插入数据。
if(!node)return;
node->pre = closed->pre; // 新的尾节点的pre ,指向原尾节点
node->next = closed; // 新的尾节点的next,指向头节点
closed->pre->next = node; // 尾节点的 next ,指向新的尾节点 node
closed->pre = node; // 头节点的pre,指向新的尾节点的node
}
void mergeclosed(Node *closed,Node *node)
{
Node* p,*q;
ASSERT(closed && node);
if(!closed)return; //由于头节点,不可改变,所以空的链表,无法插入数据。
if(!node)return;
p = closed->pre; //closed尾节点
q = node ->pre; //node尾节点
node->pre = p; // 新的尾节点的pre ,指向closed尾节点p
p->next = node; // closed尾节点的 next ,指向node 的头节点node
q->next = closed; // node 的的尾节点q的next,指向closed的头节点closed
closed->pre = q; // closed头节点的pre,指向node的尾节点的q
}