#pragma once
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<stdlib.h>
typedef int DataType;
typedef struct Node
{
DataType _data;// 數據
struct Node* _next;// 指向下一個節點的指針
}Node, *PLinkList;
//typedef struct Node Node;
//typedef struct Node* PLinkList;
/////////////////////////////////////////////////////////////////////////////////
// 單鏈表的基本操作
// 初始化/銷燬/打印單鏈表
void InitSList(PLinkList* ppList);
void DestorySList(PLinkList* ppList);
void PrintSList(PLinkList pList);
int GetListLength(PLinkList pList);
// 尾插/尾刪/頭插/頭刪
void PushBack(PLinkList* ppList, DataType x);
void PopBack(PLinkList* ppList);
void PushFront(PLinkList* ppList, DataType x);
void PopFront(PLinkList* ppList);
// 查找/刪除/插入
Node* Find(PLinkList pList, DataType x);
int Remove(PLinkList* ppList, DataType x);
void Erase(PLinkList* ppList, Node* n);
void Insert(PLinkList* ppList, Node* n, DataType x);
void InitSList(PLinkList* ppList)
{
assert(ppList);
*ppList = 0;
}
void DestorySList(PLinkList* ppList)
{
Node* begin = *ppList;
assert(ppList);
while (begin)
{
Node* tmp = begin;
begin = begin->_next;
free(tmp);
}
*ppList = 0;
}
void PrintSList(PLinkList pList)
{
Node* begin = pList;
printf("PLinkList:");
while (begin)
{
printf("->%d", begin->_data);
begin = begin->_next;
}
printf("->NULL\n");
}
int GetListLength(PLinkList ppList)
{
int length = 0;
Node* begin = ppList;
while (begin)
{
length++;
begin = begin->_next;
}
return length;
}
Node* CreateNode(DataType x)
{
Node* n = (Node*)malloc(sizeof(Node));
n->_data = x;
n->_next = 0;
return n;
}
void PushBack(PLinkList* ppList, DataType x)
{
Node* n = 0;
assert(ppList);
n = CreateNode(x);
// 沒有節點/一個以上
if (*ppList == NULL)
{
*ppList = n;
}
else
{
Node* begin = *ppList;
while (begin->_next)
{
begin = begin->_next;
}
begin->_next = n;
}
}
void PopBack(PLinkList* ppList)
{
Node* begin = 0;
assert(ppList);
// 1.沒有節點
if (*ppList == 0)
{
return;
}
// 2.一個節點
if ((*ppList)->_next == 0)
{
free(*ppList);
*ppList = 0;
return;
}
// 3.有兩個以上的節點
begin = *ppList;
while (begin->_next->_next != NULL)
{
begin = begin->_next;
}
free(begin->_next);
begin->_next = 0;
}
void PushFront(PLinkList* ppList, DataType x)
{
Node* n = 0;
assert(ppList);
n = CreateNode(x);
// 1:沒有節點 or 一個以上
if (*ppList == NULL)
{
*ppList = n;
}
else
{
n->_next = *ppList;
*ppList = n;
}
}
void PopFront(PLinkList* ppList)
{
Node* n = 0;
assert(ppList);
// 沒有節點
if (*ppList == NULL)
{
return;
}
// 一個節點
if ((*ppList)->_next == NULL)
{
free(*ppList);
*ppList = NULL;
return;
}
// 兩個節點以上
n = *ppList;
*ppList = n->_next;
free(n);
}
Node* Find(PLinkList ppList, DataType x)
{
Node* begin = 0;
assert(ppList);
begin = ppList;
while (begin)
{
if (begin->_data == x)
{
return begin;
}
begin = begin->_next;
}
return 0;
}
int Remove(PLinkList* ppList, DataType x)
{
Node* prev = 0;
Node* begin = 0;
Node* del = 0;
assert(ppList);
begin = *ppList;
while (begin)
{
if (begin->_data == x)
{
Node* del = begin;
// 頭節點 or 中間節點
if (*ppList == begin)
{
*ppList = (*ppList)->_next;
}
else
{
prev->_next = begin->_next;
}
free(del);
return 0;
}
prev = begin;
begin = begin->_next;
}
return -1;
}
void Insert(PLinkList* ppList, Node* n, DataType x)
{
Node* tmp = 0;
assert(ppList);
tmp = CreateNode(x);
// 沒有節點 / 一個以上節點
if (*ppList == NULL)
{
*ppList = tmp;
}
else
{
assert(n);
tmp->_next = n->_next;
n->_next = tmp;
}
}
void Erase(PLinkList* ppList, Node* n)
{
Node* del = 0;
assert(ppList);
assert(n);
// 處理尾節點的情況
if (n->_next == 0)
{
PopBack(ppList);
return;
}
// 將n的下一個next節點的值賦值給n, 刪除n的next節點。
n->_data = n->_next->_data;
del = n->_next;
n->_next = n->_next->_next;
free(del);
}
// 逆置/排序(冒泡)/合併
void Reverse(PLinkList* ppList);// 逆置
void DelNonTailNode(Node* n);// 刪除單鏈表的一個非尾節點
void InsertFrontNode(Node* n, DataType x);// 在當前節點前插入一個數據x
Node* JosephCycle(PLinkList pList, int m);約瑟夫環(約瑟夫問題)
void BubbleSort(PLinkList pList, Node* end);// 排序-(冒泡升序排序)
Node* Partion(Node* left, Node* right, int& leftCnt,int& rightCnt)//使用前後指針版的快速排序
void QucikSort_OP(PLinkList left, PLinkList right)//排序優化->快速排序
PLinkList Merge(PLinkList l1, PLinkList l2);// 合併兩個有序鏈表,合併後的鏈表依然有序
PLinkList MergeRecursive(PLinkList l1, PLinkList l2);// 遞歸實現鏈表合併
【快慢指針問題】
PLinkList FindMidNode(PLinkList pList)// 查找單鏈表的中間節點
void DelKNode(PLinkList pList,int k);刪除單鏈表的倒數第k個節點(k > 1 && k < 鏈表的總長度);
// 【鏈表帶環問題】
int CheckCycle(PLinkList pList, PLinkList* meetNode)// 判斷鏈表是否帶環, 若鏈表帶環則求環的長度和相遇節點,不帶環返回-1
// 刪除單鏈表的一個非尾節點
void DelNonTailNode(Node* n)
{
Node* del = 0;
// 斷言是否是尾節點
assert(n->_next);
// 將n的下一個next節點的值賦值給n, 刪除n的next節點。
n->_data = n->_next->_data;
del = n->_next;
n->_next = n->_next->_next;
free(del);
}
// 在當前節點前插入一個數據x
void InsertFrontNode(Node* n, DataType x)
{
DataType tmpData;
Node* tmpNode = CreateNode(x);
assert(n);
// 數據插到當前節點後
tmpNode->_next = n->_next;
n->_next = tmpNode;
// 交換數據
tmpData = n->_data;
n->_data = tmpNode->_data;
tmpNode->_data = tmpData;
}
//
//約瑟夫環(約瑟夫問題)是一個數學的應用問題:已知n個人(以編號1,2,3...n分別表示)圍坐在一張//圓桌周圍。從編號爲k的人開始報數,數到m的那個人出列;他的下一個人又從1開始報數,數到m的那個人//又出列;依此規律重複下去,直到圓桌周圍的人全部出列。
//
Node* JosephCycle(PLinkList pList, int m)
{
Node* begin = pList;
assert(pList);
while (1)
{
Node* del = 0;
int i =0;
// 判斷只有一個節點時退出
if (begin->_next == begin)
{
break;
}
// 跳m-1步
for (; i < m - 1; ++i)
{
begin = begin->_next;
}
printf("%d", begin->_data);
// 替換刪除法進行刪除
del = begin->_next;
begin->_data = begin->_next->_data;
begin->_next = begin->_next->_next;
free(del);
}
return begin;
}
// 逆置
void Reverse(PLinkList* ppList)
{
Node* head = 0;
Node* next = 0;
assert(ppList);
// 無節點或只有一個節點則直接返回
if (*ppList ==
NULL || (*ppList)->_next ==NULL)
{
return;
}
// 取出next節點
next = (*ppList)->_next;
// 取出頭結點並置空尾指針
head = *ppList;
head->_next = 0;
// 從第二個節點開始進行頭插。
while (next)
{
Node* tmp = next;
next = next->_next;
tmp->_next = head;
head = tmp;
}
// 更新頭結點指針
*ppList = head;
}
// 排序-(冒泡升序排序)
void BubbleSort(PLinkList pList, Node* end)
{
// 優化標記
int exchange =0;
Node* prev = 0;
Node* next = 0;
Node* tail = 0;
// 沒有節點 or 一個節點則直接返回
if (pList == end || pList->_next == end)
{
return;
}
// tail做尾標記
tail = end;
while (tail != pList)
{
// 將數據向後冒
prev = pList;
next = pList->_next;
while (next != tail)
{
if (prev->_data > next->_data)
{
DataType tmp = prev->_data;
prev->_data = next->_data;
next->_data = tmp;
exchange = 1;
}
prev = next;
next = next->_next;
}
if (exchange ==0)
{
return;
}
// 更新尾標記
tail = prev;
}
}
//
// 使用前後指針版的快速排序
//
Node* Partion(Node* left, Node* right, int& leftCnt,int& rightCnt)
{
Node* key = left;
Node* prev = left, *next = left->_next;
leftCnt = 0, rightCnt =0;
int totalCnt =0;
while (next != right)
{
if (next->_data <= key->_data)
{
prev = prev->_next;
if (prev != next)
{
swap(prev->_data, next->_data);
}
leftCnt++;
}
next = next->_next;
totalCnt++;
}
rightCnt = totalCnt - leftCnt;
if (prev != key)
{
swap(prev->_data, key->_data);
}
return prev;
}
// 排序優化->快速排序
void QucikSort_OP(PLinkList left, PLinkList right)
{
int leftCnt, rightCnt;
// 一個節點或者一個
if ((left != right)&& (left && left->_next != right))
{
Node* key = Partion(left, right, leftCnt, rightCnt);
if (leftCnt <13)
{
BubbleSort(left, key);
}
else
{
QucikSort_OP(left, key);
}
if (rightCnt <13)
{
BubbleSort(key->_next, right);
}
else
{
QucikSort_OP(key->_next, right);
}
}
}
// 合併兩個有序鏈表,合併後的鏈表依然有序
PLinkList Merge(PLinkList pList1, PLinkList pList2)
{
PLinkList mergePList;
Node* tail = 0;
Node* begin1 = 0;
Node* begin2 = 0;
// 若兩個鏈表相同,則直接返回
if (pList1 == pList2)
{
return pList1;
}
// 若其中一個鏈表爲空,則返回另一個鏈表
if (pList1 ==NULL)
{
return pList2;
}
if (pList2 ==NULL)
{
return pList2;
}
// 取出數據小的節點爲新鏈表的頭結點。
begin1 = pList1;
begin2 = pList2;
if (begin1->_data < begin2->_data)
{
mergePList = begin1;
begin1 = begin1->_next;
}
else
{
mergePList = begin2;
begin2 = begin2->_next;
}
// 標記尾節點,方便歸併的數據尾插。
tail = mergePList;
while (begin1 && begin2)
{
if (begin1->_data < begin2->_data)
{
tail->_next = begin1;
begin1 = begin1->_next;
tail = tail->_next;
}
else
{
tail->_next = begin2;
begin2 = begin2->_next;
tail = tail->_next;
}
}
// 鏈接剩餘尾鏈表
if (begin1)
{
tail->_next = begin1;
}
elseif (begin2)
{
tail->_next = begin2;
}
return mergePList;
}
// 遞歸實現鏈表合併
PLinkList MergeRecursive(PLinkList pList1, PLinkList pList2)
{
PLinkList mergePList;
// 若兩個鏈表相同,則直接返回
if (pList1 == pList2)
{
return pList1;
}
// 若其中一個鏈表爲空,則返回另一個鏈表
if (pList1 ==NULL)
{
return pList2;
}
if (pList2 ==NULL)
{
return pList2;
}
if (pList1->_data < pList2->_data)
{
mergePList = pList1;
mergePList->_next = MergeRecursive(pList1->_next, pList2);
}
else
{
mergePList = pList2;
mergePList->_next = MergeRecursive(pList1, pList2->_next);
}
return mergePList;
}
// 【快慢指針問題】
// 查找單鏈表的中間節點
PLinkList FindMidNode(PLinkList pList)
{
// 快慢指針,快指針一次走兩步,慢指針一次走一步。
PLinkList slow, fast;
if (pList ==NULL)
{
return pList;
}
slow = pList;
fast = pList;
while (fast && fast->_next)
{
slow = slow->_next;
fast = fast->_next->_next;
}
//
// fast 爲空則鏈表長度爲偶數,slow爲中間節點。
// fast->next爲空則鏈表長度爲奇數,slow爲中間節點。
//
return slow;
}
// 刪除單鏈表的倒數第k個節點(k > 1 && k < 鏈表的總長度);
// 時間複雜度O(N)
void DelKNode(PLinkList pList,int k)
{
PLinkList slow, fast;
if (pList ==NULL)
{
return;
}
slow = pList;
fast = pList;
assert(k >1);
// 快指針走k步以後,慢指針纔開始移動
while (fast)
{
if (--k <0)
{
slow = slow->_next;
}
fast = fast->_next;
}
if (k <0)
{
Node* del = slow->_next;
slow->_data = slow->_next->_data;
slow->_next = slow->_next->_next;
free(del);
}
}
// 【鏈表帶環問題】
// 判斷鏈表是否帶環, 若鏈表帶環則求環的長度和相遇節點,不帶環返回-1
int CheckCycle(PLinkList pList, PLinkList* meetNode)
{
PLinkList slow, fast;
if (pList ==NULL)
{
return -1;
}
slow = pList;
fast = pList;
//
// 使用快慢指針,當快指針追上慢指針,則表示有環,否則表示無環。
//
while (fast && fast->_next)
{
slow = slow->_next;
fast = fast->_next->_next;
if (slow == fast)
{
// 慢指針再跑一圈,計算環的長度。
int cycleLength =0;
*meetNode = fast;
do{
slow = slow->_next;
++cycleLength;
} while (slow != fast);
return cycleLength;
}
}
// 鏈表不帶環則返回-1
return -1;
}
// 獲取環入口點
Node* GetCycleEntryNode(PLinkList pList, PLinkList meetNode)
{
int length1 =0, length2 = 0, interval = 0;
Node* begin1 = pList, *begin2 = meetNode->_next;
assert(pList);
assert(meetNode);
//
// 1:模擬從相遇節點斷開環,則轉換爲兩單鏈表相交,求交點的問題。
//
while (begin1 != meetNode)
{
begin1 = begin1->_next;
++length1;
}
while (begin2 != meetNode)
{
begin2 = begin2->_next;
++length2;
}
// 2:先計算兩個鏈表的長度,長的鏈表先走interval(兩鏈表長度的差值)步。
begin1 = pList, begin2 = meetNode->_next;
// 重置鏈表起點
if (length1 > length2)
{
interval = length1 - length2;
while (interval--)
{
begin1 = begin1->_next;
}
}
elseif (length1 < length2)
{
interval = length2 - length1;
while (interval--)
{
begin2 = begin2->_next;
}
}
// 3:則第一次相遇的節點爲環的入口點。
while (begin1 != begin2)
{
begin1 = begin1->_next;
begin2 = begin2->_next;
}
return begin1;
}
// 【鏈表相交問題】
//
// 判斷兩個鏈表是否相交,假設兩個鏈表都不帶環。
// 求環的交點,長鏈表先走n步(n爲兩鏈表的長度差),然後再一起走,第一個相遇點則爲交點。
//
int CheckCross(PLinkList pList1, PLinkList pList2)
{
Node* end1 = 0, *end2 =0;
assert(pList1 && pList2);
while (pList1)
{
end1 = pList1;
pList1 = pList1->_next;
}
while (pList2)
{
end2 = pList2;
pList2 = pList2->_next;
}
if (end1 == end2)
{
return 1;
}
return0;
}
//
// 複雜鏈表的複製。
// 一個鏈表的每個節點,有一個指向next指針指向下一個節點,還有一個random指針指向這
// 個鏈表中的一個隨機節點或者NULL,現在要求實現複製這個鏈表,返回複製後的新鏈表。
//
typedef struct ComplexNode
{
DataType _data; // 數據
struct ComplexNode* _next;// 指向下一個節點的指針
struct ComplexNode* _random;// 指向隨機節點
}ComplexNode;
ComplexNode* CreateComplexNode(DataType x)
{
ComplexNode* tmp = new ComplexNode;
tmp->_data = x;
tmp->_next = NULL;
tmp->_random = NULL;
return tmp;
}
void CreateComplexNode(ComplexNode*& head)
{
ComplexNode* n1 = CreateComplexNode(1);
ComplexNode* n2 = CreateComplexNode(2);
ComplexNode* n3 = CreateComplexNode(3);
ComplexNode* n4 = CreateComplexNode(4);
n1->_next = n2;
n2->_next = n3;
n3->_next = n4;
n4->_next = NULL;
n1->_random = n4;
n2->_random = n3;
n3->_random = n2;
n4->_random = n1;
head = n1;
}
void PrintComplexList(ComplexNode* head)
{
while (head)
{
printf("【%d】", head->_data);
printf(":random->%d",
head->_random->_data);
printf(":next->");
head = head->_next;
}
printf("NULL\n");
}
ComplexNode* CopyComplexList(ComplexNode* cpList)
{
ComplexNode* head = cpList;
// 1.將copy出新鏈表的節點插在原鏈表每個節點的後面
head = cpList;
while (head)
{
ComplexNode* tmp = CreateComplexNode(head->_data);
ComplexNode* prev = head;
head = head->_next;
prev->_next = tmp;
tmp->_next = head;
}
// 2.處理copy鏈表節點的random指向
head = cpList;
while (head)
{
ComplexNode* random = head->_random;
ComplexNode* next = head->_next;
if (random)
{
next->_random = random->_next;
}
head = head->_next->_next;
}
// 3.摘下copy鏈表節點,構建出copy鏈表
head = cpList;
ComplexNode* newHead, *newTail;
if (head)
{
newHead = head->_next;
newTail = head->_next;
// 摘下copy節點
head->_next = head->_next->_next;
head = head->_next;
}
while (head)
{
newTail->_next = head->_next;
newTail = newTail->_next;
// 摘下copy節點
head->_next = head->_next->_next;
head = head->_next;
}
return newHead;
}
#include<stdio.h>
#include <iostream>
using namespace std;
#include "Slist.h"
// 測試PushBack
void Test1()
{
PLinkList pList;
printf("Test PushBack begin\n");
InitSList(&pList);
PushBack(&pList,1);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,4);
PrintSList(pList);
DestorySList(&pList);
printf("Test PushBack end\n\n");
}
// 測試PopBack
void Test2()
{
PLinkList pList;
printf("Test PopBack begin\n");
InitSList(&pList);
PushBack(&pList,1);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,4);
PrintSList(pList);
PopBack(&pList);
PopBack(&pList);
PopBack(&pList);
PopBack(&pList);
PrintSList(pList);
DestorySList(&pList);
printf("Test PopBack end\n\n");
}
// PushFront
void Test3()
{
PLinkList pList;
printf("Test PushFront begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PushFront(&pList,3);
PushFront(&pList,4);
PrintSList(pList);
DestorySList(&pList);
printf("Test PushFront end\n\n");
}
// PopFront
void Test4()
{
PLinkList pList;
printf("Test PopFront begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PushFront(&pList,3);
PushFront(&pList,4);
PrintSList(pList);
PopFront(&pList);
PopFront(&pList);
PopFront(&pList);
PopFront(&pList);
PrintSList(pList);
DestorySList(&pList);
printf("Test PopFront end\n\n");
}
// Find
void Test5()
{
PLinkList pList;
printf("Test Find begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PushFront(&pList,3);
PushFront(&pList,4);
PrintSList(pList);
if (Find(pList,4))
{
printf("Find 4 Success\n");
}
if (Find(pList,5) ==0)
{
printf("Not Find 5\n");
}
DestorySList(&pList);
printf("Test Find end\n\n");
}
// Remove
void Test6()
{
PLinkList pList;
printf("Test Remove begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PushFront(&pList,3);
PushFront(&pList,4);
PrintSList(pList);
Remove(&pList,1);
Remove(&pList,2);
Remove(&pList,3);
Remove(&pList,4);
PrintSList(pList);
DestorySList(&pList);
printf("Test Remove end\n\n");
}
// Insert
void Test7()
{
PLinkList pList;
printf("Test Insert begin\n");
InitSList(&pList);
Insert(&pList, pList,1);
Insert(&pList, pList,2);
Insert(&pList, pList,3);
PrintSList(pList);
DestorySList(&pList);
printf("Test Insert end\n\n");
}
// Erase
void Test8()
{
Node* del = 0;
PLinkList pList;
printf("Test Erase begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PrintSList(pList);
del = Find(pList,1);
Erase(&pList, del);
del = Find(pList,2);
Erase(&pList, del);
PrintSList(pList);
DestorySList(&pList);
printf("Test Erase end\n\n");
}
/////////////////////////////////////////////////////////////////////////////////
// 面試題測試
// Reverse
void Test9()
{
Node* del = 0;
PLinkList pList;
printf("Test Reverse begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PushFront(&pList,3);
PushFront(&pList,4);
PrintSList(pList);
Reverse(&pList);
PrintSList(pList);
DestorySList(&pList);
printf("Test Reverse end\n\n");
}
// Sort & SortOP
void Test10()
{
Node* del = 0;
PLinkList pList;
printf("Test Sort begin\n");
InitSList(&pList);
//PushFront(&pList, 6);
//PushFront(&pList, 4);
//PushFront(&pList, 1);
//PushFront(&pList, 5);
//PushFront(&pList, 3);
//PushFront(&pList, 2);
//PushFront(&pList, 7);
//PushFront(&pList, 8);
//PushFront(&pList, 8);
//PushFront(&pList, 9);
//PushFront(&pList, 0);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,4);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,4);
PushBack(&pList,4);
PushBack(&pList,4);
PushBack(&pList,4);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,4);
PushBack(&pList,4);
PushBack(&pList,4);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,3);
PushBack(&pList,4);
PrintSList(pList);
//BubbleSort(pList);
QucikSort_OP(pList,NULL);
PrintSList(pList);
DestorySList(&pList);
printf("Test Sort end\n\n");
}
// Merge
void Test11()
{
Node* del = 0;
PLinkList pList1;
PLinkList pList2;
PLinkList mergePList;
printf("Test Merge begin\n");
InitSList(&pList1);
InitSList(&pList2);
InitSList(&mergePList);
PushFront(&pList1,1);
PushFront(&pList1,2);
PushFront(&pList1,3);
PushFront(&pList1,4);
PushFront(&pList2,10);
PrintSList(pList1);
PushFront(&pList2,0);
PushFront(&pList2,1);
PushFront(&pList2,5);
PushFront(&pList2,8);
PushFront(&pList2,9);
PrintSList(pList2);
BubbleSort(pList1,NULL);
BubbleSort(pList2,NULL);
mergePList = Merge(pList1, pList2);
PrintSList(mergePList);
DestorySList(&mergePList);
printf("Test Merge end\n\n");
}
// MergeRecursive
voidTest12()
{
Node* del = 0;
PLinkList pList1;
PLinkList pList2;
PLinkList mergePList;
printf("Test MergeRecursive begin\n");
initSList(&pList1);
InitSList(&pList2);
InitSList(&mergePList);
PushFront(&pList1,1);
PushFront(&pList1,2);
PushFront(&pList1,3);
PushFront(&pList1,4);
PushFront(&pList2,10);
PrintSList(pList1);
PushFront(&pList2,0);
PushFront(&pList2,1);
PushFront(&pList2,5);
PushFront(&pList2,8);
PushFront(&pList2,9);
PrintSList(pList2);
BubbleSort(pList1,NULL);
BubbleSort(pList2,NULL);
mergePList = MergeRecursive(pList1, pList2);
PrintSList(mergePList);
DestorySList(&mergePList);
printf("Test MergeRecursive end\n\n");
}
// DelNonTailNode
void Test13()
{
Node* del = 0;
PLinkList pList;
printf("Test DelMidNode begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PushFront(&pList,3);
PushFront(&pList,4);
PrintSList(pList);
del = Find(pList,3);
DelNonTailNode(del);
PrintSList(pList);
DestorySList(&pList);
printf("Test DelMidNode end\n\n");
}
// InsertFrontNode
void Test14()
{
Node* n = 0;
PLinkList pList;
printf("Test InsertFrontNode begin\n");
InitSList(&pList);
PushFront(&pList,1);
PushFront(&pList,2);
PushFront(&pList,3);
PushFront(&pList,4);
PrintSList(pList);
n = Find(pList,3);
InsertFrontNode(n,0);
PrintSList(pList);
DestorySList(&pList);
printf("Test InsertFrontNode end\n\n");
}
// FindMidNode
void Test15()
{
Node* n = 0;
PLinkList pList;
printf("Test FindMidNode begin\n");
InitSList(&pList);
PushBack(&pList,1);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,4);
PrintSList(pList);
n = FindMidNode(pList);
printf("Mid:%d\n", n->_data);
PushBack(&pList,5);
PrintSList(pList);
n = FindMidNode(pList);
printf("Mid:%d\n", n->_data);
DestorySList(&pList);
printf("Test FindMidNode end\n\n");
}
// DelKNode
void Test16()
{
Node* n = 0;
PLinkList pList;
printf("Test DelKNode begin\n");
InitSList(&pList);
PushBack(&pList,1);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,4);
PrintSList(pList);
DelKNode(pList,3);
PrintSList(pList);
DestorySList(&pList);
printf("Test DelKNode end\n\n");
}
// CheckCycle
void Test17()
{
int length =0;
Node* realEntry = 0, *getEntry =0;
Node* end = 0;
PLinkList pList;
PLinkList meetNode = 0;
printf("Test CheckCycle begin\n");
InitSList(&pList);
PushBack(&pList,1);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,4);
PushBack(&pList,5);
PushBack(&pList,6);
PushBack(&pList,7);
PrintSList(pList);
length = CheckCycle(pList, &meetNode);
printf("Plist Length:%d\n",length);
// 創建帶環鏈表
end = Find(pList,7);
realEntry = Find(pList,4);
end->_next = realEntry;
printf("realEntry:%d\n",realEntry->_data);
length = CheckCycle(pList, &meetNode);
printf("Plist Length:%d, meetNode:%d\n", length, meetNode->_data);
getEntry = GetCycleEntryNode(pList, meetNode);
printf("realEntry:%d, getEntry:%d\n", realEntry->_data, getEntry->_data);
// 解環
end->_next = 0;
DestorySList(&pList);
printf("Test CheckCycLeLength end\n\n");
}
// CheckCross
void Test18()
{
PLinkList pList1, pList2;
Node* realCrossNode, *end;
printf("Test CheckCross begin\n");
InitSList(&pList1);
InitSList(&pList2);
PushBack(&pList1,1);
PushBack(&pList1,2);
PushBack(&pList1,3);
PushBack(&pList1,4);
PushBack(&pList1,5);
PushBack(&pList1,6);
PushBack(&pList1,7);
PrintSList(pList1);
PushBack(&pList2,10);
PushBack(&pList2,11);
PushBack(&pList2,12);
PrintSList(pList2);
printf("Cross:%d\n",CheckCross(pList1, pList2));
// 創建相交鏈表
realCrossNode = Find(pList1,4);
end = Find(pList2,12);
end->_next = realCrossNode;
printf("Cross:%d\n",CheckCross(pList1, pList2));
// 解開
end->_next = NULL;
DestorySList(&pList1);
DestorySList(&pList2);
printf("Test CheckCycLeLength end\n\n");
}
// JosephCycle
void Test19()
{
Node* end;
PLinkList pList;
printf("Test JosephCycle begin\n");
InitSList(&pList);
PushBack(&pList,1);
PushBack(&pList,2);
PushBack(&pList,3);
PushBack(&pList,4);
PushBack(&pList,5);
PushBack(&pList,6);
PushBack(&pList,7);
PrintSList(pList);
// 創建帶環鏈表
end = Find(pList,7);
end->_next = pList;
printf("JosephCycle:%d\n",JosephCycle(pList,3)->_data);
printf("Test JosephCycle end\n\n");
}
// 複雜鏈表的複製
void Test20()
{
ComplexNode* cpList;
CreateComplexNode(cpList);
PrintComplexList(cpList);
ComplexNode* copyList = CopyComplexList(cpList);
PrintComplexList(copyList);
}
int main()
{
//Test1();
//Test2();
//Test3();
//Test4();
//Test5();
//Test6();
//Test7();
//Test8();
//Test9();
//Test10();
//Test11();
//Test12();
//Test13();
//Test14();
//Test15();
//Test16();
//Test17();
//Test18();
//Test19();
//Test20();
return0;
}