数据结构作业之顺序表

1.代码如下

#include <stdio.h>
#include <malloc.h>

#define LIST_MAX_LENGTH 10 

/**
 * define the structure and the pointer to the structure
 */
 
typedef struct SequentialList 
{
    int actualLength;
    int data[LIST_MAX_LENGTH];
}*SequentialListPtr;

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

/**
 *output the memory
 */

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

/**
 *initial the sequential list 
 */

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

/**
 *insert an element to the initial list 
 */

void sequentialListInsert(SequentialListPtr paraListPtr,int paraPosition,int paraValue){
    //step 1: check if the sequential list is full
    if(paraListPtr->actualLength>=LIST_MAX_LENGTH)
    {
        printf("Cannot insert element: List full.\r\n");
        return ;
    }
    
    //step 2: check if the position is a negative number 
    if(paraPosition<0)
    {
        printf("Cannot insert element: negation position unsupported.");
        return ;
    }
    
    //step 3: check if the position is beyond the actual size  
    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 4: move the remaining part
    int i;
    for(i=paraListPtr->actualLength;i>paraPosition;i--)
    {
        paraListPtr->data[i]=paraListPtr->data[i-1];
    }
    
    //step 5: insert the element 
    paraListPtr->data[paraPosition]=paraValue;
    
    //step 6: update the actual length 
    paraListPtr->actualLength++;
}

/**
 *test the sequentialListInsert function 
 */
 

void sequentialInsertTest()
{
    int i;
    int tempArray[5]={3,5,2,7,4};
    
    printf("---- sequentialInsertTest begins. ----\r\n");
    
    //initail sequential list.
    SequentialListPtr tempList = sequentialListInit(tempArray,5);
    printf("After initialization, the list is: ");
    outputList(tempList);
    
    //insert to the first position.
    printf("Now insert to the first, the list is: ");
    sequentialListInsert(tempList,0,8);
    outputList(tempList);
    
    //insert to the last position.
    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. ----\r\n"); 
}

/**
 *delete an element from the sequential list 
 */

int sequentialListDelete(SequentialListPtr paraListPtr,int paraPosition)
{
    //step 1. check the position of the element 
    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. move the remaining part.
    int resultValue = paraListPtr->data[paraPosition];
    int i;
    for(i=paraPosition;i<paraListPtr->actualLength;i++)
    {
        paraListPtr->data[i]=paraListPtr->data[i+1]; 
    }
    
    //step 3.updata the actual length.
    paraListPtr->actualLength--;
    
    //step 4.return the value that is deleted.
    return resultValue;
}

/**
 *test the  sequentialListDelete function
 */

void sequentialDeleteTest()
{
    int tempArray[5]={3,5,2,7,4};
    
    printf("---- sequentialDeleteTest begins. ----\r\n");
    
    //initialize the sequential list.
    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 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 5th.
    printf("Now delete the 5th, the list is: ");
    sequentialListDelete(tempList,5);
    outputList(tempList);
    
    //delete the -6th.
    printf("Now delete the (-6)th, the list is: ");
    sequentialListDelete(tempList,-6);
    outputList(tempList);
    
    printf("---- sequentialDeletTest ends. ----\r\n");
    
    outputMemory(tempList);

/**
 *find an element from the list
 */
 
int locateElement(SequentialListPtr paraListPtr,int paraValue)
{
    int i;
    for(i=0;i<paraListPtr->actualLength;i++)
    {
        if(paraListPtr->data[i]==paraValue)
        {
            return i;
        }
    }
    return -1;

/**
 *get the element from the list 
 */

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

void main()
{
    sequentialInsertTest();
    sequentialDeleteTest();
}

2.代码说明

1)actual length 是数据的个数,而i是数组的下标

2)在对顺序表进行插入时,需要注意三点,一是顺序表是否已经满了,二是插入的位置是否小于0,三是插入的位置是否大于actual length

3) 在对顺序表进行删除时,需要注意两点,一是删除的位置是否小于0,二是删除的位置是否大于等于actual length

4) 注意在代码进行插入和删除后记得改变actual length的值

5)在清除顺序表时,需要让actual length 的值为0

 3.图示插入和删除操作

4.运行结果

---- sequentialInsertTest begins. ----
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.
Cannot insert element: the position 8 is bigger than the list length 7.
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.
Cannot insert element: List full.
12 11 10 8 3 5 2 7 4 9
inserting 14.
Cannot insert element: List full.
12 11 10 8 3 5 2 7 4 9
---- sequentialInsertTest ends. ----
---- sequentialDeleteTest begins. ----
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: Cannot delete element : the position 5 is beyond the list length 2.
5 7
Now delete the (-6)th, the list is: Invalid position: -6.
5 7
---- sequentialDeletTest ends. ----
The address of the structure: 7564608
The address of actuallength: 7564608
The address of data: 7564612
The address of actual data: 7564612
The address of second data: 7564616

--------------------------------
Process exited after 0.1315 seconds with return value 37
请按任意键继续. . .

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值