数据结构与算法基础-学习-02-线性表之顺序表-插入元素、删除元素

一、测试环境

名称
cpu 12th Gen Intel® Core™ i7-12700H
操作系统 CentOS Linux release 7.9.2009 (Core)
内存 3G
逻辑核数 2
gcc 版本 4.8.5 20150623

二、小发现

1、顺序表插入元素效率

顺序表插入的元素如果在首位,需要把所有的元素向后移动一位,和链表在插入元素效率上有一定差距。

2、顺序表定位元素效率

顺序表定位元素,可以根据索引号直接定位元素,而链表需要依次遍历比较,才可以定位想要的元素。

3、顺序表删除元素效率

顺序表删除的元素如果在首位,需要把所有的元素向前移动一位,比较耗时。如果删除元素在尾部,不需要移动其他数据,效率较高。但和链表在删除元素效率上有一定差距。

三、函数介绍

1、InsertOrderTableElem

(1)函数定义

Status InsertOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType VarElem)
{
    JudgePointerNull(OrderTablePointer);
    printf("ElemPosition : %d, VarElem : %c ,",ElemPosition,VarElem);
    if(ElemPosition < 1 || ElemPosition > OrderTablePointer->ElemArrayLen + 1)
    {
        printf("Error ElemPosition : %d, Need 1 <= ElemPosition <= %d\n",ElemPosition,OrderTablePointer->ElemArrayLen + 1);
        PrintPretty();
        return FailFlag;
    }
    if(OrderTablePointer->ElemArrayLen == ElemArrayMaxLen)
    {
        printf("ElemArrayLen Is Full, Can't Insert Data\n");
        PrintPretty();
        return FailFlag;
    }
    int i;
    for(i = OrderTablePointer->ElemArrayLen - 1; i >= ElemPosition - 1; i--)
    {
        OrderTablePointer->ElemArray[i+1] = OrderTablePointer->ElemArray[i];
    }
    OrderTablePointer->ElemArray[ElemPosition-1] = VarElem;
    OrderTablePointer->ElemArrayLen++;
    printf("Insert Data Success\n");
    PrintPretty();
    return SuccessFlag;
}

(2)参数说明

参数名 说明
OrderTablePointer 需要插入的顺序表。
ElemPosition 插入的位置,大于等于1,小于等于当前列表最大长度加一。
VarElem 需要插入的元素。

2、DeleteOrderTableElem

(1)函数定义

Status DeleteOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType *DelVarElem)
{
    JudgePointerNull(OrderTablePointer);
    *DelVarElem = OrderTablePointer->ElemArray[ElemPosition - 1];
    printf("ElemPosition : %d, DelVarElem : %c ,",ElemPosition,*DelVarElem);
    if(ElemPosition < 1 || ElemPosition > OrderTablePointer->ElemArrayLen)
    {
        printf("Error ElemPosition : %d, Need 1 <= ElemPosition <= %d\n",ElemPosition,OrderTablePointer->ElemArrayLen);
        PrintPretty();
        return FailFlag;
    }
    int i;
    for(i = ElemPosition - 1; i < OrderTablePointer->ElemArrayLen - 1; i++)
    {
        OrderTablePointer->ElemArray[i] = OrderTablePointer->ElemArray[i + 1];
    }
    OrderTablePointer->ElemArray[OrderTablePointer->ElemArrayLen - 1] = '\0';
    OrderTablePointer->ElemArrayLen--;
    printf("Delete Data Success\n");
    PrintPretty();
    return SuccessFlag;
}

(2)参数说明

参数名 说明
OrderTablePointer 需要删除的顺序表。
ElemPosition 删除的位置,大于等于1,小于等于当前列表最大长度。
VarElem 返回ElemPosition位置删除的元素。

四、测试代码

1、LinearTable_OrderTable.c

#include 
#include 
#include 
#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 SuccessFlag;
}

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 SuccessFlag;
    }
    else
    {
        printf("Order Table Is Not Empty\n");
        PrintPretty();
        return FailFlag;
    }
}

// VarElem传入参数必须是malloc的,如果不是会出现段错误。
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("ElemPosition : %d ,Elem : %c , Get Data Success\n",ElemPosition,*VarElem);
    PrintPretty();
    return SuccessFlag;
}

int LocateOrderTableElem_V1(OrderTable *OrderTablePointer, ElemType SelectElem)
{
    JudgePointerNull(OrderTablePointer);
    int i;
    for(i = 0; i < OrderTablePointer->ElemArrayLen; i++)
    {
        if(SelectElem == OrderTablePointer->ElemArray[i])
        {
            printf("Position : %d, Locate Order Table Elem Success\n",i+1);
            PrintPretty();
            return i + 1;
        }
    }
    printf("Locate Order Table Elem Fail!!!\n");
    PrintPretty();
    return FailFlag;
}

