数据结构,第一次作业

“c

this is code

#include<stdio.h>
#include<malloc.h>
#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("\r\n");
}
void outputmemory(sequentiallistptr paralist)
{
    printf("The address of the structure: %lld\r\n", paralist);
    printf("The address of the actuallength: %lld\r\n", &paralist->actuallength);
    printf("The address of the data: %lld\r\n", &paralist->data);
    printf("The address of the actul data: %lld\r\n", &paralist->data[0]);
    printf("The address of the second data: %lld\r\n", &paralist->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];
    }
    resultPtr->actuallength = paralength;
    return resultPtr;

}
void sequentiallistinsert(sequentiallistptr paralistptr, int paraPosition, int paraValue)
{
    if (paralistptr->actuallength >= LIST_MAX_LENGTH) {
        printf("Cannot insert element: list full.\r\n");
        return;
    }

    if (paraPosition < 0) {
        printf("Cannot insert element: negative position unsupported.");
        return;
    }
    if (paraPosition > paralistptr->actuallength) {
        printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", paraPosition, paralistptr->actuallength);
        return;
    }//前面的if都用了return,后就不用else
    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("---- sequentialInsertTest begins. ----\r\n");

    sequentiallistptr tempList = sequentiallistInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputlist(tempList);
    printf("Now insert to the first, the list is: ");
    sequentiallistinsert(tempList, 0, 8);
    outputlist(tempList);
    printf("Now insert to the last, the list is: ");
    sequentiallistinsert(tempList, 6, 9);
    outputlist(tempList);
    printf("Now insert beyond the tail. \r\n");
    sequentiallistinsert(tempList, 8, 9);
    printf("The list is:");
    outputlist(tempList);
    for (i = 0; i < 5; i++) {
        printf("Inserting %d.\r\n", (i + 10));
        sequentiallistinsert(tempList, 0, (i + 10));
        outputlist(tempList);
    }

    printf("---- sequentialInsertTest ends. ----\r\n");
}
int sequentialListDelete(sequentiallistptr paraListPtr, int paraPosition) {

    if (paraPosition < 0) {
        printf("Invalid position: %d.\r\n", paraPosition);
        return -1;
    }

    if (paraPosition >= paraListPtr->actuallength) {
        printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", 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. ----\r\n");

    sequentiallistptr tempList = sequentiallistInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputlist(tempList);

    printf("Now delete the first, the list is: ");
    sequentialListDelete(tempList, 0);
    outputlist(tempList);

    printf("Now delete the last, the list is: ");
    sequentialListDelete(tempList, 3);
    outputlist(tempList);

    printf("Now delete the second, the list is: ");
    sequentialListDelete(tempList, 1);
    outputlist(tempList);

    printf("Now delete the 5th, the list is: ");
    sequentialListDelete(tempList, 5);
    outputlist(tempList);

    printf("Now delete the (-6)th, the list is: ");
    sequentialListDelete(tempList, -6);
    outputlist(tempList);

    printf("---- sequentialDeleteTest ends. ----\r\n");

    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;
}
int getElement(sequentiallistptr paraListPtr, int paraPosition) {
    if (paraPosition < 0) {
        printf("Invalid position: %d.\r\n", paraPosition);
        return -1;
    }

    if (paraPosition >= paraListPtr->actuallength) {
        printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actuallength);
        return -1;
    }

    return paraListPtr->data[paraPosition];
}
void clearList(sequentiallistptr paraListPtr) {
    paraListPtr->actuallength = 0;
}
void main() {
    sequentialInsertTest();
    sequentialDeleteTest();
}

1.前面的if都用了return,后就不用else

2.插入时,边界是length,删除时边界是length-1;

3.插入时,从尾部开始向后移一位,直到指定位置;删除时,从指定位置开始,其下一位向前移动。

4.地址用%p来写可能更好一点,VS里%ld报错了。

5。malloc时可能出现申请失败,可以加在malloc下面加

if(reultPtr == NULL)

{

printf("动态内存分配失败\n");

exit(-1);

}

6.插入

7.删除

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值