栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言数据结构-01

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言数据结构-01

顺序表

1.创建顺序表

typedef struct SequentialList {
	int actualLength;
	int data[LIST_MAX_LENGTH];
}*SequentialListPtr;

2.顺序表的初始化与动态内存申请

SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
	SequentialListPtr resultPtr = (SequentialListPtr)malloc)(sizeof(SequentialListPtr));
	for(int i = 0; i < paraLength; i++) {
	resultPtr->data[i] = paraData[i];
	}
	resultPtr->actualLength = paraLength;
	return resultPtr;//返回顺序表
}

3.顺序表的遍历或者打印

void outputList(SequentialListPtr paraList){
	for(i = 0; i actualLength; i++){
	printf(%d ",paraList->data[i];
	}
}

4.插入元素

void sequentialListInsert(SequentialListPtr paraList, int paraPosition, int paraValue){
	//空间检查
	if(paraList->actualLength>=LIST_MAX_LENGTH) {
		printf("顺序表已满,不能插入元素。");
		return;
	}
	if(paraPosition < 0||paraPostion > paraList->actualLength){
		printf("传入的位置错误,不能插入元素");
		return;
	}
	//移动要插入位置后面的数据
	for (int i = paraList->actualLength; i > paraPosition; i--){
		paraList->data[i] = paraList->data[i-1];
	}
	//插入元素
	paraList->data[paraPosition] = paraValue;
}   

5.删除元素

int sequentialListdelete(SequentialListPtr paraLlist, int pataPosition){
	//删除位置检查
	if (paraPosition < 0) {
		printf("无效的删除位置: %d",paraPosition);
		return -1;
	}
	if (paraPosition >= paraList->actualLength) {
		pritnf("删除位置%d超过顺序表,不能删除",paraPosition);
		return -1;
	}
	//移动删除位置后面的数据
	int resultValue = paraList->data[paraPosition];
	for (int i = paraPosition; i < paraList->actualLength; i++) {
		paraList->data[i]=paraList->data[i+1];
	}
	//更新顺序表的长度
	paraList->actualLength--;
	//返回删除的数值
	return resultValue;
}

6.根据值查找位置

int locateElement (SequentialListPtr paraList, int paraValue){
	for(i = 0; i < paraList->actualLength; i++){
		if(paraList->data[i] == paraValue){
		return i;
		}
	}		
}

7.根据位置查找值

int getElement (SequentialListPtr paraList, int paraPosition) {
	//位置检查
	if(paraPosition < 0||paraPosition >= paraList->actualLength){
		printf("无效的位置: %d",paraPosition);
		return -1;
	}
	return paraList->data[paraPosition];
	//返回该位置的值
}

8.删除顺序表

void clearList(SequentialListPtr paraList) {
	paraList->actualLength = 0;
}

9.测试

#include 
#include 

#define LIST_MAX_LENGTH 10

typedef struct SequentialList
{
    int actualLength;

    int data[LIST_MAX_LENGTH];
} * SequentialListPtr;

void outputList(SequentialListPtr paraList)
{
    for (int i = 0; i < paraList->actualLength; i++)
    {
        printf("%d ", paraList->data[i]);
    }
    printf("rn");
}
//打印该顺序表

void outputMemory(SequentialListPtr paraListPtr)
{
    printf("The address of the structure: %ldrn", paraListPtr);
    printf("The address of actualLength: %ldrn", ¶ListPtr->actualLength);
    printf("The address of data: %ldrn", ¶ListPtr->data);
    printf("The address of actual data: %ldrn", ¶ListPtr->data[0]);
    printf("The address of second data: %ldrn", ¶ListPtr->data[1]);
}

SequentialListPtr sequentialListInit(int paraData[], int paraLength)
{
    SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(SequentialListPtr));
    for (int i = 0; i < paraLength; i++)
    {
        resultPtr->data[i] = paraData[i];
    } // 初始化顺序表
    resultPtr->actualLength = paraLength;

    return resultPtr;
}

//越界检查
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue)
{
    // 空间检查
    if (paraListPtr->actualLength >= LIST_MAX_LENGTH)
    {
        printf("顺序表已满,不能插入元素。rn");
        return;
    }

    // 位置检查
    if (paraPosition > paraListPtr->actualLength)
    {
        printf("传入的位置错误,不能插入元素.rn");
        return;
    }

    // 移动要插入位置后面的数据
    for (int i = paraListPtr->actualLength; i > paraPosition; i--)
    {
        paraListPtr->data[i] = paraListPtr->data[i - 1];
    }

    // 元素插入
    paraListPtr->data[paraPosition] = paraValue;

    // 更新顺序表长度
    paraListPtr->actualLength++;
}

//单元测试函数
void sequentialInsertTest()
{
    int i;
    int tempArray[5] = {3, 5, 2, 7, 4};

    printf("---- 测试开始 ----rn");

    //测试首位置和尾位置

    // Initialize.
    SequentialListPtr tempList = sequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputList(tempList);

    // Insert to the first.
    printf("Now insert to the first, the list is: ");
    sequentialListInsert(tempList, 0, 8);
    outputList(tempList);

    // Insert to the last.
    printf("Now insert to the last, the list is: ");
    sequentialListInsert(tempList, 6, 9);
    outputList(tempList);

    // Insert beyond the tail.
    printf("Now insert beyond the tail. rn");
    sequentialListInsert(tempList, 8, 9);
    printf("The list is:");
    outputList(tempList);

    // Insert to position 3.
    for (i = 0; i < 5; i++)
    {
        printf("Inserting %d.rn", (i + 10));
        sequentialListInsert(tempList, 0, (i + 10));
        outputList(tempList);
    } // Of for i

    printf("---- 测试结束----rn");
}