Status InsertOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType VarElem)
{
    JudgePointerNull(OrderTablePointer);
    printf("ElemPosition : %d, VarElem : %c ,",ElemPosition,VarElem);
    if(ElemPosition < 1 || ElemPosition > OrderTablePointer->ElemArrayLen + 1)
    {
        printf("Error ElemPosition : %d, Need 1 <= ElemPosition <= %d\n",ElemPosition,OrderTablePointer->ElemArrayLen + 1);
        PrintPretty();
        return FailFlag;
    }
    if(OrderTablePointer->ElemArrayLen == ElemArrayMaxLen)
    {
        printf("ElemArrayLen Is Full, Can't Insert Data\n");
        PrintPretty();
        return FailFlag;
    }
    int i;
    for(i = OrderTablePointer->ElemArrayLen - 1; i >= ElemPosition - 1; i--)
    {
        OrderTablePointer->ElemArray[i+1] = OrderTablePointer->ElemArray[i];
    }
    OrderTablePointer->ElemArray[ElemPosition-1] = VarElem;
    OrderTablePointer->ElemArrayLen++;
    printf("Insert Data Success\n");
    PrintPretty();
    return SuccessFlag;
}

Status DeleteOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType *DelVarElem)
{
    JudgePointerNull(OrderTablePointer);
    *DelVarElem = OrderTablePointer->ElemArray[ElemPosition - 1];
    printf("ElemPosition : %d, DelVarElem : %c ,",ElemPosition,*DelVarElem);
    if(ElemPosition < 1 || ElemPosition > OrderTablePointer->ElemArrayLen)
    {
        printf("Error ElemPosition : %d, Need 1 <= ElemPosition <= %d\n",ElemPosition,OrderTablePointer->ElemArrayLen);
        PrintPretty();
        return FailFlag;
    }
    int i;
    for(i = ElemPosition - 1; i < OrderTablePointer->ElemArrayLen - 1; i++)
    {
        OrderTablePointer->ElemArray[i] = OrderTablePointer->ElemArray[i + 1];
    }
    OrderTablePointer->ElemArray[OrderTablePointer->ElemArrayLen - 1] = '\0';
    OrderTablePointer->ElemArrayLen--;
    printf("Delete Data Success\n");
    PrintPretty();
    return SuccessFlag;
}

2、 LinearTable_OrderTable.h

#ifndef LinearTable_OrderTable_H
#define LinearTable_OrderTable_H

#define ExceptionExitFlag -1
#define SuccessFlag        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);
int LocateOrderTableElem_V1(OrderTable *OrderTablePointer, ElemType SelectElem);
Status InsertOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType VarElem);
Status DeleteOrderTableElem(OrderTable *OrderTablePointer, int ElemPosition, ElemType *DelVarElem);

void PrintPretty();

#endif

3、main.c

#include 
#include 
#include 
#include "LinearTable_OrderTable.h"

int main()
{
    //ElemType *VarElem   = (ElemType *)MyMalloc(sizeof(ElemType));
    ElemType VarElem       = '\0';
    ElemType DelVarElem    = '\0';
    ElemType SelectElem    = 'A';
    int i;

    OrderTable *TestData = (OrderTable *)MyMalloc(sizeof(OrderTable));;
    InitOrderTable(TestData);
    for(i = 65; i <= 74; i++)
    {
        InsertOrderTableElem(TestData, 1, i);
    }
    PrintOrderTable(TestData);
    JudgeOrderTableIsEmpty(TestData);
    GetOrderTableElem(TestData, 1, &VarElem);
    LocateOrderTableElem_V1(TestData, SelectElem);
    DeleteOrderTableElem(TestData, 1, &DelVarElem);
    DeleteOrderTableElem(TestData, GetOrderTableLen(TestData), &DelVarElem);
    DeleteOrderTableElem(TestData, GetOrderTableLen(TestData) / 2, &DelVarElem);
    ClearOrderTable(TestData);
    PrintOrderTable(TestData);
    DestroyOrderTable(TestData);

    //free(VarElem);
    //VarElem = NULL;  
    return SuccessFlag;
}

4、makefile

CC = gcc
CFLAG_EXEC = -Wall -g
CFLAG_ALIAS = -o
RM_COMM = rm -rf

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 -g LinearTable_OrderTable.c main.c -o Test_LinearTable_OrderTable 

[gbase@czg2 LinearTable_OrderTable]$ ./Test_LinearTable_OrderTable 
Start Init List
Init List Success !!!
*********************************
ElemPosition : 1, VarElem : A ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : B ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : C ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : D ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : E ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : F ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : G ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : H ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : I ,Insert Data Success
*********************************
ElemPosition : 1, VarElem : J ,Insert Data Success
*********************************
Print List
ElemArray    : J ,I ,H ,G ,F ,E ,D ,C ,B ,A 
ElemArrayLen : 10
*********************************
Order Table Is Not Empty
*********************************
ElemPosition : 1 ,Elem : J , Get Data Success
*********************************
Position : 10, Locate Order Table Elem Success
*********************************
ElemPosition : 1, DelVarElem : J ,Delete Data Success
*********************************
Get ElemArray Len : 9
*********************************
ElemPosition : 9, DelVarElem : A ,Delete Data Success
*********************************
Get ElemArray Len : 8
*********************************
ElemPosition : 4, DelVarElem : F ,Delete Data Success
*********************************
Clear Order Table
*********************************
Print List
ElemArray    : I ,H ,G ,E ,D ,C ,B , , , 
ElemArrayLen : 0
*********************************
Start Destroy List
Destroy List Success !!!
*********************************

你可能感兴趣的:(数据结构与算法基础学习,学习,链表,数据结构,c语言)