欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

带头双向循环链表增删操作以及 相关习题

程序员文章站 2022-03-24 17:52:49
...

带头双向链表增删操作:https://github.com/uniquefairty/Data-Structure-/tree/master/List.c
1,将两个有序链表合并为一个新的有序链表并返回,新链表是通过拼接给定的两个链表的所有结点组成的。
思路:定义两个指针分别指向两个链表,比较结点中的数据,进行前插操作。
如果指向list2的指针先走到尽头,则证明已经合并。
如果指向list的指针先走到尽头,则需要把list2剩下的一段结点都插在list中。

ListNode* mergeTwoLists(List *plist, List* plist2)
{
 ListNode *cur1=plist->_head->_next;
 ListNode *cur2=plist2->_head->_next;
 ListNode *tmp;
 while (cur1!=plist->_head  &&cur2!=plist->_head )//双链表遍历的跳出条件
 {
  if (cur1->_data < cur2->_data)
  {
   cur1 = cur1->_next;
  }
  else
  {
   tmp = cur2;//要把cur2插进list中,保护在list2中遍历的指针
   cur2 = cur2->_next;//指向下次需要比较的节点
  
   cur1->_prev->_next = tmp;
   tmp->_prev = cur1->_prev;
   tmp->_next = cur1;
   cur1->_prev = tmp;
  }
 }
 //如果遍历结束后,list到末尾了,证明list2还没结束(两个肯定不会同时到末尾),而且list2剩下的节点都比list大,所以要把list2剩下的节点插进list的后面
 if (cur1 == plist->_head)
 {
  cur1->_prev ->_next = cur2;
  cur2->_prev = cur1->_prev;
  
  cur1->_prev = plist2->_head->_prev;
  plist2->_head->_prev->_next = cur1;
 }
 free(plist2->_head);
}

2.在一个排序的链表中,存在重复的结点,请删除该链表重复的结点,重复的结点不保留,返回链头指针。

void deleteDuplication(List* plist)
{
 ListNode *cur;
 for (cur = plist->_head->_next; cur != plist->_head->_prev;)
 {
  if (cur->_data == cur->_next->_data)
  {
   ListErase(cur->_next);//不能删掉cur,若删掉cur,下次循环就找不到cur的值,或者可以执行删除下一个节点操作
  }
  else
  {
   cur = cur->_next;
  }
 }
}

带头双向链表增删操作:https://github.com/uniquefairty/Data-Structure-/tree/master/List.c