int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition)
{
    // 删除位置检查
    if (paraPosition < 0 || paraPosition >= paraListPtr->actualLength)
    {
        printf("无效的位置: %d.rn", paraPosition);
        return -1;
    }

    // 移动删除位置后面的数据
    int resultValue = paraListPtr->data[paraPosition];
    for (int i = paraPosition; i < paraListPtr->actualLength; i++)
    {
        paraListPtr->data[i] = paraListPtr->data[i + 1];
    } // Of for i

    //更新长度
    paraListPtr->actualLength--;

    // 返回删除的值
    return resultValue;
}
void sequentialDeleteTest()
{
    int tempArray[5] = {3, 5, 2, 7, 4};

    printf("---- 测试开始 ----rn");

    // Initialize.
    SequentialListPtr tempList = sequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputList(tempList);

    // Delete the first.
    printf("Now delete the first, the list is: ");
    sequentialListDelete(tempList, 0);
    outputList(tempList);

    // Delete to the last.
    printf("Now delete the last, the list is: ");
    sequentialListDelete(tempList, 3);
    outputList(tempList);

    // Delete the second.
    printf("Now delete the second, the list is: ");
    sequentialListDelete(tempList, 1);
    outputList(tempList);

    // Delete the second.
    printf("Now delete the 5th, the list is: ");
    sequentialListDelete(tempList, 5);
    outputList(tempList);

    // Delete the second.
    printf("Now delete the (-6)th, the list is: ");
    sequentialListDelete(tempList, -6);
    outputList(tempList);

    printf("---- 测试结束 ----rn");

    outputMemory(tempList);
}

int locateElement(SequentialListPtr paraListPtr, int paraValue)
{
    for (int i = 0; i < paraListPtr->actualLength; i++)
    {
        if (paraListPtr->data[i] == paraValue)
        {
            return i;
        }
    }

    return -1;
}

void locateElementTest()
{
    printf("------测试开始------n");
    int i = 5;
    int paraarry[5] = {1, 2, 3, 4, 5};
    SequentialListPtr paraList = sequentialListInit(paraarry, i);
    //测试5的位置
    printf("测试元素 5 的位置:n");
    int getnumber = locateElement(paraList, 5);//接受返回的locateElement函数值
    if (getnumber == -1)
    {
        printf("测试元素 %d 有误,未在数据中。n",5);
    }
    else if (getnumber != -1)
    {
        printf("测试的元素 %d 位置是 %d。n",5, getnumber);
    }
    //测试-2的位置
    printf("测试元素 -2 的位置:n");
    getnumber = locateElement(paraList,-2);
    if (getnumber == -1)
    {
        printf("测试元素 %d 有误,未在数据中。n",-2);
    }
    else if (getnumber != -1)
    {
        printf("测试的元素 %d 位置是 %d。n",-2, getnumber);
    }
    printf("-----测试结束-----n");
}

int getElement(SequentialListPtr paraListPtr, int paraPosition)
{
    // 位置检查
    if (paraPosition < 0 || paraPosition >= paraListPtr->actualLength)
    {
        printf("无效位置 %d 。rn", paraPosition);
        return -1;
    }

    return paraListPtr->data[paraPosition]; //返回该位置的值
}

void getElementTest()
{
    printf("-----测试开始-----n");
    int i=5;
    int paraarry[5]={1,2,3,4,5};
    SequentialListPtr paraList = sequentialListInit(paraarry,i);
    //测试1位置的元素
    printf("测试 1 位置的元素:n");
    int getnumber = getElement(paraList,1);
    if(getnumber != -1)
    {
        printf("测试位置 1 的元素是 %d。n",getnumber);
    }
    //测试6位置的元素
    printf("测试 6 位置的元素:n");
    getnumber = getElement(paraList,6);
    if(getnumber != -1)
    {
        printf("测试位置 6 的元素是 %d。n",getnumber);
    }
    printf("-----测试结束-----n");

}
int clearList(SequentialListPtr paraListPtr)
{
    paraListPtr->actualLength = 0;
}
void main()
{
    sequentialInsertTest();
    sequentialDeleteTest();
    getElementTest();
    locateElementTest();
}

10.测试结果

---- 测试开始 ----
After initialization, the list is: 3 5 2 7 4       
Now insert to the first, the list is: 8 3 5 2 7 4  
Now insert to the last, the list is: 8 3 5 2 7 4 9 
Now insert beyond the tail. 
传入的位置错误,不能插入元素.
The list is:8 3 5 2 7 4 9 
Inserting 10.
10 8 3 5 2 7 4 9
Inserting 11.
11 10 8 3 5 2 7 4 9
Inserting 12.
12 11 10 8 3 5 2 7 4 9
Inserting 13.
顺序表已满,不能插入元素。
12 11 10 8 3 5 2 7 4 9 
Inserting 14.
顺序表已满,不能插入元素。
12 11 10 8 3 5 2 7 4 9
---- 测试结束----
---- 测试开始 ----
After initialization, the list is: 3 5 2 7 4
Now delete the first, the list is: 5 2 7 4
Now delete the last, the list is: 5 2 7
Now delete the second, the list is: 5 7 
Now delete the 5th, the list is: 无效的位置: 5.
5 7
Now delete the (-6)th, the list is: 无效的位置: -6.
5 7
---- 测试结束 ----
The address of the structure: 7672896
The address of actualLength: 7672896
The address of data: 7672900
The address of actual data: 7672900
The address of second data: 7672904
-----测试开始-----
测试 1 位置的元素:
测试位置 1 的元素是 2。
测试 6 位置的元素:
无效位置 6 。
-----测试结束-----
------测试开始------
测试元素 5 的位置:
测试的元素 5 位置是 4。
测试元素 -2 的位置:
测试元素 -2 有误,未在数据中。
-----测试结束-----
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号