c语言无头节点单链表,假设有一个循环链表的长度大于1,且表中既无头结点也无头指针。已知S为指向链表某个结点的指针(C语言)...

假设有一个循环链表的长度大于1,且表中既无头结点也无头指针。已知S为指向链表某个结点的指针,试编写算法在链表中删除指针S所指结点的前驱结点。

#include

#include

typedef struct _DLNode

{

struct _DLNode *next;

int value;

} DLNode;

/*

* 1. 如果链有没有节点,就返回NULL

* 2. 如果链表只有一个节点,输入节点的前驱节点就是它本身,则返回输入节点 * 3. 如果链表有多于一个节点,就返回输入节点的前驱节点

*/

DLNode* getPriorNode(DLNode *node)

{

DLNode *next;

if (!node)

{

return NULL;

}

next = node->next;

while (node != next->next)

{

next = next->next;

}

return next;

}

void delPriorNode(DLNode *node)

{

DLNode *prior = getPriorNode(node);

if (prior)

{

getPriorNode(prior)->next = prior->next;

/* free(prior); */ /* 只有节点是malloc的才能free */

}

}

void printList(DLNode *node)

{

DLNode *next;

if (!node)

{

return;

}