【无标题】

#include<stdio.h>
#include<malloc.h>
#define List_Max_Length 10
typedef struct SequentialList{
    int actualLength;
    int data[List_Max_Length];
}*SequentialListPtr;

/***
output
***/
void outputList(SequentialListPtr paraList){
    int i;
    for(i=0;i<paraList->actualLength;i++){
        printf("%d ",paraList->data[i]);
    }
    printf("\n");
}

/***
output memory 
***/
void outputMemory(SequentialListPtr paraListPtr){
    printf("The address of the structure:%ld\r\n",paraListPtr);
    printf("The address of the actualLength:%ld\r\n",&paraListPtr->actualLength);
    printf("The address of the data:%ld\r\n",*paraListPtr->data);
    printf("The address of the actual data:%ld\r\n0",&paraListPtr->data[0]);
    printf("The address of the second data:%ld\r\n",&paraListPtr->data[1]);
}

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

/***
insert an element
***/

void SequentialListInsert(SequentialListPtr paraListPtr,int paraPosition,int paraValue){
//step 1
if(paraListPtr->actualLength>=List_Max_Length){
    printf("Cannot insert element:list full.\r\n");
    return;
}

//step 2
if(paraPosition<0){
    printf("Cannot insert elemnt: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;    
}
 
//step 3
int i=0;
for(i=paraListPtr->actualLength;i>paraPosition;i--){
    paraListPtr->data[i]=paraListPtr->data[i-1];
}
 
//step 4
paraListPtr->data[paraPosition]=paraValue;

//step 5
paraListPtr->actualLength ++;
}


/***
test insert function
***/
void SequentialInsertTest(){
    int i;
    int tempArray[5]={3,5,2,7,4};
    printf("------sequentialInsertTest begins--------\n");
    
    //initialize
    SequentialListPtr tempList=SequentialListInit(tempArray,5);
    printf("After initialization,the list is: ");
    outputList(tempList);
    
    //insert
    printf("Now insert to the first,the list is: ");
    SequentialListInsert(tempList,0,8);
    outputList(tempList);
    
    //insert to the list
    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\r\n");
    SequentialListInsert(tempList,8,9);
    printf("The list is:");
    outputList(tempList);
    
    //insert to position 3
    
    for(i=0;i<5;i++){
        printf("inserting %d\r\n,",(i+10));
        SequentialListInsert(tempList,0,(i+10));
        outputList(tempList);
    }
    
    printf("-------------sequentialInsertTest ends------------");    
}

/***
delet an element
***/

int SequentialListDelete(SequentialListPtr paraListPtr,int paraPosition){
    //step 1
    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;
    }
    
    //step 2
    int resultValue = paraListPtr->data[paraPosition];
    int i=0;
    for (i = paraPosition; i < paraListPtr->actualLength; i ++) {
        paraListPtr->data[i] = paraListPtr->data[i + 1];
    }
    
    //step 3
    paraListPtr->actualLength --;
    
    //step 4
    return resultValue;
}

/***
Test the delete function
***/

void SequentialDeleteTest(){
    int tempArray[5]={3,5,2,7,4};
    printf("--------sequentialDeleteTest begins--------\r\n");
    
    //initialize
    SequentialListPtr tempList = SequentialListInit(tempArray, 5);
    printf("After initialization, the list is: ");
    outputList(tempList);
    
    //delete first
    printf("Now delete the first, the list is: ");
    SequentialListDelete(tempList, 0);
    outputList(tempList);
    
    //delete 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 fifth.
    printf("Now delete the 5th, the list is: ");
    SequentialListDelete(tempList, 5);
    outputList(tempList);

    //delete the negative sixth.
    printf("Now delete the (-6)th, the list is: ");
    SequentialListDelete(tempList, -6);
    outputList(tempList);

    printf("-------sequentialDeleteTest ends-------\r\n");
    outputMemory(tempList);
}


/***
locate :whether the element exist in the list 
***/
int locateElement(SequentialListPtr paraListPtr, int paraValue) {
    int i=0;
    for (i = 0; i < paraListPtr->actualLength; i ++) {
        if (paraListPtr->data[i] == paraValue) {
            return i;
        }
    }
    return -1;
}


/***
get an element in the list
***/


int getElement(SequentialListPtr paraListPtr,int paraPosition){
    //step 1
    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];
}

/***
clear the list
***/

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


int main() {
    SequentialInsertTest();
    SequentialDeleteTest();
    return 0;
}
 

结果

心得体会:

大致知道顺序表插入、删除、定位、清理的过程与原理

顺序表中元素与元素之间没有空的位置

实际长度小于等于最大长度。

对函数的理解比之前清晰,体会到函数的好处,改BUG的时候比较方便,主函数十分简洁。

改bug用了好久!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值