赞
踩
链表的定义:
- typedef int DataType;
-
- typedef struct ListNode
- {
- Datatype data;
- struct ListNode* next;
- }ListNode;
递归实现链表的逆序打印
- void Reverse(ListNode *pList)//递归实现逆序打印
- {
- if(pList == NUll)
- {
- return;
- }
- else if(pList != NULL)
- {
- Reverse(pList->next);
- printf("%d-->",pList->data);
- }
- }
删除无头结点的非尾节点
- void RemoveNotHead(ListNode **ppList,Datatype x)//删除无头链表的非尾节点
- {
- ListNode *del = NULL;
- ListNode *cul = Find(*ppList,x);
- assert(ppList);
- if(cur==NULL)
- {
- return
- }
- del = cur->next;
- cur->data = del->data;
- cur->next = del->next;
- free(del);
- }
在无头单链表的一个节点前插入一个节点
- void InsertNonFront(ListNode *pos,DataType x)//在无头单链表的一个节点前
- 插入一个节点
- {
- ListNode *cur = pos->next;
- ListNode *tmp = BuyNode(x);
- DataType _tmp;
- assert(pos);
- pos->next = tmp;
- tmp->next = cur;
- _tmp = pos->data;
- pos->next = tmp->data;
- tmp->data = -tmp;
-
- }
逆置单链表
- ListNode *_Reverse(ListNode *pList)//逆转链表
- {
- ListNode *NewList = NUll;
- ListNode *cur = pList;
- while(cur)
- {
- //1.摘节点
- ListNode *tmp = cur;
- cur = cur->next;
- //2.头插
- tmp->next = NewList;
- NewList = tmp;
- }
- return NewList;
- }
约瑟夫环
- ListNode *JosephRing(ListNode *list,int k)
- {
- ListNode *cur = list;
- int count = k;
- ListNode *Next = cur->next;
- if(list == NULL)
- {
- return;
- }
- while(cur->next!=cur)
- {
- while(--count)
- {
- cur = cur->next;
- }
- cur->data = Next->data;
- cur->next = Next->next;
- free(next);
- }
- return cur;
- }
链表的冒泡排序
- void BubbleSortList(ListNode *pList)//对链表进行冒泡排序
- {
- ListNode *cur = plist;
- ListNode *tail = NUll;
- if((cur == NUll)||(cur->next == NUll))
- {
- return;
- }
- while(cur->next!=tail)//总趟数
- {
- while(cur->next!=tail)
- {
- if((cur->data) > (cur->next->data))
- {
- DataType tmp = cur->data;
- cur->data = cur->next->data;
- cur->next->data = tmp;
- }
- cur = cur->next;
- }
- tail = cur;
- cur = pList;
- }
- }
找中间节点
- ListNode *FindMidNode(ListNode *pList)//找中间节点
- {
- ListNode *fast = pList;
- ListNode *slow = pList;
- if(pList == NULL)
- {
- return;
- }
- while(fast!=NULL&&fast->next!=NUll)
- {
- fast = fast->next->next;//双指针,快指针走两步,慢指针走一步
- slow = slow->next;
- }
- return slow;
- }
有序合并
- ListNode *Merge(ListNode *ppl1,ListNode *ppl2)//有序链表合并
- {
- ListNode *head = NULL;
- ListNode *cur = NULL;
- assert(ppl2!=NULL);
- assert(ppl1!=NULL);
-
- if((*ppl1)->data<(*ppl2)->data)
- {
- head = *ppl1;
- cur = head;
- *ppl1 = (*ppl1)->next;
- }else
- {
- head = *ppl2;
- cur = head;
- *ppl2 = (*ppl2)->next;
- }
- while((*ppl11=NULL)&&(*ppl2!=NULL))
- {
- if((*ppl1)->data<(*ppl2)->data)
- {
- cur->next = *ppl1;
- *ppl1 = (*ppl1)->next;
- }
- else
- {
- cur->next = *ppl2;
- *ppl2 = (*ppl2)->next;
- }
- cur = cur->next;
- }
- if(*ppl1!=NULL)
- {
- cur->next = *ppl1;
- }
- else if(*ppl2!=NULL)
- {
- cur->next = *ppl2;
- }
- return head;
- }
判断链表是否有环
- ListNode *IsCycle(ListNode *pList)//判断是否带环
- {
- ListNode *slow = pList;
- ListNode *fast = pList;
- while(fast&&fast->next)
- {
- slow = slow->next;
- fast = fast->next->next;//快指针走两步,慢指针走一步
- if(fast==slow) return fast;//若存在环则两指针会相遇
- }
- return NULL;
- }
求环的长度
- int GetCycleLength(ListNode *Meet)//求环长度
- {
- ListNode *meet = NULL;
- int count = 0;
- if(Meet == NULL)
- {
- return count;
- }
- meet = IsCycle(Meet);
- Meet = meet;
- meet = meet->next;
- count++;
- while(meet != Meet)//从相遇节点开始,知道下次相遇即为环的长度
- {
- count++;
- meet = meet->next;
- }
- return count;
- }
求环的入口
定义两个指针,一个指针从链表开始出发,另一个指针从相遇点出发,两个指针再次相遇的点就是入口点
- ListNode *GetCircleEntryNode(ListNode *meet,ListNode *plist)//求环的入口点
- {
- ListNode *first = meet;
- ListNode *second = plist;
- if((plist == NULL)||(meet == NULL))//一个指针从链表开始,另一个从相遇点出发,两个指针再次相遇的点就是入口点
- return NULL;
-
- while(first != second)
- {
- first = first->next;
- second = second->next;
- }
- return first;
- }
判断两个环是否相交
先比较两个链表,找出长链表,定义两个指针,先让一个指针从长链表走相差步,另一个指针再从短链表走,当两个链表同时指向同一个节点,带节点为相交点
- ListNode *GetCrossNode(ListNode *l1,ListNode *l2)//判断两环是否相交
- {
- int len1 = 0;
- int len2 = 0;
- ListNode *cur1 = l1;
- ListNode *cur2 = l2;
- ListNode *shortlist,*longlist;
- if(cur == NULL||cur1 == NULL)
- return NULL;
- while(cur1)
- {
- len1++;
- cur1 = cur1->next;
- }
- while(cur2)
- {
- len2++;
- cur2 = cur2->next;
- }
- shortlist = l2;
- longlist = l1;
- if(len2 > len1)
- {
- shortlist = l1;
- longlist = l2;
- }
-
- int gap = abs(len1-len2);
- while(gap--)
- {
- longlist = longlist->next;
- }
- while(shortlist != longlist)
- {
- shortlist = shortlist->next;
- longlist = longlist->next;
- }
- }
删除倒数第K个数
- void DelLinkList(ListNode *pList, int k)//删除倒数第K个节点
- {
- ListNode *fast = pList;
- ListNode *slow = pList;
- ListNode *Del = NULL;
- if(pList == NULL)return;
- while((fast != NULL)&&(fast->next != NULL))
- {
- while(--k>0)
- {
- Del = fast;//fast先走K步之后slow走,直达fast走到链表结尾
- fast = fast->next;
- }
- fast = fast->next;
- slow = slow->next;
- }
- Del = slow->next;
- slow->data = Del->data;
- slow->next = Del->next;
- free(Del);
- Del = NULL;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。