程序片段(01):指针与结构体.c
内容概要:指针与结构体
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
typedef struct
{
long long QQ;
char * pPass;
int passLen;
}Row;
typedef struct
{
Row * pRow;
int rowNum;
}Array;
int main01(void)
{
Array * pArr = (Array *)malloc(sizeof(Array));
(*pArr).rowNum = 10;
(*pArr).pRow = (Row *)malloc((*pArr).rowNum * sizeof(Row));
pArr->pRow[0].QQ = 77025077;
pArr->pRow[0].pPass = (char *)malloc(10 * sizeof(char));
strcpy(pArr->pRow[0].pPass, "yincheng");
pArr->pRow[0].passLen = 10;
system("pause");
}
程序片段(02):链表.c
内容概要:链表简单
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node * pNextLinkNode;
}LinkNode;
void showLinkList(LinkNode * pLinkList)
{
if (NULL == pLinkList)
{
return;
}
printf("%p, %d, %p
", pLinkList, (*pLinkList).data, (*pLinkList).pNextLinkNode);
showLinkList((*pLinkList).pNextLinkNode);
}
int main01(void)
{
LinkNode * pLinkList;
LinkNode linkNode01;
linkNode01.data = 1;
LinkNode linkNode02;
linkNode02.data = 2;
LinkNode linkNode03;
linkNode03.data = 3;
LinkNode linkNode04;
linkNode04.data = 4;
LinkNode linkNode05;
linkNode05.data = 5;
pLinkList = &linkNode01;
linkNode01.pNextLinkNode = &linkNode02;
linkNode02.pNextLinkNode = &linkNode03;
linkNode03.pNextLinkNode = &linkNode04;
linkNode04.pNextLinkNode = &linkNode05;
linkNode05.pNextLinkNode = NULL;
showLinkList(pLinkList);
system("pause");
}
程序片段(03):LinkList.h+LinkList.c+LinkListOnHeapTest.c
内容概要:LinkList
#pragma once
typedef struct Node
{
int data;
struct Node * pNext;
}LinkNode;
void initLinkNode(LinkNode * pLinkNode);
void linkListHeadInsertData(LinkNode ** ppLinkList, int data);
void linkListTailInsertData(LinkNode ** ppLinkList, int data);
void showLinkList(LinkNode * pLinkList);
void revShowLinkList(LinkNode * pLinkList);
LinkNode * linkListSelectFirstData(LinkNode * pLinkList, int data);
void linkListRandInsertData(LinkNode ** pLinkList, int data, int insertData);
void linkListDeleteFirstData(LinkNode ** pLinkList, int data);
void linkListDeleteAllDatas(LinkNode ** pLinkList, int data);
void linkListUpdateFirstData(LinkNode * pLinkList, int oldData, int newData);
void linkListUpdateAllDatas(LinkNode * pLinkList, int oldData, int NewData);
int countLinkNodes(LinkNode * pLinkList);
void linkListRevByLoop(LinkNode ** ppLinkList);
LinkNode * linkListRevByRecu(LinkNode * ppLinkList);
void linkListBubbleSort(LinkNode * pLinkList);
void linkListQsort(LinkNode * pHeadNode, LinkNode * pTailNode);
void linkListQuick(LinkNode * pHeadNode, LinkNode * pTailNode);
void linkListMerger(LinkNode * pLinkList1, LinkNode * pLinkList2, LinkNode ** ppLinkList);
LinkNode * linkListSelectMidNode(LinkNode * pLinkList);
int linkListIsCircleLinkList(LinkNode * pLinkList);
void showCircleLinkList(LinkNode * pLinkList);
LinkNode * circleLinkListGetEndNode(LinkNode ** ppCircleLinkList, int num);
#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
void initLinkNode(LinkNode * pLinkNode)
{
if (NULL == pLinkNode)
abort();
(*pLinkNode).data = 0;
(*pLinkNode).pNext = NULL;
}
void linkListHeadInsertData(LinkNode ** ppLinkList, int data)
{
LinkNode * pNewNode = (LinkNode *)malloc(sizeof(LinkNode));
(*pNewNode).data = data;
(*pNewNode).pNext = NULL;
if (NULL == *ppLinkList)
{
*ppLinkList = pNewNode;
return;
}
(*pNewNode).pNext = *ppLinkList;
*ppLinkList = pNewNode;
}
void linkListTailInsertData(LinkNode ** ppLinkList, int data)
{
LinkNode * pNewNode = (LinkNode *)malloc(sizeof(LinkNode));
(*pNewNode).data = data;
(*pNewNode).pNext = NULL;
if (NULL == *ppLinkList)
{
*ppLinkList = pNewNode;
return;
}
LinkNode * pTmpNode = *ppLinkList;
while (NULL != (*pTmpNode).pNext)
{
pTmpNode = (*pTmpNode).pNext;
}
(*pTmpNode).pNext = pNewNode;
}
void showLinkList(LinkNode * pLinkList)
{
if (NULL == pLinkList)
return;
printf("%p, %d, %p
", pLinkList, (*pLinkList).data, (*pLinkList).pNext);
showLinkList((*pLinkList).pNext);
}
void revShowLinkList(LinkNode * pLinkList)
{
if (NULL == pLinkList)
return;
revShowLinkList((*pLinkList).pNext);
printf("%p, %d, %p
", pLinkList, (*pLinkList).data, (*pLinkList).pNext);
}
LinkNode * linkListSelectFirstData(LinkNode * pLinkList, int data)
{
if (NULL == pLinkList)
abort();
for (LinkNode * pTmpNode = pLinkList; NULL != pTmpNode; pTmpNode = (*pTmpNode).pNext)
{
if (data == (*pTmpNode).data)
{
return pTmpNode;
}
}
return NULL;
}
void linkListRandInsertData(LinkNode ** ppLinkList, int data, int insertData)
{
if (NULL == *ppLinkList)
abort();
LinkNode * pTmpNode1 = NULL, *pTmpNode2 = *ppLinkList;
while (NULL != pTmpNode2)
{
if (data == (*pTmpNode2).data)
break;
pTmpNode1 = pTmpNode2;
pTmpNode2 = (*pTmpNode2).pNext;
}
if (NULL == pTmpNode1 && data != (**ppLinkList).data)
{
return;
}
LinkNode * pNewNode = (LinkNode *)malloc(sizeof(LinkNode));
(*pNewNode).data = insertData;
(*pNewNode).pNext = NULL;
if (NULL == pTmpNode1)
{
(*pNewNode).pNext = *ppLinkList;
*ppLinkList = pNewNode;
}
else
{
(*pNewNode).pNext = pTmpNode2;
(*pTmpNode1).pNext = pNewNode;
}
}
void linkListDeleteFirstData(LinkNode ** ppLinkList, int data)
{
if (NULL == *ppLinkList)
return;
LinkNode * pTmpNode1 = NULL, *pTmpNode2 = *ppLinkList;
while (NULL != pTmpNode2)
{
if (data == (*pTmpNode2).data)
break;
pTmpNode1 = pTmpNode2;
pTmpNode2 = (*pTmpNode2).pNext;
}
if (NULL == pTmpNode1 && data != (*(*ppLinkList)).data)
{
return;
}
if (NULL == pTmpNode1)
{
*ppLinkList = (*pTmpNode2).pNext;
}
else
{
(*pTmpNode1).pNext = (*pTmpNode2).pNext;
}
free(pTmpNode2);
}
void linkListUpdateFirstData(LinkNode * pLinkList, int oldData, int newData)
{
if (NULL == pLinkList)
abort();
for (LinkNode * pTmpNode = pLinkList; NULL != pTmpNode; pTmpNode = (*pTmpNode).pNext)
{
if (oldData == (*pTmpNode).data)
{
(*pTmpNode).data = newData;
break;
}
}
}
void linkListBubbleSort(LinkNode * pLinkList)
{
if (NULL == pLinkList)
abort();
for (LinkNode * pTmpNode1 = pLinkList; NULL != (*pTmpNode1).pNext; pTmpNode1 = (*pTmpNode1).pNext)
{
for (LinkNode * pTmpNode2 = (*pTmpNode1).pNext; NULL != pTmpNode2; pTmpNode2 = (*pTmpNode2).pNext)
{
if ((*pTmpNode1).data > (*pTmpNode2).data)
{
(*pTmpNode1).data = (*pTmpNode1).data ^ (*pTmpNode2).data;
(*pTmpNode2).data = (*pTmpNode1).data ^ (*pTmpNode2).data;
(*pTmpNode1).data = (*pTmpNode1).data ^ (*pTmpNode2).data;
}
}
}
}
int countLinkNodes(LinkNode * pLinkList)
{
if (NULL == pLinkList)
return 0;
return 1 + countLinkNodes((*pLinkList).pNext);
}
void linkListRevByLoop(LinkNode ** ppLinkList)
{
if (NULL == *ppLinkList || NULL == (*(*ppLinkList)).pNext)
{
return;
}
LinkNode * pPreNode, *pCurNode, *pNextNode;
pPreNode = pCurNode = pNextNode = NULL;
pPreNode = *ppLinkList;
pCurNode = (*(*ppLinkList)).pNext;
while (NULL != pCurNode)
{
pNextNode = (*pCurNode).pNext;
(*pCurNode).pNext = pPreNode;
pPreNode = pCurNode;
pCurNode = pNextNode;
}
(*(*ppLinkList)).pNext = NULL;
*ppLinkList = pPreNode;
}
LinkNode * linkListRevByRecu(LinkNode * pLinkList)
{
if (NULL == pLinkList || NULL == (*pLinkList).pNext)
{
return pLinkList;
}
LinkNode * pNextNode = (*pLinkList).pNext;
LinkNode * pHeadNode = linkListRevByRecu(pNextNode);
(*pNextNode).pNext = pLinkList;
(*pLinkList).pNext = NULL;
return pHeadNode;
}
LinkNode * getDivNode(LinkNode * pHeadNode, LinkNode * pTailNode)
{
int divValue = (*pHeadNode).data;
LinkNode * pTmpNode1 = pHeadNode;
LinkNode * pTmpNode2 = (*pHeadNode).pNext;
while (pTailNode != pTmpNode2)
{
if (divValue > (*pTmpNode2).data)
{
pTmpNode1 = (*pTmpNode1).pNext;
int temp = (*pTmpNode1).data;
(*pTmpNode1).data = (*pTmpNode2).data;
(*pTmpNode2).data = temp;
}
pTmpNode2 = (*pTmpNode2).pNext;
}
int temp = (*pTmpNode1).data;
(*pTmpNode1).data = (*pHeadNode).data;
(*pHeadNode).data = temp;
return pTmpNode1;
}
void linkListQsort(LinkNode * pHeadNode, LinkNode * pTailNode)
{
if (pHeadNode == pTailNode)
return;
LinkNode * pDivNode = getDivNode(pHeadNode, pTailNode);
linkListQsort(pHeadNode, pDivNode);
linkListQsort((*pDivNode).pNext, pTailNode);
}
void linkListQuick(LinkNode * pHeadNode, LinkNode * pTailNode)
{
if (pHeadNode == pTailNode)
return;
int divValue = (*pHeadNode).data;
LinkNode * pTmpNode1 = pHeadNode;
for (LinkNode * pTmpNode2 = (*pHeadNode).pNext; NULL != pTmpNode2; pTmpNode2 = (*pTmpNode2).pNext)
{
if (divValue > (*pTmpNode2).data)
{
pTmpNode1 = (*pTmpNode1).pNext;
int temp = (*pTmpNode1).data;
(*pTmpNode1).data = (*pTmpNode2).data;
(*pTmpNode2).data = temp;
}
}
int temp = (*pTmpNode1).data;
(*pTmpNode1).data = (*pHeadNode).data;
(*pHeadNode).data = temp;
linkListQuick(pHeadNode, pTmpNode1);
linkListQuick((*pTmpNode1).pNext, pTailNode);
}
void linkListMerger(LinkNode * pLinkList1, LinkNode * pLinkList2, LinkNode ** ppLinkList3)
{
if (NULL == pLinkList1 || NULL == pLinkList2)
abort();
while (NULL != pLinkList1 || NULL != pLinkList2)
{
if (NULL != pLinkList1 && NULL != pLinkList2)
{
if ((*pLinkList1).data < (*pLinkList2).data)
{
linkListTailInsertData(ppLinkList3, (*pLinkList1).data);
pLinkList1 = (*pLinkList1).pNext;
}
else
{
linkListTailInsertData(ppLinkList3, (*pLinkList2).data);
pLinkList2 = (*pLinkList2).pNext;
}
}
else
{
while (NULL != pLinkList1)
{
linkListTailInsertData(ppLinkList3, (*pLinkList1).data);
pLinkList1 = (*pLinkList1).pNext;
}
while (NULL != pLinkList2)
{
linkListTailInsertData(ppLinkList3, (*pLinkList2).data);
pLinkList2 = (*pLinkList2).pNext;
}
}
}
}
LinkNode * linkListSelectMidNode(LinkNode * pLinkList)
{
if (NULL == pLinkList || NULL == (*pLinkList).pNext)
return pLinkList;
LinkNode * pTmpNode1 = pLinkList;
LinkNode * pTmpNode2 = (*pLinkList).pNext;
while (NULL != pTmpNode2)
{
if (NULL == (pTmpNode2 = pTmpNode2->pNext))
break;
pTmpNode2 = pTmpNode2->pNext;
pTmpNode1 = pTmpNode1->pNext;
}
return pTmpNode1;
}
int linkListIsCircleLinkList(LinkNode * pLinkList)
{
if (NULL == pLinkList)
return 0;
LinkNode * pTmpNode1 = pLinkList;
LinkNode * pTmpNode2 = (*pLinkList).pNext;
while (NULL != pTmpNode2)
{
if ((*pTmpNode1).data == (*pTmpNode2).data)
return 1;
if (NULL == (pTmpNode2 = (*pTmpNode2).pNext))
return 0;
if (NULL == (pTmpNode2 = (*pTmpNode2).pNext))
return 0;
pTmpNode1 = (*pTmpNode1).pNext;
}
return 0;
}
void showCircleLinkList(LinkNode * pCircleLinkList)
{
if (NULL == pCircleLinkList)
abort();
LinkNode * pTmpNode = pCircleLinkList;
do
{
printf("%4d", (*pTmpNode).data);
pTmpNode = (*pTmpNode).pNext;
} while (pCircleLinkList != pTmpNode);
}
LinkNode * circleLinkListGetEndNode(LinkNode ** ppCircleLinkList, int num)
{
}
#include "LinkList.h"
#include
#include
int main01(void)
{
LinkNode * pLinkList = NULL;
linkListTailInsertData(&pLinkList, 1);
linkListTailInsertData(&pLinkList, 2);
linkListTailInsertData(&pLinkList, 3);
linkListTailInsertData(&pLinkList, 6);
linkListTailInsertData(&pLinkList, 5);
linkListTailInsertData(&pLinkList, 4);
linkListQuick(pLinkList, NULL);
showLinkList(pLinkList);
system("pause");
}
int main02(void)
{
LinkNode * pLinkList1 = NULL;
linkListTailInsertData(&pLinkList1, 2);
linkListTailInsertData(&pLinkList1, 4);
linkListTailInsertData(&pLinkList1, 6);
linkListTailInsertData(&pLinkList1, 8);
linkListTailInsertData(&pLinkList1, 10);
linkListTailInsertData(&pLinkList1, 12);
linkListTailInsertData(&pLinkList1, 14);
LinkNode * pLinkList2 = NULL;
linkListTailInsertData(&pLinkList2, 1);
linkListTailInsertData(&pLinkList2, 3);
linkListTailInsertData(&pLinkList2, 5);
linkListTailInsertData(&pLinkList2, 7);
linkListTailInsertData(&pLinkList2, 9);
linkListTailInsertData(&pLinkList2, 11);
linkListTailInsertData(&pLinkList2, 13);
LinkNode * pLinkList3 = NULL;
linkListMerger(pLinkList1, pLinkList2, &pLinkList3);
showLinkList(pLinkList3);
printf("midValue = %d
", (*linkListSelectMidNode(pLinkList3)).data);
system("pause");
}
int main(void)
{
LinkNode * pCircleLinkList = NULL;
linkListTailInsertData(&pCircleLinkList, 1);
linkListTailInsertData(&pCircleLinkList, 2);
linkListTailInsertData(&pCircleLinkList, 3);
linkListTailInsertData(&pCircleLinkList, 4);
linkListTailInsertData(&pCircleLinkList, 5);
linkListTailInsertData(&pCircleLinkList, 6);
linkListTailInsertData(&pCircleLinkList, 7);
linkListTailInsertData(&pCircleLinkList, 8);
linkListTailInsertData(&pCircleLinkList, 9);
LinkNode * pTailNode = linkListSelectFirstData(pCircleLinkList, 9);
(*pTailNode).pNext = pCircleLinkList;
showCircleLinkList(pCircleLinkList);
system("pause");
}
程序片段(04):数组快速排序.c
内容概要:快速排序法
#include
#include
void showArray(int * pArr, int len)
{
for (int i = 0; i < len; ++i)
{
printf("%4d", *(pArr + i));
}
printf("
");
}
void arrayQsort(int * pArr, int startI, int endI)
{
if (startI < endI)
{
int i = startI;
for (int j = i + 1; j <= endI; ++j)
{
if (*(pArr + j) < *(pArr + startI))
{
++i;
int temp = pArr[i];
pArr[i] = pArr[j];
pArr[j] = temp;
}
}
int temp = pArr[startI];
pArr[startI] = pArr[i];
pArr[i] = temp;
arrayQsort(pArr, startI, i - 1);
arrayQsort(pArr, i + 1, endI);
}
}
int main(void)
{
int arr[10] = { 10, 11, 9, 17, 8, 29, 5, 39, 6, 102 };
showArray(arr, 10);
arrayQsort(arr, 0, 9);
showArray(arr, 10);
system("pause");
}