逆序打印单链表
void ReversePrintList(listNode* pHead)
{
if (pHead == NULL)
return NULL;
ReversePrintList(pHead->next);
cout << pHead->next;
}
删除一个无头单链表的非尾节点
void DelNontaiNode(ListNode* pos)
{
ListNode* cur = NULL;
cur = pos->next;
pos->data = cur->data;
pos->next = cur->next;
free(cur);
}
复杂链表的复制
typedef struct Node
{
int data;
struct Node* next;
struct Node* random;
}Node,*pNode;
pNode CreateListNode(int data)
{
pNode newNode = (pNode)malloc(sizeof(Node));
if (NULL == newNode)
{
cout << "not fo memory" << endl;
exit(1);
}
newNode->data = data;
newNode->next = NULL;
newNode->random = NULL;
return newNode;
}
pNode CloneComplexList(pNode pHead)
{
if (NULL == pHead)
{
return NULL;
}
pNode cur = pHead;
pNode pNext = NULL;
pNode pNew = NULL;
while (cur) //复制节点到其后的每一个位置
{
pNew = CreateListNode(cur->data);
pNew->next = cur->next;
cur->next = pNew;
cur = pNew->next;
}
//节点赋值
cur = pHead;
pNext = NULL;
while (cur)
{
pNext = cur->next;
pNext->random = cur->random;
cur = pNext->next;
}
//拆链
pNew = pHead->next;
pNext = pNew;
cur = pNext->next;
pHead->next = cur;
while (cur)
{
pNext->next = cur->next;
pNext = pNext->next;
cur = pNext->next;
cur = cur->next;
}
return pNew;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)