一、测试环境
名称 | 值 |
---|---|
cpu | 12th Gen Intel® Core™ i7-12700H |
操作系统 | CentOS Linux release 7.9.2009 (Core) |
内存 | 3G |
逻辑核数 | 2 |
gcc 版本 | 4.8.5 20150623 |
二、个人理解
数据结构分为逻辑结构和物理结构(也称为存储结构)。
1、逻辑结构
逻辑结构又可以分为以下四种结构。
(1)集合结构
例如:{1,2,3,5,6},这里面的元素除了属于一个相同的集合外,各个元素之间没有任何联系。
(2)线性结构
例如:(a)-(b)-(c),a是b的“直接前驱”,b是a的“直接后继”。相邻元素之间是有一定的关系存在,且是一对一的。
(3)树形结构
结构中的各个元素之间存在一对多的关系,图不好画,bing一个,例如:
(4)图结构
结构中的数据元素是多对多的关系,图不好画,bing一个,例如:
2、物理结构
物理结构分为两种。
(1)顺序结构
顺序存储结构是把数据元素存放在一组地址连续的存储单元里,其数据元素间的逻辑关系和物理关系是一致的。
编写C代码时,用数组的方式实现。
(2)链式结构
链式存储结构是把数据元素存放在任意的存储单元里,这些存储单元可以是连续的,也可以是不连续的,数据元素的物理关系并不能反映其逻辑关系,因此需要借助指针来表示数据元素之间的逻辑关系。
编写C代码时,用链表的方式实现。
三、代码
1、LinearTable_OrderTable.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LinearTable_OrderTable.h"
void PrintPretty()
{
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(OrderTable *OrderTablePointer)
{
if(OrderTablePointer == NULL)
{
printf("Pointer Is Null ,Exit !\n");
exit(ExceptionExitFlag);
}
}
Status InitOrderTable(OrderTable *OrderTablePointer)
{
printf("Start Init List\n");
JudgePointerNull(OrderTablePointer);
OrderTablePointer->ElemArray = (ElemType *)MyMalloc(sizeof(ElemType) * ElemArrayMaxLen);
memset(OrderTablePointer->ElemArray, '\0', sizeof(ElemType) * ElemArrayMaxLen);
OrderTablePointer->ElemArrayLen = 0;
printf("Init List Success !!!\n");
PrintPretty();
return SuccessfulFlag;
}
void DestroyOrderTable(OrderTable *OrderTablePointer)
{
printf("Start Destroy List\n");
JudgePointerNull(OrderTablePointer);
free(OrderTablePointer->ElemArray);
OrderTablePointer->ElemArray = NULL;
free(OrderTablePointer);
OrderTablePointer = NULL;
printf("Destroy List Success !!!\n");
PrintPretty();
}
void PrintOrderTable(OrderTable *OrderTablePointer)
{
printf("Print List\n");
JudgePointerNull(OrderTablePointer);
int i;
printf("ElemArray : ");
for(i=0; i<ElemArrayMaxLen -1; i++)
{
printf("%c ,",OrderTablePointer->ElemArray[i]);
}
printf("%c \n",OrderTablePointer->ElemArray[i]);
printf("ElemArrayLen : %d\n",OrderTablePointer->ElemArrayLen);
PrintPretty();
}
void ClearOrderTable(OrderTable *OrderTablePointer)
{
printf("Clear Order Table\n");
JudgePointerNull(OrderTablePointer);
OrderTablePointer->ElemArrayLen = 0;
PrintPretty();
}
int GetOrderTableLen(OrderTable *OrderTablePointer)
{
JudgePointerNull(OrderTablePointer);
printf("Get ElemArray Len : %d\n",OrderTablePointer->ElemArrayLen);
PrintPretty();
return OrderTablePointer->ElemArrayLen;
}
int JudgeOrderTableIsEmpty(OrderTable *OrderTablePointer)
{
JudgePointerNull(OrderTablePointer);
if(OrderTablePointer->ElemArrayLen == 0)
{
printf("Order Table Is Empty\n");
PrintPretty();
return SuccessfulFlag;
}
else
{
printf("Order Table Is Not Empty\n");
PrintPretty();
return FailFlag;
}
}
int GetOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType *VarElem)
{
JudgePointerNull(OrderTablePointer);
if(ElemPosition < 1 || ElemPosition > OrderTablePointer->ElemArrayLen)
{
printf("Error ElemPosition : %d, Need 1 <= ElemPosition <= ElemArrayLen(%d)\n",ElemPosition,OrderTablePointer->ElemArrayLen);
PrintPretty();
return FailFlag;
}
*VarElem = OrderTablePointer->ElemArray[ElemPosition-1];
printf("Get ElemPosition : %d Elem Successful\n",ElemPosition);
PrintPretty();
return SuccessfulFlag;
}
2、LinearTable_OrderTable.h
#ifndef LinearTable_OrderTable_H
#define LinearTable_OrderTable_H
#define ExceptionExitFlag -1
#define SuccessfulFlag 1
#define FailFlag 0
#define ElemArrayMaxLen 10
typedef int Status;
typedef char ElemType;
typedef struct
{
ElemType *ElemArray;
int ElemArrayLen;
}OrderTable;
void *MyMalloc(size_t size);
void PrintOrderTable(OrderTable *OrderTablePointer);
Status InitOrderTable(OrderTable *OrderTablePointer);
void DestroyOrderTable(OrderTable *OrderTablePointer);
void JudgePointerNull(OrderTable *OrderTablePointer);
void ClearOrderTable(OrderTable *OrderTablePointer);
int GetOrderTableLen(OrderTable *OrderTablePointer);
int JudgeOrderTableIsEmpty(OrderTable *OrderTablePointer);
int GetOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType *VarElem);
void PrintPretty();
#endif
3、main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LinearTable_OrderTable.h"
int main()
{
ElemType *VarElem = NULL;
OrderTable *TestData = (OrderTable *)MyMalloc(sizeof(OrderTable));;
InitOrderTable(TestData);
PrintOrderTable(TestData);
ClearOrderTable(TestData);
GetOrderTableLen(TestData);
JudgeOrderTableIsEmpty(TestData);
GetOrderTableElem(TestData, 1, VarElem);
DestroyOrderTable(TestData);
return SuccessfulFlag;
}
4、makefile
CC = gcc
CFLAG_O = -c -Wall -fpic -O3
CFLAG_SO = -shared
CFLAG_EXEC = -Wall -O3
CFLAG_ALIAS = -o
RM_COMM = rm -rf
SRC_PATH = ../test_src/
EXEC_PATH = ../exec/
TEMP_PATH = ../temp/
INCLUDE_COMMAND = -I
LIB_COMMAND = -L
INCLUDE_PATH = ../include/
LIB_PATH = ../lib/
LIB_NAME = -l
#gcc -g -Wall LinearTable_OrderTable.c main.c -o Test_LinearTable_OrderTable
all : main
main :
$(CC) $(CFLAG_EXEC) LinearTable_OrderTable.c main.c $(CFLAG_ALIAS) Test_LinearTable_OrderTable
clean :
$(RM_COMM) Test_LinearTable_OrderTable
四、编译运行
[gbase@czg2 LinearTable_OrderTable]$ make clean
rm -rf Test_LinearTable_OrderTable
[gbase@czg2 LinearTable_OrderTable]$ make
gcc -Wall -O3 LinearTable_OrderTable.c main.c -o Test_LinearTable_OrderTable
[gbase@czg2 LinearTable_OrderTable]$ ./Test_LinearTable_OrderTable
Start Init List
Init List Success !!!
*********************************
Print List
ElemArray : , , , , , , , , ,
ElemArrayLen : 0
*********************************
Clear Order Table
*********************************
Get ElemArray Len : 0
*********************************
Order Table Is Empty
*********************************
Error ElemPosition : 1, Need 1 <= ElemPosition <= ElemArrayLen(0)
*********************************
Start Destroy List
Destroy List Success !!!
*********************************