第三周项目1(3)对线性表的修改

来源:互联网 发布:人工智能在制造业应用 编辑:程序博客网 时间:2024/06/13 18:39
/*
*Copyright(c)2017,烟台大学计算机学院
*All right reserved.
*文件名:SK.cpp
*作者:盛凯
*完成日期:2017年9月20日
*版本号:v1.0
*
*问题描述:线性表的修改
*输入描述:无
*程序输出:线性表
*/
#include <stdio.h>
#include <malloc.h>


#define MaxSize 50    //Maxsize将用于后面定义存储空间的大小
typedef int ElemType;  //ElemType在不同场合可以根据问题的需要确定,在此取简单的int
typedef struct
{
    ElemType data[MaxSize];  //利用了前面MaxSize和ElemType的定义
    int length;
} SqList;


void InitList(SqList *&L)   //引用型指针
{
    L=(SqList *)malloc(sizeof(SqList));
    //分配存放线性表的空间
    L->length=0;
}


bool ListInsert(SqList *&L,int i,ElemType e)
{
    int j;
    if (i<1 || i>L->length+1)
        return false;   //参数错误时返回false
    i--;            //将顺序表逻辑序号转化为物理序号
    for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置
        L->data[j]=L->data[j-1];
    L->data[i]=e;           //插入元素e
    L->length++;            //顺序表长度增1
    return true;            //成功插入返回true
}


//输出线性表DispList(L)
bool ListEmpty(SqList *L)
{
    return(L->length==0);
}
void DispList(SqList *L)
{
    int i;
    if (ListEmpty(L))
        return;
    for (i=0; i<L->length; i++)
        printf("%d ",L->data[i]);
    printf("\n");
}
//判定是否为空表ListEmpty(L)




int main()
{
    SqList *sq;
    InitList(sq);
    ListInsert(sq, 1, 5);
    ListInsert(sq, 2, 3);
    ListInsert(sq, 1, 4);
    DispList(sq);
    return 0;

}




知识点总结:

联系了线性表插入的三种情况,在头位置插入,在尾位置插入和中间位置插入元素。

原创粉丝点击