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

C语言数据结构 1:顺序表

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

C语言数据结构 1:顺序表

顺序表是最常用的数据结构

自主完成的代码:
​
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() {
    int tempArray[5] = {3, 5, 2, 7, 4};
    printf("---- locateElementTest begins. ----rn");
    
    //初始化顺序表
    SequentialListPtr tempList = sequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputList(tempList);
    
    int locateNumber=0;//保存被测函数返回的值
    
    printf("测试值:5rn");
    locateNumber = locateElement(tempList,5);
    if(locateNumber == -1) {
        printf("没有找到该值rn");
    } else {
        printf("位置为 %drn",locateNumber);
    }
    
    printf("测试值:1rn");
    locateNumber = locateElement(tempList,1);
    if(locateNumber == -1) {
        printf("没有找到该值rn");
    } else {
        printf("位置为 %drn",locateNumber);
    }//重复代码块因为此次测试只用了两次,所以没有写成函数
    
    printf("---- locateElementTest ends. ----rn");
    
    free(tempList);
}
​

​
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
    //判断位置是否合法
    if (paraPosition < 0) {
        printf("不合法的地址: %d.rn", paraPosition);
        return -1;
    } else if (paraPosition >= paraListPtr->actualLength) {
        printf("无法找到位置 %d ,该位置超出的此顺序表的长度 %d.rn", paraPosition, paraListPtr->actualLength);
        return -1;
    }
    
    return paraListPtr->data[paraPosition];
}
​

​
void getElementTest() {
    int tempArray[5] = {3, 5, 2, 7, 4};
    printf("---- getElementTest begins. ----rn");
    
    //初始化顺序表
    SequentialListPtr tempList = sequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputList(tempList);
    
    int testData=0;//保存被测函数返回的数据
    
    printf("测试值:3rn");
    testData = getElement(tempList, 3);
    if(testData != -1) {
        printf("该位置数据为:%drn",testData);
    }
    
    printf("测试值:-2rn");
    testData = getElement(tempList, -2);
    if(testData != -1) {
        printf("该位置数据为:%drn",testData);
    }
    
    printf("测试值:8rn");
    testData = getElement(tempList, 8);
    if(testData != -1) {
        printf("该位置数据为:%drn",testData);
    }
    
    printf("---- getElementTest ends. ----rn");
    
    free(tempList);
}
​

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

全部代码:
​
#include 
#include 
​
#define LIST_MAX_LENGTH 100
​

​
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: %prn", paraListPtr);
    printf("The address of actualLength: %prn", ¶ListPtr->actualLength);
    printf("The address of data: %prn", ¶ListPtr->data);
    printf("The address of actual data: %prn", ¶ListPtr->data[0]);
    printf("The address of second data: %prn", ¶ListPtr->data[1]);
}
​

​
SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
    SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
    for (int i = 0; i < paraLength; i ++) {
        resultPtr->data[i] = paraData[i];
    }// Of for i
    resultPtr->actualLength = paraLength;
    
    return resultPtr;
}
​

​
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPostion, int paraValue) {
    int i = 0;
​
    //检查是否还有剩余空间
    if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
        printf("Cannot insert element: list full.rn");
        return ;
    }
    
    //检查是否为负数
    if(paraValue < 0) {
        printf("Cannot insert element: negative position unsupported.");
        return;
    }
​
    //移动元素
    for (i = paraListPtr->actualLength; i > paraPostion; i--) {
        paraListPtr->data[i] = paraListPtr->data[i - 1];
    }
​
    //放入元素
    paraListPtr->data[paraPostion] = paraValue;
​
    //更新长度
    paraListPtr->actualLength++;
}
​

​
void sequentialInsertTest() {
    int i;
    int tempArray[5] = {3, 5, 2, 7, 4};
    
    printf("---- sequentialInsertTest begins. ----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("---- sequentialInsertTest ends. ----rn");
    
    free(tempList);//释放资源
}
​

​
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
    //检查位置是否合理
    if (paraPosition < 0) {
        printf("Invalid position: %d.rn", paraPosition);
        return -1;
    } else if (paraPosition >= paraListPtr->actualLength) {
        printf("Cannot delete element: the position %d is beyond the list length %d.rn", paraPosition, paraListPtr->actualLength);
        return -1;
    }
    
    //移动并以此覆盖掉要删除的数
    int resultValue = paraListPtr->data[paraPosition];//先将此数保存下来
    for (int i = paraPosition; i < paraListPtr->actualLength; i ++) {
        paraListPtr->data[i] = paraListPtr->data[i + 1];
    }
    
    //更新长度
    paraListPtr->actualLength --;
    
    //返回结果
    return resultValue;
}
​

​
void sequentialDeleteTest() {
    int tempArray[5] = {3, 5, 2, 7, 4};
    
    printf("---- sequentialDeleteTest begins. ----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("---- sequentialDeleteTest ends. ----rn");
    
    outputMemory(tempList);
    
    free(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() {
    int tempArray[5] = {3, 5, 2, 7, 4};
    printf("---- locateElementTest begins. ----rn");
    
    //初始化顺序表
    SequentialListPtr tempList = sequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputList(tempList);
    
    int locateNumber=0;//保存被测函数返回的值
    
    printf("测试值:5rn");
    locateNumber = locateElement(tempList,5);
    if(locateNumber == -1) {
        printf("没有找到该值rn");
    } else {
        printf("位置为 %drn",locateNumber);
    }
    
    printf("测试值:1rn");
    locateNumber = locateElement(tempList,1);
    if(locateNumber == -1) {
        printf("没有找到该值rn");
    } else {
        printf("位置为 %drn",locateNumber);
    }//重复代码块因为此次测试只用了两次,所以没有写成函数
    
    printf("---- locateElementTest ends. ----rn");
    
    free(tempList);
}
​

​
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
    //判断位置受否合法
    if (paraPosition < 0) {
        printf("不合法的地址: %d.rn", paraPosition);
        return -1;
    } else if (paraPosition >= paraListPtr->actualLength) {
        printf("无法找到位置 %d ,该位置超出的此顺序表的长度 %d.rn", paraPosition, paraListPtr->actualLength);
        return -1;
    }
    
    return paraListPtr->data[paraPosition];
}
​

​
void getElementTest() {
    int tempArray[5] = {3, 5, 2, 7, 4};
    printf("---- getElementTest begins. ----rn");
    
    //初始化顺序表
    SequentialListPtr tempList = sequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputList(tempList);
    
    int testData=0;//保存被测函数返回的数据
    
    printf("测试值:3rn");
    testData = getElement(tempList, 3);
    if(testData != -1) {
        printf("该位置数据为:%drn",testData);
    }
    
    printf("测试值:-2rn");
    testData = getElement(tempList, -2);
    if(testData != -1) {
        printf("该位置数据为:%drn",testData);
    }
    
    printf("测试值:8rn");
    testData = getElement(tempList, 8);
    if(testData != -1) {
        printf("该位置数据为:%drn",testData);
    }
    
    printf("---- getElementTest ends. ----rn");
    
    free(tempList);
}
​

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

​

运行结果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835412.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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