《C语言》单链表——增、删、查、改、销毁、排序、翻转、合并、取中间节点...
Main.c
#include "S_List.h"
#include <time.h>
void main()
{
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, i);
}
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddHead(&P_Head, i);
}
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddHead(&P_Head, i);
}
Show(P_Head);
if (NULL != FindNode(P_Head,0) && NULL != FindNode(P_Head, 5) && NULL != FindNode(P_Head, 9))
{
puts("Find.");
}
else
{
puts("Can't find.");
}
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddHead(&P_Head, i);
}
Show(P_Head);
InsertNode(&P_Head, 0, 100);
InsertNode(&P_Head, 5, 100);
InsertNode(&P_Head, 9, 100);
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddHead(&P_Head, i);
}
Show(P_Head);
DeleteNode(&P_Head, 0);
DeleteNode(&P_Head, 5);
DeleteNode(&P_Head, 9);
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddHead(&P_Head, i);
}
Show(P_Head);
Modeifier(P_Head, 0,100);
Modeifier(P_Head, 5,100);
Modeifier(P_Head, 9,100);
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 100; i++)
{
AddHead(&P_Head, i);
}
Show(P_Head);
printf("该链表有 %u 个节点\n", CounteNode(P_Head));
#endif
#if 0
S_List* P_Head1 = NULL;
S_List* P_Head2 = NULL;
S_List* P_All = NULL;
for (int i = 0; i < 10; i++)
{
if (i % 2 != 0)
{
AddBack(&P_Head1, i);
}
else
{
AddBack(&P_Head2, i);
}
}
AddBack(&P_Head2, 250);
AddBack(&P_Head2, 9527);
AddBack(&P_Head1, 520);
AddBack(&P_Head1, 1314);
Show(P_Head1);
Show(P_Head2);
Merge(&P_All, P_Head1, P_Head2);
Show(P_All);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, i);
}
Show(P_Head);
ReversalByLoop(&P_Head);
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, i);
}
Show(P_Head);
P_Head=ReversalByRecursion(P_Head);
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, i);
}
Show(P_Head);
Destroy(&P_Head);
Show(P_Head);
#endif
#if 0
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, i);
}
Show(P_Head);
printf("中间节点:%d\n", GetMiddleNode(P_Head)->Data);
#endif
#if 0
srand((unsigned int)time(NULL));
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, rand()%100);
}
Show(P_Head);
BubbleSort(P_Head);
Show(P_Head);
#endif
#if 0
srand((unsigned int)time(NULL));
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, rand() % 100);
}
Show(P_Head);
QuickSort(P_Head,NULL);
Show(P_Head);
#endif
#if 1
srand((unsigned int)time(NULL));
S_List* P_Head = NULL;
for (int i = 0; i < 10; i++)
{
AddBack(&P_Head, rand() % 100);
}
Show(P_Head);
SelectSort(P_Head);
Show(P_Head);
#endif
system("pause");
}
S_List.h
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct S_List
{
DataType Data;
struct S_List* P_Next;
}S_List;
void InitNode(S_List* P_Node);
void Show(S_List* P_Head);
void AddBack(S_List** PP_Head, DataType Data);
void AddHead(S_List** PP_Head, DataType Data);
S_List* FindNode(S_List* P_Head, DataType Data);
void InsertNode(S_List** PP_Head, DataType InData, DataType InsData);
void DeleteNode(S_List** PP_Head, DataType DelData);
void Modeifier(S_List* P_Head, DataType OldData, DataType NewData);
unsigned int CounteNode(S_List* P_Head);
void Merge(S_List** PP_All, S_List* P_Head1, S_List* P_Head2);
void ReversalByLoop(S_List** PP_Head);
S_List* ReversalByRecursion(S_List* P_Head);
void Destroy(S_List** PP_Head);
S_List* GetMiddleNode(S_List* P_Head);
void BubbleSort(S_List* P_Head);
void QuickSort(S_List* P_Begin, S_List* P_End);
void SelectSort(S_List* P_Head);
void Swap(S_List* Pa, S_List* Pb);
S_List* Segmentation(S_List* P_Begin, S_List* P_End);
#ifdef __cplusplus
}
#endif
S_LIst.c
#include "S_List.h"
void InitNode(S_List* P_Node)
{
P_Node->P_Next = NULL;
P_Node->Data = 0;
}
void Show(S_List* P_Head)
{
if (NULL == P_Head)
{
puts("");
return;
}
else
{
printf("%p\t%p\t%d\n", P_Head, P_Head->P_Next, P_Head->Data);
Show(P_Head->P_Next);
}
}
void AddBack(S_List** PP_Head, DataType Data)
{
S_List* P_New = (S_List*)malloc(sizeof(S_List));
InitNode(P_New);
P_New->Data = Data;
if (NULL == *PP_Head)
{
*PP_Head = P_New;
}
else
{
S_List* P_Bak = *PP_Head;
while (NULL != P_Bak->P_Next)
{
P_Bak = P_Bak->P_Next;
}
P_Bak->P_Next = P_New;
}
}
void AddHead(S_List** PP_Head, DataType Data)
{
S_List* P_New = (S_List*)malloc(sizeof(S_List));
InitNode(P_New);
P_New->Data = Data;
if (NULL == PP_Head)
{
*PP_Head = P_New;
}
else
{
P_New->P_Next = *PP_Head;
*PP_Head = P_New;
}
}
S_List* FindNode(S_List* P_Head, DataType Data)
{
if (NULL == P_Head)
{
return NULL;
}
else
{
while (NULL != P_Head)
{
if (Data == P_Head->Data)
{
return P_Head;
}
P_Head = P_Head->P_Next;
}
}
return NULL;
}
void InsertNode(S_List** PP_Head, DataType InData, DataType InsData)
{
if (NULL != *PP_Head)
{
S_List* P_Res = FindNode(*PP_Head, InData);
if (NULL != P_Res)
{
S_List* P_New = (S_List*)malloc(sizeof(S_List));
InitNode(P_New);
P_New->Data = InsData;
S_List* P_Bak = NULL;
if (P_Res == *PP_Head)
{
P_Bak = P_Res->P_Next;
P_Res->P_Next = P_New;
P_New->P_Next = P_Bak;
}
else if (NULL==P_Res->P_Next)
{
P_Res->P_Next = P_New;
}
else
{
P_Bak = P_Res->P_Next;
P_Res->P_Next = P_New;
P_New->P_Next = P_Bak;
}
}
}
}
void DeleteNode(S_List** PP_Head, DataType DelData)
{
if (NULL != *PP_Head)
{
S_List* P_Res = FindNode(*PP_Head, DelData);
if (NULL != P_Res)
{
if (*PP_Head == P_Res)
{
*PP_Head = (*PP_Head)->P_Next;
free(P_Res);
P_Res = NULL;
}
else
{
S_List* P_Bak = *PP_Head;
while (P_Res != P_Bak->P_Next)
{
P_Bak = P_Bak->P_Next;
}
P_Bak->P_Next = P_Res->P_Next;
free(P_Res);
P_Res = NULL;
}
}
}
}
void Modeifier(S_List* P_Head, DataType OldData, DataType NewData)
{
if (NULL != P_Head)
{
S_List* P_Res = FindNode(P_Head, OldData);
if (NULL != P_Res)
{
P_Res->Data = NewData;
}
}
}
unsigned int CounteNode(S_List* P_Head)
{
if (NULL == P_Head)
{
return 0;
}
else
{
return 1 + CounteNode(P_Head->P_Next);
}
}
void Merge(S_List** PP_All, S_List* P_Head1, S_List* P_Head2)
{
while (NULL != P_Head1||NULL != P_Head2)
{
if (NULL != P_Head1&&NULL != P_Head2)
{
if (P_Head1->Data < P_Head2->Data)
{
AddBack(PP_All, P_Head1->Data);
P_Head1 = P_Head1->P_Next;
}
else
{
AddBack(PP_All, P_Head2->Data);
P_Head2 = P_Head2->P_Next;
}
}
else
{
while (NULL != P_Head1)
{
AddBack(PP_All, P_Head1->Data);
P_Head1 = P_Head1->P_Next;
}
while (NULL != P_Head2)
{
AddBack(PP_All, P_Head2->Data);
P_Head2 = P_Head2->P_Next;
}
}
}
}
void ReversalByLoop(S_List** PP_Head)
{
if (NULL == *PP_Head || NULL == (*PP_Head))
{
return;
}
else
{
S_List* P_Pre, *P_Cur, *P_Next;
P_Pre = P_Cur = P_Next = NULL;
P_Pre = *PP_Head;
P_Cur = *PP_Head;
while (NULL != P_Cur)
{
P_Next = P_Cur->P_Next;
P_Cur->P_Next = P_Pre;
P_Pre = P_Cur;
P_Cur = P_Next;
}
(*PP_Head)->P_Next = NULL;
*PP_Head = P_Pre;
}
}
S_List* ReversalByRecursion(S_List* P_Head)
{
if (NULL == P_Head || NULL == P_Head->P_Next)
{
return P_Head;
}
else
{
S_List* P_Next = P_Head->P_Next;
S_List* P_NewHead = ReversalByRecursion(P_Next);
P_Next->P_Next = P_Head;
P_Head->P_Next = NULL;
return P_NewHead;
}
}
void Destroy(S_List** PP_Head)
{
if (NULL == *PP_Head)
{
return;
}
else
{
S_List* P_Bak = (*PP_Head)->P_Next;
while (NULL != P_Bak)
{
S_List* P_Bak2 = P_Bak;
P_Bak = P_Bak->P_Next;
free(P_Bak2);
P_Bak2 = NULL;
}
free(*PP_Head);
*PP_Head = NULL;
}
}
S_List* GetMiddleNode(S_List* P_Head)
{
if (NULL == P_Head || NULL == P_Head->P_Next)
{
return P_Head->P_Next;
}
else
{
S_List* P1 = P_Head;
S_List* P2 = P_Head;
while (NULL != P2->P_Next)
{
P1 = P1->P_Next;
P2 = P2->P_Next;
if (NULL != P2->P_Next)
{
P2 = P2->P_Next;
}
}
return P1;
}
return NULL;
}
void BubbleSort(S_List* P_Head)
{
for (S_List* Pi = P_Head; NULL != Pi; Pi = Pi->P_Next)
{
for (S_List* Pj = Pi->P_Next; NULL != Pj; Pj = Pj->P_Next)
{
if (Pi->Data < Pj->Data)
{
Swap(Pi, Pj);
}
}
}
}
void QuickSort(S_List* P_Begin, S_List* P_End)
{
if (P_Begin != P_End)
{
S_List* Pi = Segmentation(P_Begin, P_End);
QuickSort(P_Begin, Pi);
QuickSort(Pi->P_Next, P_End);
}
}
void SelectSort(S_List* P_Head)
{
S_List* P_Max=NULL;
for (S_List* Pi = P_Head; NULL != Pi; Pi = Pi->P_Next)
{
P_Max = Pi;
for (S_List* Pj = Pi->P_Next; NULL != Pj; Pj = Pj->P_Next)
{
if (P_Max->Data < Pj->Data)
{
P_Max = Pj;
}
}
if (P_Max!=Pi)
{
Swap(P_Max, Pi);
}
}
}
void Swap(S_List* Pa, S_List* Pb)
{
DataType Temp = Pa->Data;
Pa->Data = Pb->Data;
Pb->Data = Temp;
}
S_List* Segmentation(S_List* P_Begin, S_List* P_End)
{
DataType Key = P_Begin->Data;
S_List* Pi = P_Begin;
S_List* Pj = Pi->P_Next;
while (Pj != P_End)
{
if (Pj->Data > P_Begin->Data)
{
Pi = Pi->P_Next;
Swap(Pi, Pj);
}
Pj = Pj->P_Next;
}
Swap(Pi, P_Begin);
return Pi;
}