作为一个自动化专业毕业的年轻小伙,自己的编程水平有限,需要广泛学习训练。特此记录下自己编程学习代码。非科班毕业,没有严格考量时间和空间的复杂度。
学习当然是学习了别人的思想然后内化的呀。
源码1 linklist1.h
// 单向class="superseo">链表
//头节点无数据域只有指针域
#ifndef _LINKLIST1_H
#define _LINKLIST1_H
#include
#include
#define TRACE(str) printf("%s\n",str)
struct LinkNode
{
void *pData;
struct LinkNode *pNext;
};
struct LinkList
{
struct LinkNode head;
int length;
};
//创建链表
struct LinkList* CreatLinkList(void);
//按位置往链表插入节点
void InsertToLinlkListByPos(struct LinkList*pLinkList, int position, void* pData);
//往链表头插入节点
void InsertToLinkListHead(struct LinkList*pLinkList, void* pData);
//往链表尾插入节点
void InsertToLinkListTail(struct LinkList*pLinkList, void* pData);
//遍历链表
void TraverseLinkList(struct LinkList* pLinkList, void(*printCallback)(void*));
//按位置获取链表节点的数据指针
void *GetDataPointerOfNodeInLinkListByPos(struct LinkList* pLinkList,int pos);
//获取链表第一个节点的数据指针
void *GetDataPointerOfFirstNodeInLinkList(struct LinkList* pLinkList);
//获取链表最后一个节点的数据指针
void *GetDataPointerOfLastNodeInLinkList(struct LinkList* pLinkList);
//按位置删除链表的节点
void DeleteNodeOFLinkListByPosition(struct LinkList* pLinkList, int position);
//删除链表的第一个节点
void DeleteFirstNodeOfLinkList(struct LinkList* pLinkList);
//删除链表的最后一个节点
void DeleteLastNodeOfLinkList(struct LinkList* pLinkList);
//按数据指针删除链表内节点
void DeleteNodeOFLinkListByValue(struct LinkList* pLinkList, void* data, int(*comparecallback)(void* data1, void* data2));
//清空链表
void ClearLinkList(struct LinkList* pLinkList);
//摧毁链表
void DestroyLinkList(struct LinkList* linklistpointer);
//链表去重(删除数据域相同的节点)
void QuCHongLiskList(struct LinkList*pLinkList,int(*CompareCallBack)(void*,void*));
//链表逆序
void ReverseLinkList(struct LinkList*pLinkList);
#endif
2 linklist1.c
#include "linklist1.h"
struct LinkList* CreatLinkList(void)
{
struct LinkList* pLinkList;
pLinkList = (struct LinkList*)malloc(sizeof(struct LinkList));
if (pLinkList == NULL)
{
return(NULL);
}
pLinkList->head.pData = NULL;
pLinkList->head.pNext = NULL;
pLinkList->length = 0;
return(pLinkList);
}
void ClearLinkList(struct LinkList* pLinkList)
{
if (pLinkList == NULL)
{
return;
}
if (pLinkList->length < 1)
{
return;
}
int length = pLinkList->length;
for (int i = 0; i < length; i++)
{
//用尾删函数删除链表节点
DeleteLastNodeOfLinkList(pLinkList);
}
}
void InsertToLinlkListByPos(struct LinkList*pLinkList,int position,void*pData)
{
if (pLinkList == NULL)
{
return;
}
if (pData == NULL)
{
return;
}
if (position < 0 || position > pLinkList->length)
{
return;
}
struct LinkNode* pFrontNode = &pLinkList->head;
for (int i = 0; i < position; i ++)
{
pFrontNode = pFrontNode->pNext;
}
struct LinkNode* pNewNode = (struct LinkNode*)malloc(sizeof(struct LinkNode));
pNewNode->pData = pData;
pNewNode->pNext = NULL;
pNewNode->pNext = pFrontNode->pNext;
pFrontNode->pNext = pNewNode;
pLinkList->length++;
}
void InsertToLinkListHead(struct LinkList*pLinkList, void* pData)
{
if (pLinkList == NULL)
{
return;
}
if (pData == NULL)
{
return;
}
InsertToLinlkListByPos(pLinkList,0,pData);
}
void InsertToLinkListTail(struct LinkList*pLinkList, void* pData)
{
if (pLinkList == NULL)
{
return;
}
if (pData == NULL)
{
return;
}
InsertToLinlkListByPos(pLinkList,pLinkList->length,pData);
}
void TraverseLinkList(struct LinkList*pLinkList,void(*printCallback)(void*))
{
if (pLinkList == NULL)
{
return;
}
struct LinkNode* pCurNode = &pLinkList->head;
for (int i = 0; i < pLinkList->length; i++)
{
pCurNode = pCurNode->pNext;
printCallback(pCurNode->pData);
}
}
void *GetDataPointerOfNodeInLinkListByPos(struct LinkList* pLinkList,int pos)
{
if (pLinkList == NULL)
{
return(NULL);
}
if (pos < 0)
{
return(NULL);
}
if (pos >= pLinkList->length)
{
return(NULL);
}
struct LinkNode *pNode = &pLinkList->head;
for (int i = 0; i <= pos; i ++)
{
pNode = pNode->pNext;
}
return(pNode->pData);
}
//获取链表第一个节点的数据指针
void *GetDataPointerOfFirstNodeInLinkList(struct LinkList* pLinkList)
{
if (pLinkList == NULL)
{
return(NULL);
}
if (pLinkList->length < 1)
{
return(NULL);
}
void *pData;
pData = GetDataPointerOfNodeInLinkListByPos(pLinkList,0);
return(pData);
}
//获取链表最后一个节点的数据指针
void *GetDataPointerOfLastNodeInLinkList(struct LinkList* pLinkList)
{
if (pLinkList == NULL)
{
return(NULL);
}
if (pLinkList->length < 1)
{
return(NULL);
}
void *pData;
pData = GetDataPointerOfNodeInLinkListByPos(pLinkList,pLinkList->length - 1);
return(pData);
}
void DeleteNodeOFLinkListByPosition(struct LinkList*pLinkList,int position)
{
if (pLinkList == NULL)
{
return;
}
if (position < 0 || position >= pLinkList->length)
{
return;
}
struct LinkNode* pCurNode;
pCurNode = &pLinkList->head;
for (int i = 0; i < position; i++)
{
pCurNode = pCurNode->pNext;
}
struct LinkNode* pDeleteNode;
pDeleteNode = pCurNode->pNext;
pCurNode->pNext = pDeleteNode->pNext;
free(pDeleteNode);
pLinkList->length--;
}
void DeleteFirstNodeOfLinkList(struct LinkList* pLinkList)
{
if (pLinkList == NULL)
{
return;
}
DeleteNodeOFLinkListByPosition(pLinkList,0);
}
void DeleteLastNodeOfLinkList(struct LinkList* pLinkList)
{
if (pLinkList == NULL)
{
return;
}
DeleteNodeOFLinkListByPosition(pLinkList,pLinkList->length - 1);
}
void DeleteNodeOFLinkListByValue(struct LinkList* pLinkList, void* pDataIn, int(*comparecallback)(void* data1, void* data2))
{
if (pLinkList == NULL)
{
return;
}
if (pDataIn == NULL)
{
return;
}
for (int i = 0; i < pLinkList->length; i ++)
{
void *pData;
pData = GetDataPointerOfNodeInLinkListByPos(pLinkList,i);
if (comparecallback(pData,pDataIn) == 0)
{
DeleteNodeOFLinkListByPosition(pLinkList,i);
i --;
}
}
}
void DestroyLinkList(struct LinkList* pLinkList)
{
if (pLinkList == NULL)
{
return;
}
ClearLinkList(pLinkList);
free(pLinkList);
}
void QuCHongLiskList(struct LinkList*pLinkList,int(*compareCallBack)(void*,void*))
{
if (pLinkList == NULL)
{
return;
}
int length = pLinkList->length;
if (length < 2)
{
return;
}
if (compareCallBack == NULL)
{
return;
}
void **ppData = (void **)malloc(sizeof(void *) * length);
int sameCount = 0;
void *pDataI;
void *pDataJ;
for (int i = 0; i < pLinkList->length; i ++)
{
pDataI = GetDataPointerOfNodeInLinkListByPos(pLinkList,i);
for (int j = i + 1; j < pLinkList->length; j ++)
{
pDataJ = GetDataPointerOfNodeInLinkListByPos(pLinkList,j);
if (compareCallBack(pDataI,pDataJ) == 0)
{
*(ppData + sameCount) = pDataJ;
sameCount ++;
}
}
}
for (int i = 0; i < sameCount; i ++)
{
DeleteNodeOFLinkListByValue(pLinkList,*(ppData + i),compareCallBack);
}
free(ppData);
}
void ReverseLinkList(struct LinkList*pLinkList)
{
//思路:顺序删除,逆序插入
if (pLinkList == NULL)
{
return;
}
if (pLinkList->length < 2)
{
return;
}
int num = pLinkList->length;
void **ppData = (void *)malloc(sizeof(void *) * num);
for (int i = 0; i < num; i ++)
{
*(ppData + i) = GetDataPointerOfNodeInLinkListByPos(pLinkList,i);
}
for (int i = 0; i < num; i ++)
{
DeleteLastNodeOfLinkList(pLinkList);
}
for (int i = 0; i < num; i ++)
{
InsertToLinkListTail(pLinkList,*(ppData + num - i - 1));
}
}
3 main.c(测试用)
#include
#include "linklist1.h"
struct person
{
char name[64];
int age;
};
void PrintCallback(void* data)
{
struct person* Person_Pointer;
Person_Pointer = (struct person*)data;
printf("name:%s,age:%d\n", Person_Pointer->name, Person_Pointer->age);
}
int CompareCallback(void* data1, void* data2)
{
struct person* p1, * p2;
p1 = (struct person*)data1;
p2 = (struct person*)data2;
if (p1->age == p2->age)
{
if (strcmp(p1->name, p2->name) == 0)
{
return(0);
}
else
{
return(1);
}
}
else
{
return(1);
}
}
//插入及遍历测试
void Test0(void)
{
struct LinkList* pLinkList;
pLinkList=CreatLinkList();
struct person p1 = { "qqq",20 };
struct person p2 = { "www",19 };
struct person p3 = { "eee",18 };
struct person p4 = { "rrr",17 };
struct person p5 = { "qqq",20 };
InsertToLinlkListByPos(pLinkList, 0, &p1);
InsertToLinlkListByPos(pLinkList, 1, &p2);
InsertToLinlkListByPos(pLinkList, 2, &p3);
InsertToLinlkListByPos(pLinkList, 3, &p4);
InsertToLinlkListByPos(pLinkList, 4, &p5);
TraverseLinkList(pLinkList,PrintCallback);
}
//按位置删除测试
void Test1(void)
{
struct LinkList* pLinkList;
pLinkList=CreatLinkList();
struct person p1 = { "qqq",20 };
struct person p2 = { "www",19 };
struct person p3 = { "eee",18 };
struct person p4 = { "rrr",17 };
struct person p5 = { "qqq",20 };
InsertToLinlkListByPos(pLinkList, 0, &p1);
InsertToLinlkListByPos(pLinkList, 1, &p2);
InsertToLinlkListByPos(pLinkList, 2, &p3);
InsertToLinlkListByPos(pLinkList, 3, &p4);
InsertToLinlkListByPos(pLinkList, 4, &p5);
DeleteNodeOFLinkListByPosition(pLinkList,2);
TraverseLinkList(pLinkList,PrintCallback);
}
//按数据指针删除链表内节点测试
void Test2(void)
{
struct LinkList* pLinkList;
pLinkList=CreatLinkList();
struct person team[] = { "qqq",20,"www",19,"eee",18,"rrr",17,"qqq",20 };
for (int i = 0; i < 5; i ++)
{
InsertToLinkListTail(pLinkList,&team[i]);
}
//TraverseLinkList(pLinkList,PrintCallback);
struct person p = {"qqq",20};
DeleteNodeOFLinkListByValue(pLinkList,&p,CompareCallback);
TraverseLinkList(pLinkList,PrintCallback);
}
//按位置获取链表节点的数据指针测试
void Test3(void)
{
struct LinkList* pLinkList;
pLinkList=CreatLinkList();
struct person team[] = { "qqq",20,"www",19,"eee",18,"rrr",17,"qqq",20 };
for (int i = 0; i < 5; i ++)
{
InsertToLinkListTail(pLinkList,&team[i]);
}
struct person *p;
p = GetDataPointerOfNodeInLinkListByPos(pLinkList,0);
printf("age = %d,name = %s\n",p->age,p->name);
}
//链表去重测试
void Test4(void)
{
struct LinkList* pLinkList;
pLinkList=CreatLinkList();
struct person team[] = { "qqq",20,"www",19,"eee",18,"rrr",17,"qqq",20 };
for (int i = 0; i < 5; i ++)
{
InsertToLinkListTail(pLinkList,&team[i]);
}
QuCHongLiskList(pLinkList,CompareCallback);
TraverseLinkList(pLinkList,PrintCallback);
}
//链表逆序测试
void Test5(void)
{
struct LinkList* pLinkList;
pLinkList=CreatLinkList();
struct person team[] = { "qqq",20,"www",19,"eee",18,"rrr",17,"qqq",20 };
for (int i = 0; i < 5; i ++)
{
InsertToLinkListTail(pLinkList,&team[i]);
}
ReverseLinkList(pLinkList);
TraverseLinkList(pLinkList,PrintCallback);
}
int main(void)
{
Test5();
getchar();
return(0);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)