一、测试环境
名称 | 值 |
---|---|
cpu | 12th Gen Intel® Core™ i7-12700H |
操作系统 | CentOS Linux release 7.9.2009 (Core) |
内存 | 3G |
逻辑核数 | 2 |
gcc 版本 | 4.8.5 20150623 |
二、个人理解
1、循环链表优点
无论指针指向哪个节点,都可以访问任何一个其他节点。
2、尾指针循环链表优点
同上,加上一个优点:访问尾节点的时间复杂度为O(1)。
3、两个尾指针循环链表合并
合并尾指针循环链表L,L1步骤:
(1)让L的指针域指向L1的首元节点。
(2)L1的指针域指向L的头节点。
(3)删除L1的头节点。
突然发现自己的画画水平还可以,嘿嘿。
三、函数介绍
线性表之链式表的相关函数包括之前篇章《数据结构与算法基础-学习-05-线性表之链式表-删除元素、头插法创建单链表、尾插法创建单链表等实现》写的,都以此最新的篇幅为准。
1、CreateCycleLinkList
(1)用途
创建循环链表。
(2)函数定义
Status CreateCycleLinkList(LinkList *L, int LinkListLen)
{
JudgePointerNull(L);
if(LinkListLen < 1)
{
printf("Cycle Link List Len Need >= 1\n");
return FailFlag;
}
printf("Create Empty Cycle Link List Len : %d\n",LinkListLen);
int i;
InitLinkList(L);
ElemType *CurNode = L->ElemArray;
for(i=1; i<=LinkListLen; i++)
{
ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(InsertElem);
CurNode->NextPointer = InsertElem;
CurNode = InsertElem;
(L->ElemArrayLen)++;
}
CurNode->NextPointer = L->ElemArray;
printf("Create Empty Cycle Link List Success\n");
PrintPretty();
return SuccessFlag;
}
(3)参数说明
参数名 | 说明 |
---|---|
L | 需要创建的循环链表指针。 |
LinkListLen | 初始化的数据节点长度。 |
2、CreateCycleTailLinkList
(1)用途
创建尾指针循环链表。
(2)函数定义
Status CreateCycleTailLinkList(LinkList *L, int LinkListLen)
{
JudgePointerNull(L);
if(LinkListLen < 1)
{
printf("Cycle Tail Link List Len Need >= 1\n");
return FailFlag;
}
printf("Create Empty Cycle Tail Link List Len : %d\n",LinkListLen);
int i;
InitLinkList(L);
ElemType *CurNode = L->ElemArray;
for(i=1; i<=LinkListLen; i++)
{
ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(InsertElem);
CurNode->NextPointer = InsertElem;
CurNode = InsertElem;
(L->ElemArrayLen)++;
}
CurNode->NextPointer = L->ElemArray;
L->ElemArray = CurNode;
L->ElemArrayLen = LinkListLen;
printf("Create Empty Cycle Tail Link List Success\n");
PrintPretty();
return SuccessFlag;
}
(3)参数说明
参数名 | 说明 |
---|---|
L | 需要创建的尾指针循环链表指针。 |
LinkListLen | 初始化的数据节点长度。 |
3、UpdateCycleTailLinkListElem
(1)用途
更新尾指针循环链表数据节点。
(2)函数定义
Status UpdateCycleTailLinkListElem(LinkList *L, int Position, ElemType UpdateElem)
{
JudgePointerNull(L);
if(Position < 1 || Position > L->ElemArrayLen)
{
printf("Position Need >= 1 and <= %d\n",L->ElemArrayLen);
return FailFlag;
}
if(Position == L->ElemArrayLen)
{
CopyElemTypeDataNoNextP(L->ElemArray, &UpdateElem);
printf("Update Cycle Tail Link List Elem Success\n");
return SuccessFlag;
}
int i;
ElemType *TmpNode = L->ElemArray->NextPointer;
for(i=1; i<= Position; i++)
{
TmpNode = TmpNode->NextPointer;
}
CopyElemTypeDataNoNextP(TmpNode, &UpdateElem);
printf("Update Cycle Tail Link List Elem Success\n");
PrintPretty();
return SuccessFlag;
}
(3)参数说明
参数名 | 说明 |
---|---|
L | 需要更新尾指针循环链表指针。 |
Position | 需要更新尾指针循环链表的数据位置。 |
UpdateElem | 需要更新的数据。 |
4、Connect2CycleTailLinkList
(1)用途
两个尾指针循环链表连接。
(2)函数定义
LinkList *Connect2CycleTailLinkList(LinkList *L1, LinkList *L2)
{
JudgePointerNull(L1);
JudgePointerNull(L2);
printf("Connect 2 Cycle Tail Link List ");
LinkList *NewCTLinkList = (LinkList *)MyMalloc(sizeof(LinkList));
//NewCTLinkList->ElemArray = (ElemType *)MyMalloc(sizeof(ElemType));
NewCTLinkList->ElemArrayLen = (L1->ElemArrayLen) + (L2->ElemArrayLen);
NewCTLinkList->ElemArray = L2->ElemArray;
ElemType *L1Head = L1->ElemArray->NextPointer;
ElemType *L2Head = L2->ElemArray->NextPointer;
L2->ElemArray->NextPointer = L1Head;
L1->ElemArray->NextPointer = L2Head->NextPointer;
free(L2Head);
L1->ElemArray = NULL;
L2->ElemArray = NULL;
L1->ElemArrayLen = 0;
L2->ElemArrayLen = 0;
printf("Success \n");
PrintPretty();
return NewCTLinkList;
}
(3)参数说明
参数名 | 说明 |
---|---|
L1 | 需要连接的尾指针循环链表指针。 |
L 2 | 需要连接的尾指针循环链表指针。 |
四、测试代码
1、LinearTable_LinkList.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include "LinearTable_LinkList.h"
void PrintPretty()
{
printf("*********************************\n");
}
void PrintPretty_V1()
{
printf("################\n");
}
void *MyMalloc(size_t size)
{
void *Result = (void *)malloc(size);
if(Result == NULL)
{
printf("malloc Function Exec Fail , Out Of Memory ,Exit!!!\n");
exit(ExceptionExitFlag);
}
return Result;
}
void JudgePointerNull(LinkList *L)
{
if(!L)
{
printf("Pointer Is Null ,Exit !\n");
exit(ExceptionExitFlag);
}
}
void JudgeElemTypePointerIsNull(ElemType *E)
{
if(!E)
{
printf("Pointer Is Null ,Exit !\n");
exit(ExceptionExitFlag);
}
}
Status InitElemType(ElemType *E)
{
JudgeElemTypePointerIsNull(E);
memset(E->StudentNum , '\0', sizeof(char) * StudentNumLen);
memset(E->StudentName, '\0', sizeof(char) * StudentNameLen);
E->StudentScore = 0;
E->NextPointer = NULL;
//printf("Init ElemType Success\n");
//PrintPretty();
return SuccessFlag;
}
Status InitLinkList(LinkList *L)
{
JudgePointerNull(L);
//L = (LinkList *)MyMalloc(sizeof(LinkList));
L->ElemArray = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(L->ElemArray);
L->ElemArrayLen = 0;
printf("Init LinkList Success\n");
PrintPretty();
return SuccessFlag;
}
void PrintLinkList(LinkList *L)
{
printf("Print Link List\n");
JudgePointerNull(L);
printf("Link List Pointer : %p\n",L);
PrintPretty_V1();
ElemType *p = L->ElemArray;
while(p)
{
printf("CurrentPointer : %p\n",p);
printf("StudentNum : %s\n",p->StudentNum);
printf("StudentName : %s\n",p->StudentName);
printf("StudentScore : %d\n",p->StudentScore);
printf("NextPointer : %p\n",p->NextPointer);
PrintPretty_V1();
p = p->NextPointer;
if(p == L->ElemArray)
{
printf("Cycle Link List Printf End\n");
break;
}
}
printf("ElemArrayLen : %d\n",L->ElemArrayLen);
PrintPretty();
}
void PrintElemType(ElemType *E)
{
printf("Print Elem Type\n");
JudgeElemTypePointerIsNull(E);
printf("StudentNum : %s\n",E->StudentNum);
printf("StudentName : %s\n",E->StudentName);
printf("StudentScore : %d\n",E->StudentScore);
printf("NextPointer : %p\n",E->NextPointer);
PrintPretty();
}
int JudgeLinkListIsEmpty(LinkList *L)
{
JudgePointerNull(L);
if(L->ElemArrayLen == 0)
{
printf("Link List Is Empty\n");
PrintPretty();
return SuccessFlag;
}
else
{
printf("Link List Is Not Empty\n");
PrintPretty();
return FailFlag;
}
}
void DestroyLinkList(LinkList *L)
{
printf("Start Destroy Link List\n");
JudgePointerNull(L);
ElemType *p = L->ElemArray;
ElemType *q = NULL;
while(p)
{
q = p->NextPointer;
free(p);
//printf("Free Pointer : %p\n",p);
p = q;
if(p == L->ElemArray)
{
printf("Cycle Link List ");
break;
}
}
L->ElemArrayLen = 0;
free(L);
//printf("Free Link List Pointer: %p\n",L);
L = NULL;
printf("Destroy Success !!!\n");
PrintPretty();
}
Status ClearLinkListElem(LinkList *L)
{
printf("Start Clear Link List Elem\n");
JudgePointerNull(L);
L->ElemArrayLen = 0;
ElemType *p = L->ElemArray->NextPointer;
ElemType *q = NULL;
while(p)
{
q = p->NextPointer;
//printf("Free Pointer : %p\n",p);
free(p);
p = q;
}
L->ElemArray->NextPointer = NULL;
printf("Clear Link List Elem Success !!!\n");
PrintPretty();
return SuccessFlag;
}
int GetLinkListLen(LinkList *L)
{
JudgePointerNull(L);
printf("Get Link List Len : %d\n",L->ElemArrayLen);
PrintPretty();
return L->ElemArrayLen;
}
Status CopyElemTypeData(ElemType *Dest, ElemType *Src)
{
strcpy(Dest->StudentNum,Src->StudentNum);
strcpy(Dest->StudentName,Src->StudentName);
Dest->StudentScore = Src->StudentScore;
Dest->NextPointer = Src->NextPointer;
return SuccessFlag;
}
Status CopyElemTypeDataNoNextP(ElemType *Dest, ElemType *Src)
{
strcpy(Dest->StudentNum,Src->StudentNum);
strcpy(Dest->StudentName,Src->StudentName);
Dest->StudentScore = Src->StudentScore;
return SuccessFlag;
}
int GetLinkListElem(LinkList *L, int ElemPosition, ElemType *VarElem)
{
JudgePointerNull(L);
if(ElemPosition < 1 || ElemPosition > L->ElemArrayLen)
{
printf("Error ElemPosition : %d, Need 1 <= ElemPosition <= ElemArrayLen(%d)\n",ElemPosition,L->ElemArrayLen);
PrintPretty();
return FailFlag;
}
int i;
ElemType *p = L->ElemArray;
for(i=1; i<=ElemPosition; i++)
{
p = p->NextPointer;
}
CopyElemTypeData(VarElem,p);
//PrintElemType(VarElem);
printf("ElemPosition : %d ,Get Data Success\n",ElemPosition);
PrintPretty();
return SuccessFlag;
}
Status CmpElemType(ElemType *E1, ElemType *E2)
{
//printf("Compare Struct ElemType\n");
JudgeElemTypePointerIsNull(E1);
JudgeElemTypePointerIsNull(E2);
if(strcmp(E1->StudentNum,E2->StudentNum) != 0)
{
return FailFlag;
}
if(strcmp(E1->StudentName,E2->StudentName) != 0)
{
return FailFlag;
}
if((E1->StudentScore) != (E2->StudentScore))
{
return FailFlag;
}
return SuccessFlag;
}
ElemType * LocateElemPointer(LinkList *L, ElemType E)
{
printf("Locate Element Pointer\n");
JudgePointerNull(L);
ElemType *p = L->ElemArray->NextPointer;
int i = 0;
while(p)
{
if(CmpElemType(p,&E) == SuccessFlag)
{
i++;
printf("Pointer : %p\n",p);
printf("Locate Link List Elem Success\n");
PrintPretty();
return p;
}
p = p->NextPointer;
i++;
}
if(i != L->ElemArrayLen)
{
printf("ElemArrayLen != Actual Link List Len, Exception Exit!!!\n");
exit(ExceptionExitFlag);
}
printf("Locate Link List Elem Fail!!!\n");
PrintPretty();
return NULL;
}
int LocateElemPosition(LinkList *L, ElemType E)
{
printf("Locate Element Position\n");
JudgePointerNull(L);
ElemType *p = L->ElemArray->NextPointer;
int i = 0;
while(p)
{
if(CmpElemType(p,&E) == SuccessFlag)
{
i++;
printf("Position : %d\n",i);
printf("Locate Link List Elem Success\n");
PrintPretty();
return i;
}
p = p->NextPointer;
i++;
}
if(i != L->ElemArrayLen)
{
printf("ElemArrayLen != Actual Link List Len, Exception Exit!!!\n");
exit(ExceptionExitFlag);
}
printf("Locate Link List Elem Fail!!!\n");
PrintPretty();
return 0;
}
//Status InsertLinkListElem(LinkList *L, int ElemPosition, ElemType *VarElem)
//在插入数据时,不能直接用传入参数VarElem对链表L进行指针赋值,也就是注释掉的两行,会导致后续ClearLinkListElem执行时,提示:double free or corruption (out)。
//ClearLinkListElem代码中并没有多次free,改为注释两行上面的五行,问题得到解决。
Status InsertLinkListElem(LinkList *L, int ElemPosition, ElemType VarElem)
{
JudgePointerNull(L);
//printf("ElemPosition : %d\n",ElemPosition);
//PrintElemType(VarElem);
if(ElemPosition < 1 || ElemPosition > L->ElemArrayLen + 1)
{
printf("Error ,Need 1 <= ElemPosition <= %d\n",L->ElemArrayLen + 1);
PrintPretty();
return FailFlag;
}
int i = 0;
ElemType *p = L->ElemArray;
while(p)
{
if(i == ElemPosition - 1)
{
break;
}
p = p->NextPointer;
i++;
}
ElemType *TmpElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(TmpElem);
CopyElemTypeData(TmpElem,&VarElem);
TmpElem->NextPointer = p->NextPointer;
p->NextPointer = TmpElem;
//(VarElem->NextPointer) = (p->NextPointer);
//p->NextPointer = VarElem;
L->ElemArrayLen++;
//printf("Insert Data Success\n");
//PrintPretty();
return SuccessFlag;
}
Status DeleteLinkListElem(LinkList *L, int ElemPosition, ElemType *DelVarElem)
{
JudgePointerNull(L);
printf("ElemPosition : %d\n",ElemPosition);
if(ElemPosition < 1 || ElemPosition > L->ElemArrayLen)
{
printf("Error ,Need 1 <= ElemPosition <= %d\n",L->ElemArrayLen);
PrintPretty();
return FailFlag;
}
int i = 0;
ElemType *p = L->ElemArray;
while(p)
{
if(i == ElemPosition - 1)
{
break;
}
p = p->NextPointer;
i++;
}
ElemType *TmpDelElem = p->NextPointer;
CopyElemTypeData(DelVarElem,TmpDelElem);
//DelVarElem = TmpDelElem; //虽然这样方便,但怕出现问题,就用上面的方法把。
//PrintElemType(DelVarElem);
p->NextPointer = TmpDelElem->NextPointer;
free(TmpDelElem);
TmpDelElem = NULL;
L->ElemArrayLen--;
printf("Delete Data Success\n");
PrintPretty();
return SuccessFlag;
}
Status CreateHeadLinkList(LinkList *L, int LinkListLen)
{
JudgePointerNull(L);
if(LinkListLen < 1)
{
printf("LinkListLen Need >= 1\n");
return FailFlag;
}
printf("Create Empty Head Insert Link List Len : %d\n",LinkListLen);
int i;
InitLinkList(L);
ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(InsertElem);
for(i=1; i<=LinkListLen; i++)
{
//ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InsertLinkListElem(L, 1, *InsertElem);
//free(InsertElem);
//InsertElem = NULL;
}
free(InsertElem);
InsertElem = NULL;
printf("Create Empty Head Insert Link List Success\n");
PrintPretty();
return SuccessFlag;
}
Status CreateHeadLinkList_V1(LinkList *L, int LinkListLen)
{
JudgePointerNull(L);
if(LinkListLen < 1)
{
printf("LinkListLen Need >= 1\n");
return FailFlag;
}
printf("Create Empty Head Insert Link List Len : %d\n",LinkListLen);
int i;
InitLinkList(L);
for(i=1; i<=LinkListLen; i++)
{
ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(InsertElem);
InsertElem->NextPointer = L->ElemArray->NextPointer;
L->ElemArray->NextPointer = InsertElem;
(L->ElemArrayLen)++;
}
printf("Create Empty Head Insert Link List Success\n");
PrintPretty();
return SuccessFlag;
}
void ComputeProcedureTime(Status Func(LinkList *, int), LinkList *L, int LinkListLen)
{
printf("========================================\n");
struct timeval start,end;
unsigned long TotalTime;
gettimeofday(&start, NULL);
Func(L, LinkListLen);
gettimeofday(&end, NULL);
TotalTime = end.tv_sec - start.tv_sec;
printf("Elapsed Time : %ld s\n",TotalTime);
}
Status CreateTailLinkList(LinkList *L, int LinkListLen)
{
JudgePointerNull(L);
if(LinkListLen < 1)
{
printf("LinkListLen Need >= 1\n");
return FailFlag;
}
printf("Create Empty Tail Insert Link List Len : %d\n",LinkListLen);
int i;
InitLinkList(L);
ElemType *CurNode = L->ElemArray;
for(i=1; i<=LinkListLen; i++)
{
ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(InsertElem);
CurNode->NextPointer = InsertElem;
CurNode = InsertElem;
(L->ElemArrayLen)++;
}
printf("Create Empty Tail Insert Link List Success\n");
PrintPretty();
return SuccessFlag;
}
Status CreateCycleLinkList(LinkList *L, int LinkListLen)
{
JudgePointerNull(L);
if(LinkListLen < 1)
{
printf("Cycle Link List Len Need >= 1\n");
return FailFlag;
}
printf("Create Empty Cycle Link List Len : %d\n",LinkListLen);
int i;
InitLinkList(L);
ElemType *CurNode = L->ElemArray;
for(i=1; i<=LinkListLen; i++)
{
ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(InsertElem);
CurNode->NextPointer = InsertElem;
CurNode = InsertElem;
(L->ElemArrayLen)++;
}
CurNode->NextPointer = L->ElemArray;
printf("Create Empty Cycle Link List Success\n");
PrintPretty();
return SuccessFlag;
}
Status CreateCycleTailLinkList(LinkList *L, int LinkListLen)
{
JudgePointerNull(L);
if(LinkListLen < 1)
{
printf("Cycle Tail Link List Len Need >= 1\n");
return FailFlag;
}
printf("Create Empty Cycle Tail Link List Len : %d\n",LinkListLen);
int i;
InitLinkList(L);
ElemType *CurNode = L->ElemArray;
for(i=1; i<=LinkListLen; i++)
{
ElemType *InsertElem = (ElemType *)MyMalloc(sizeof(ElemType));
InitElemType(InsertElem);
CurNode->NextPointer = InsertElem;
CurNode = InsertElem;
(L->ElemArrayLen)++;
}
CurNode->NextPointer = L->ElemArray;
L->ElemArray = CurNode;
L->ElemArrayLen = LinkListLen;
printf("Create Empty Cycle Tail Link List Success\n");
PrintPretty();
return SuccessFlag;
}
Status UpdateCycleTailLinkListElem(LinkList *L, int Position, ElemType UpdateElem)
{
JudgePointerNull(L);
if(Position < 1 || Position > L->ElemArrayLen)
{
printf("Position Need >= 1 and <= %d\n",L->ElemArrayLen);
return FailFlag;
}
if(Position == L->ElemArrayLen)
{
CopyElemTypeDataNoNextP(L->ElemArray, &UpdateElem);
printf("Update Cycle Tail Link List Elem Success\n");
return SuccessFlag;
}
int i;
ElemType *TmpNode = L->ElemArray->NextPointer;
for(i=1; i<= Position; i++)
{
TmpNode = TmpNode->NextPointer;
}
CopyElemTypeDataNoNextP(TmpNode, &UpdateElem);
printf("Update Cycle Tail Link List Elem Success\n");
PrintPretty();
return SuccessFlag;
}
LinkList *Connect2CycleTailLinkList(LinkList *L1, LinkList *L2)
{
JudgePointerNull(L1);
JudgePointerNull(L2);
printf("Connect 2 Cycle Tail Link List ");
LinkList *NewCTLinkList = (LinkList *)MyMalloc(sizeof(LinkList));
//NewCTLinkList->ElemArray = (ElemType *)MyMalloc(sizeof(ElemType));
NewCTLinkList->ElemArrayLen = (L1->ElemArrayLen) + (L2->ElemArrayLen);
NewCTLinkList->ElemArray = L2->ElemArray;
ElemType *L1Head = L1->ElemArray->NextPointer;
ElemType *L2Head = L2->ElemArray->NextPointer;
L2->ElemArray->NextPointer = L1Head;
L1->ElemArray->NextPointer = L2Head->NextPointer;
free(L2Head);
L1->ElemArray = NULL;
L2->ElemArray = NULL;
L1->ElemArrayLen = 0;
L2->ElemArrayLen = 0;
printf("Success \n");
PrintPretty();
return NewCTLinkList;
}
2、LinearTable_LinkList.h
#ifndef LinearTable_LinkList_H
#define LinearTable_LinkList_H
#define InsertDataArrayLen 6
#define ExceptionExitFlag -1
#define SuccessFlag 1
#define FailFlag 0
#define StudentNumLen 8
#define StudentNameLen 8
typedef int Status;
typedef struct ElemType
{
char StudentNum[StudentNumLen];
char StudentName[StudentNameLen];
int StudentScore;
struct ElemType *NextPointer;
}ElemType;
typedef struct
{
ElemType *ElemArray;
int ElemArrayLen;
}LinkList;
void *MyMalloc(size_t size);
Status InitElemType(ElemType *E);
Status InitLinkList(LinkList *L);
void JudgePointerNull(LinkList *L);
void JudgeElemTypePointerIsNull(ElemType *E);
void PrintLinkList(LinkList *L);
int JudgeLinkListIsEmpty(LinkList *L);
void DestroyLinkList(LinkList *L);
Status ClearLinkListElem(LinkList *L);
int GetLinkListLen(LinkList *L);
Status CopyElemTypeData(ElemType *Dest, ElemType *Src);
Status CopyElemTypeDataNoNextP(ElemType *Dest, ElemType *Src);
int GetLinkListElem(LinkList *L, int ElemPosition, ElemType *VarElem);
void PrintElemType(ElemType *E);
Status CmpElemType(ElemType *E1, ElemType *E2);
ElemType * LocateElemPointer(LinkList *L, ElemType E);
int LocateElemPosition(LinkList *L, ElemType E);
Status InsertLinkListElem(LinkList *L, int ElemPosition, ElemType VarElem);
Status DeleteLinkListElem(LinkList *L, int ElemPosition, ElemType *DelVarElem);
Status CreateHeadLinkList(LinkList *L, int LinkListLen);
Status CreateHeadLinkList_V1(LinkList *L, int LinkListLen);
Status CreateTailLinkList(LinkList *L, int LinkListLen);
Status CreateCycleLinkList(LinkList *L, int LinkListLen);
Status CreateCycleTailLinkList(LinkList *L, int LinkListLen);
Status UpdateCycleTailLinkListElem(LinkList *L, int Position, ElemType UpdateElem);
LinkList *Connect2CycleTailLinkList(LinkList *L1, LinkList *L2);
void ComputeProcedureTime(Status Func(LinkList *, int), LinkList *L, int LinkListLen);
void PrintPretty();
void PrintPretty_V1();
#endif
3、main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LinearTable_LinkList.h"
int main()
{
//测试函数
LinkList *L = (LinkList *)MyMalloc(sizeof(LinkList));
ElemType *VarElem = (ElemType *)MyMalloc(sizeof(ElemType));
ElemType *InsertElemArray = (ElemType *)MyMalloc(sizeof(ElemType) * InsertDataArrayLen);
ElemType *DelVarElem = (ElemType *)MyMalloc(sizeof(ElemType));
int i;
for(i=0; i<InsertDataArrayLen; i++)
{
InitElemType(&(InsertElemArray[i]));
strcpy(InsertElemArray[i].StudentNum ,"X666");
strcpy(InsertElemArray[i].StudentName,"Sun");
InsertElemArray[i].StudentScore = i + 100;
}
InitLinkList(L);
//PrintLinkList(L);
for(i=0; i<InsertDataArrayLen; i++)
{
InsertLinkListElem(L, 1, InsertElemArray[i]);
}
PrintLinkList(L);
GetLinkListElem(L, InsertDataArrayLen - 1, VarElem);
//PrintElemType(VarElem);
LocateElemPointer(L,*VarElem);
LocateElemPosition(L,*VarElem);
//PrintElemType(VarElem);
JudgeLinkListIsEmpty(L);
DeleteLinkListElem(L, InsertDataArrayLen, DelVarElem);
DeleteLinkListElem(L, InsertDataArrayLen / 2, DelVarElem);
DeleteLinkListElem(L, 1, DelVarElem);
PrintLinkList(L);
GetLinkListLen(L);
ClearLinkListElem(L);
//PrintLinkList(L);
DestroyLinkList(L);
free(VarElem);
//free(InsertElemArray);
free(DelVarElem);
L = NULL;
VarElem = NULL;
//InsertElemArray = NULL;
DelVarElem = NULL;
//测试单链表建立-头插法,尾插法
int LinkListLen = InsertDataArrayLen;
LinkList *H = (LinkList *)MyMalloc(sizeof(LinkList));
ComputeProcedureTime(CreateHeadLinkList,H, LinkListLen);
//PrintLinkList(H);
DestroyLinkList(H);
H = NULL;
LinkList *H1 = (LinkList *)MyMalloc(sizeof(LinkList));
ComputeProcedureTime(CreateHeadLinkList_V1,H1, LinkListLen);
//PrintLinkList(H1);
DestroyLinkList(H1);
H1 = NULL;
LinkList *H2 = (LinkList *)MyMalloc(sizeof(LinkList));
ComputeProcedureTime(CreateTailLinkList,H2, LinkListLen);
//PrintLinkList(H2);
DestroyLinkList(H2);
H2 = NULL;
//测试单链表建立-循环链表返回头指针
//循环链表只改造了PrintLinkList和DestroyLinkList,其他方法不一定适用。
LinkList *C = (LinkList *)MyMalloc(sizeof(LinkList));
CreateCycleLinkList(C, LinkListLen);
PrintLinkList(C);
DestroyLinkList(C);
C = NULL;
//测试单链表建立-尾指针循环链表
//循环链表只改造了PrintLinkList和DestroyLinkList,其他方法不一定适用。
LinkList *CT = (LinkList *)MyMalloc(sizeof(LinkList));
CreateCycleTailLinkList(CT, LinkListLen);
for(i=0; i<LinkListLen; i++)
{
UpdateCycleTailLinkListElem(CT, i+1, InsertElemArray[i]);
}
PrintLinkList(CT);
LinkList *CT1 = (LinkList *)MyMalloc(sizeof(LinkList));
CreateCycleTailLinkList(CT1, LinkListLen);
for(i=LinkListLen-1; i>=0; i--)
{
UpdateCycleTailLinkListElem(CT1, i+1, InsertElemArray[i]);
}
PrintLinkList(CT1);
//尾指针循环链表-两表链接
LinkList *CT2 = Connect2CycleTailLinkList(CT,CT1);
PrintLinkList(CT2);
DestroyLinkList(CT1);
CT1 = NULL;
DestroyLinkList(CT);
CT = NULL;
DestroyLinkList(CT2);
CT2 = NULL;
free(InsertElemArray);
InsertElemArray = NULL;
return SuccessFlag;
}
4、makefile
CC = gcc
CFLAG_EXEC = -Wall -O3
CFLAG_ALIAS = -o
RM_COMM = rm -rf
all : main
main :
$(CC) $(CFLAG_EXEC) LinearTable_LinkList.c main.c $(CFLAG_ALIAS) Test_LinearTable_LinkList
clean :
$(RM_COMM) Test_LinearTable_LinkList
五、编译测试
[gbase@czg2 LinearTable_LinkList]$ make clean
rm -rf Test_LinearTable_LinkList
[gbase@czg2 LinearTable_LinkList]$ make
gcc -Wall -O3 LinearTable_LinkList.c main.c -o Test_LinearTable_LinkList
[gbase@czg2 LinearTable_LinkList]$ ./Test_LinearTable_LinkList
Init LinkList Success
*********************************
Print Link List
Link List Pointer : 0xf8b010
################
CurrentPointer : 0xf8b160
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b280
################
CurrentPointer : 0xf8b280
StudentNum : X666
StudentName : Sun
StudentScore : 105
NextPointer : 0xf8b250
################
CurrentPointer : 0xf8b250
StudentNum : X666
StudentName : Sun
StudentScore : 104
NextPointer : 0xf8b220
################
CurrentPointer : 0xf8b220
StudentNum : X666
StudentName : Sun
StudentScore : 103
NextPointer : 0xf8b1f0
################
CurrentPointer : 0xf8b1f0
StudentNum : X666
StudentName : Sun
StudentScore : 102
NextPointer : 0xf8b1c0
################
CurrentPointer : 0xf8b1c0
StudentNum : X666
StudentName : Sun
StudentScore : 101
NextPointer : 0xf8b190
################
CurrentPointer : 0xf8b190
StudentNum : X666
StudentName : Sun
StudentScore : 100
NextPointer : (nil)
################
ElemArrayLen : 6
*********************************
ElemPosition : 5 ,Get Data Success
*********************************
Locate Element Pointer
Pointer : 0xf8b1c0
Locate Link List Elem Success
*********************************
Locate Element Position
Position : 5
Locate Link List Elem Success
*********************************
Link List Is Not Empty
*********************************
ElemPosition : 6
Delete Data Success
*********************************
ElemPosition : 3
Delete Data Success
*********************************
ElemPosition : 1
Delete Data Success
*********************************
Print Link List
Link List Pointer : 0xf8b010
################
CurrentPointer : 0xf8b160
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b250
################
CurrentPointer : 0xf8b250
StudentNum : X666
StudentName : Sun
StudentScore : 104
NextPointer : 0xf8b1f0
################
CurrentPointer : 0xf8b1f0
StudentNum : X666
StudentName : Sun
StudentScore : 102
NextPointer : 0xf8b1c0
################
CurrentPointer : 0xf8b1c0
StudentNum : X666
StudentName : Sun
StudentScore : 101
NextPointer : (nil)
################
ElemArrayLen : 3
*********************************
Get Link List Len : 3
*********************************
Start Clear Link List Elem
Clear Link List Elem Success !!!
*********************************
Start Destroy Link List
Destroy Success !!!
*********************************
========================================
Create Empty Head Insert Link List Len : 6
Init LinkList Success
*********************************
Create Empty Head Insert Link List Success
*********************************
Elapsed Time : 0 s
Start Destroy Link List
Destroy Success !!!
*********************************
========================================
Create Empty Head Insert Link List Len : 6
Init LinkList Success
*********************************
Create Empty Head Insert Link List Success
*********************************
Elapsed Time : 0 s
Start Destroy Link List
Destroy Success !!!
*********************************
========================================
Create Empty Tail Insert Link List Len : 6
Init LinkList Success
*********************************
Create Empty Tail Insert Link List Success
*********************************
Elapsed Time : 0 s
Start Destroy Link List
Destroy Success !!!
*********************************
Create Empty Cycle Link List Len : 6
Init LinkList Success
*********************************
Create Empty Cycle Link List Success
*********************************
Print Link List
Link List Pointer : 0xf8b010
################
CurrentPointer : 0xf8b160
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b130
################
CurrentPointer : 0xf8b130
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b220
################
CurrentPointer : 0xf8b220
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b280
################
CurrentPointer : 0xf8b280
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b250
################
CurrentPointer : 0xf8b250
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b1f0
################
CurrentPointer : 0xf8b1f0
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b1c0
################
CurrentPointer : 0xf8b1c0
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b160
################
Cycle Link List Printf End
ElemArrayLen : 6
*********************************
Start Destroy Link List
Cycle Link List Destroy Success !!!
*********************************
Create Empty Cycle Tail Link List Len : 6
Init LinkList Success
*********************************
Create Empty Cycle Tail Link List Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
Print Link List
Link List Pointer : 0xf8b010
################
CurrentPointer : 0xf8b160
StudentNum : X666
StudentName : Sun
StudentScore : 105
NextPointer : 0xf8b1c0
################
CurrentPointer : 0xf8b1c0
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b1f0
################
CurrentPointer : 0xf8b1f0
StudentNum : X666
StudentName : Sun
StudentScore : 100
NextPointer : 0xf8b250
################
CurrentPointer : 0xf8b250
StudentNum : X666
StudentName : Sun
StudentScore : 101
NextPointer : 0xf8b280
################
CurrentPointer : 0xf8b280
StudentNum : X666
StudentName : Sun
StudentScore : 102
NextPointer : 0xf8b220
################
CurrentPointer : 0xf8b220
StudentNum : X666
StudentName : Sun
StudentScore : 103
NextPointer : 0xf8b130
################
CurrentPointer : 0xf8b130
StudentNum : X666
StudentName : Sun
StudentScore : 104
NextPointer : 0xf8b160
################
Cycle Link List Printf End
ElemArrayLen : 6
*********************************
Create Empty Cycle Tail Link List Len : 6
Init LinkList Success
*********************************
Create Empty Cycle Tail Link List Success
*********************************
Update Cycle Tail Link List Elem Success
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Update Cycle Tail Link List Elem Success
*********************************
Print Link List
Link List Pointer : 0xf8b2b0
################
CurrentPointer : 0xf8b390
StudentNum : X666
StudentName : Sun
StudentScore : 105
NextPointer : 0xf8b030
################
CurrentPointer : 0xf8b030
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b190
################
CurrentPointer : 0xf8b190
StudentNum : X666
StudentName : Sun
StudentScore : 100
NextPointer : 0xf8b2d0
################
CurrentPointer : 0xf8b2d0
StudentNum : X666
StudentName : Sun
StudentScore : 101
NextPointer : 0xf8b300
################
CurrentPointer : 0xf8b300
StudentNum : X666
StudentName : Sun
StudentScore : 102
NextPointer : 0xf8b330
################
CurrentPointer : 0xf8b330
StudentNum : X666
StudentName : Sun
StudentScore : 103
NextPointer : 0xf8b360
################
CurrentPointer : 0xf8b360
StudentNum : X666
StudentName : Sun
StudentScore : 104
NextPointer : 0xf8b390
################
Cycle Link List Printf End
ElemArrayLen : 6
*********************************
Connect 2 Cycle Tail Link List Success
*********************************
Print Link List
Link List Pointer : 0xf8b3c0
################
CurrentPointer : 0xf8b390
StudentNum : X666
StudentName : Sun
StudentScore : 105
NextPointer : 0xf8b1c0
################
CurrentPointer : 0xf8b1c0
StudentNum :
StudentName :
StudentScore : 0
NextPointer : 0xf8b1f0
################
CurrentPointer : 0xf8b1f0
StudentNum : X666
StudentName : Sun
StudentScore : 100
NextPointer : 0xf8b250
################
CurrentPointer : 0xf8b250
StudentNum : X666
StudentName : Sun
StudentScore : 101
NextPointer : 0xf8b280
################
CurrentPointer : 0xf8b280
StudentNum : X666
StudentName : Sun
StudentScore : 102
NextPointer : 0xf8b220
################
CurrentPointer : 0xf8b220
StudentNum : X666
StudentName : Sun
StudentScore : 103
NextPointer : 0xf8b130
################
CurrentPointer : 0xf8b130
StudentNum : X666
StudentName : Sun
StudentScore : 104
NextPointer : 0xf8b160
################
CurrentPointer : 0xf8b160
StudentNum : X666
StudentName : Sun
StudentScore : 105
NextPointer : 0xf8b190
################
CurrentPointer : 0xf8b190
StudentNum : X666
StudentName : Sun
StudentScore : 100
NextPointer : 0xf8b2d0
################
CurrentPointer : 0xf8b2d0
StudentNum : X666
StudentName : Sun
StudentScore : 101
NextPointer : 0xf8b300
################
CurrentPointer : 0xf8b300
StudentNum : X666
StudentName : Sun
StudentScore : 102
NextPointer : 0xf8b330
################
CurrentPointer : 0xf8b330
StudentNum : X666
StudentName : Sun
StudentScore : 103
NextPointer : 0xf8b360
################
CurrentPointer : 0xf8b360
StudentNum : X666
StudentName : Sun
StudentScore : 104
NextPointer : 0xf8b390
################
Cycle Link List Printf End
ElemArrayLen : 12
*********************************
Start Destroy Link List
Destroy Success !!!
*********************************
Start Destroy Link List
Destroy Success !!!
*********************************
Start Destroy Link List
Cycle Link List Destroy Success !!!
*********************************