从尾到头打印链表
- (1)非递归打印:使用cur指针多次循环遍历,然后不断向前更新指向最后一个结点的指针last,循环结束条件是当last指向链表第一个结点
void TailToHeadPrint(pNode plist)
{
pNode cur = NULL;
pNode last = NULL;
while (plist != last)
{
cur = plist;
while (cur->next != last)
{
cur = cur->next;
}
printf("%d--->", cur->data);
last = cur;
}
printf("\n");
}
- (2)递归打印:可以在递归的最后一层打印头结点的元素——(1),也可以在统一在函数调用结束,返回的时候打印当前结点(逆序打印)——(2)
//(1)
void TailToHeadPrint_R(pNode plist)
{
if (NULL == plist->next)
{
printf("%d--->", plist->data);
return;
}
else
{
TailToHeadPrint_R(plist->next);
printf("%d--->", plist->data);
}
}
//(2)
void TailToHeadPrint_R(pNode plist)
{
if (NULL == plist)
{
return;
}
else
{
TailToHeadPrint_R(plist->next);
printf("%d--->", plist->data);
}
}
逆置/反转单链表
- (1)创建一个新的头指针,将原链表中的结点从头到尾一次摘下,依次采用头插的方式挂到新的头指针上
Node *Reverse1(pNode plist)
{
Node *first = NULL;
Node *ret = NULL;
assert(plist);
while (plist != NULL)
{
first = plist;
plist = plist->next;
//采用头插方式插入到ret上
first->next = ret;
ret = first;
}
return ret;
}
- (2)创建3个链表结点类型的指针,
p1=NULL、p2=plist、p3=plist->next
,先将p2->next
指针域中指针的指向从向后改为向前,然后将3个指针分别指向下一个结点,这里需要注意避免对空指针域的解引用
Node *Reverse2(pNode plist)
{
Node *p1 = NULL;
Node *p2 = NULL;
Node *p3 = NULL;
assert(plist);
p2 = plist;
p3 = p2->next;
p2->next = p1;
while (p3 != NULL)
{
p1 = p2;
p2 = p3;
if (p3 != NULL)
{
p3 = p3->next;
}
p2->next = p1;
}
return p2;
}
删除一个无头单链表的非尾结点(不能遍历链表)
采用替换法删除,所以这样的删除只适用于,数据和结点地址无需绑定在一起的情景,只是将结点看作是一个容器。将需要删除结点后一个结点内的内容赋给该结点,然后free掉pos->next
结点
void RemoveNoHeadNotTail(pNode pos)
{
Node *del = pos->next;
pos->data = pos->next->data;
pos->next = pos->next->next;
free(del);
}
在无头单链表的一个结点前插入一个结点(不能遍历链表)
- 先定指针域,后交换数据域
void InsertNoHead(pNode pos, DataType d)
{
DataType tmp = 0;
pNode newNode = BuyNode(d);
pNode next = pos->next;
pos->next = newNode;
newNode->next = next;
tmp = newNode->data;
newNode->data = pos->data;
pos->data = tmp;
}
- 先定数据域,后确定指针域
void InsertNoHead(pNode pos, DataType d)
{
pNode newNode = (Node *)malloc(sizeof(Node));
assert(newNode);
newNode->data = pos->data;
newNode->next = pos->next;
pos->data = d;
pos->next = newNode;
}
单链表实现约瑟夫环
(1)链表先构成环路:找到最后一个结点,让最后一个结点的指针域指向第一个结点
(2)每次找到第K个结点(走K-1步)删除,链表中结点个数为1时删除停止:cur->next = next
pNode JocephCircle(pNode* pplist, int k)
{
int count = k;
pNode prev = NULL;
//构成环
pNode cur = *pplist;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = *pplist;
//删除
cur = *pplist;
while (cur->next != cur)
{
count = k-1;//删除第K个结点,相当于走K-1步
while (count--)
{
prev = cur;
cur = cur->next;
}
prev->next = cur->next;
free(cur);
cur = prev->next;//删除完后将cur指向下一个结点
}
return cur;
}
求两个已排序单链表中的相同元素
- 如果采用循环嵌套的方式进行比较,首先此法在算法时间复杂度上没有一个循环高效;再者这种算法对于无序链表同样适用,没有利用好题目已给的有序信息
while (p1 != NULL)
{
while (p2 != NULL)
{
//
p2 = p2->next;
}
//
p1 = p1->next;
}
- 一个循环中两个链表都往后遍历,由于原链表有序,所以如果有一个链表内找到合适元素插入到目标链表后,另一条链表不需要再从头遍历,而是让两个链表都往后遍历,直到有一个链表为空,循环结束
pNode FindSameNode(pNode p1, pNode p2)
{
pNode cur1 = p1;
pNode cur2 = p2;
while (cur1 != NULL && cur2 != NULL)
{
if (cur1->data < cur2->data)
{
cur1 = cur1->next;
}
else if (cur1->data > cur2->data)
{
cur2 = cur2->next;
}
else
{
printf("%d ", cur1->data);
cur1 = cur1->next;
cur2 = cur2->next;
}
}
}
合并两个有序链表,合并后依然有序
(1)比较两个链表第一个结点元素的大小,谁小头插到返回链表当中,由于目标链表需要进行频繁的尾插,所以可以定义一个指针记录最后一个结点的位置
(2)循环结束条件是任何一个链表为空
(3)然后将不为空的链表剩余部分拼接到目标链表的尾部
pNode MergeOrderList(pNode p1, pNode p2)
{
pNode ret = NULL;
pNode last = NULL;
pNode cur1 = p1;
pNode cur2 = p2;
while (cur1 != NULL && cur2 != NULL)
{
if (cur1->data <= cur2->data)
{
if (ret == NULL)//空链表头插特殊处理
{
ret = cur1;
last = ret;
}
else
{
last->next = cur1;
last = cur1;
}
cur1 = cur1->next;
}
else
{
if (ret == NULL)
{
ret = cur2;
last = ret;
}
else
{
last->next = cur2;
last = cur2;
}
cur2 = cur2->next;
}
}
if (cur1 == NULL)
{
last->next = cur2;
}
if (cur2 == NULL)
{
last->next = cur1;
}
return ret;
}
一个循环中找到链表的中间结点
(1)借助快慢指针(快指针向后走两步,慢指针一次向后走一步)
(2)快指针先走
(3)注意避免对空指针的解引用情况,所以在快指针向后走的时候要进行判断,如果指针为空就跳出循环
pNode FindMid(pNode plist)
{
pNode fast = plist;
pNode low = plist;
while (fast != NULL)
{
if (fast->next == NULL)
{
break;
}
fast = fast->next;
if (fast->next == NULL)
{
break;
}
fast = fast->next;
low = low->next;
}
return low;
}
一个循环中找到单链表的倒数第K个结点
(1)借助先后指针(first指针先走K-1步,然后两个指针同时往后走)
(2)当first指针走到了最后一个结点,循环停止
(3)此时second指针所指的结点就是倒数第K个结点
pNode FindCountdownK(pNode plist, int k)
{
int count = k;
pNode first = plist;
pNode second = plist;
while (--count)//first走K-1步
{
first = first->next;
}
while (first->next != NULL)
{
first = first->next;
second = second->next;
}
return second;
}
删除链表的倒数第K个结点
(1)删除倒数第K个结点,先通过查找函数找到倒数第K+1个结点,然后改变这个结点的指针域,让它跳过倒数第K个结点即可
(2)如果链表中结点个数不足K个,都进行头删
void RemoveCountdownK(pNode *plist, int k)
{
int len = GetListLength(*plist);
pNode del = NULL;
pNode prev = NULL;
if (k >= len)
{
del = *plist;
*plist = (*plist)->next;
free(del);
del = NULL;
return;
}
prev = FindCountdownK(*plist,k+1);//找到倒数第K+1个结点
del = prev->next;
prev->next = del->next;
free(del);
del = NULL;
}