鱼C论坛

 找回密码
 立即注册
查看: 2009|回复: 1

[学习笔记] 线性表顺序存储结构的构建

[复制链接]
发表于 2018-5-13 15:01:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
**************************************************************************************************
                                       做了一些优化,可能不是最优;请评论区提出一些更好的想法




***************************************************************************************************
   #include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 20
typedef int ElemType;
typedef int Status;//不用ElemType作为函数的返回类型,原因是ElemType会变,所以重新定义
typedef struct
{
    ElemType data[MAXSIZE];//线性表的最大容量//不同语言可以动态扩容
    int length;//线性表的当前长度
}sqList;
Status InitList(sqList *L)
{
    L=(sqList *)malloc(sizeof(sqList));
    L->length=0;
}
Status ListEmpty(sqList L)
{
    if(L.length>0)
        return true;
    else
        return false;
}
void DestroyList(sqList *L)
{
    free(L);
    cout<<"线性表被释放";
    cout<<"线性表的当前长度为:"<<L->length<<endl;
}

Status GetElem(sqList L,int i,ElemType*e)
{
    if(L.length==0||i<1||i>L.length)
    {
        return 0;
    }
    else
    {
        *e=L.data[i-1];
        return 1;
    }
}
Status LocateElem(sqList L,ElemType e)
{
    for(int i=1;i<=L.length;i++)
    {

        int counts=0;
        if(L.data==e)
        {

            cout<<"元素找到在第"<<i<<"位"<<endl;
            return i;
        }
        else
        {
            counts++;
            if(counts==L.length)
            {
                cout<<"要查找的数据不在该表中"<<endl;
                return 0;

            }

        }



    }
}
Status ListInsert(sqList *L,int i,ElemType e)
{
    if(L->length>=MAXSIZE)
    {
        cout<<"线性表长度超出最大长度"<<endl;
        return 0;
    }
    else if(i<1||i>L->length)
    {
        cout<<"插入位置错误"<<endl;
        return 0;
    }
    else if(i<L->length)
    {
        for(int k=L->length;k>=i;k--)
        {
            L->data[k+1]=L->data[k];
        }
        L->data=e;
        L->length++;
    }
    return 1;
}
Status ListDelete(sqList *L,int i,ElemType *e)
{
    if(L->length<=0)
    {
        cout<<"Error:线性表为空"<<endl;
        return 0;
    }
    else if(i<0||i>L->length)
    {
        cout<<"Error:删除位置不在该线性表中"<<endl;
        return 0;
    }
    else if(i>0&&i<=L->length)
    {
        for(int k=i;k<L->length;k++)//最后的元素依然存在,只不过长度-1
        {
            L->data[k]=L->data[k+1];
        }
        L->length--;
        return 1;
    }
}
Status ListLenth(sqList L)
{
    return L.length;
}
void PrintfList(sqList L)
{
    for(int i=1;i<=L.length;i++)
    {
        cout<<L.data<<" ";
    }
    cout<<endl;
}
int main()
{
    sqList L;
    InitList(&L);
    ElemType e;
    int i=0;
    cout<<"请输入线性表的长度"<<endl;
    cin>>L.length;
    cout<<"输入顺序的元素:"<<endl;
    for(int i=1;i<=L.length;i++)//从1开始,让人读的程序,因为我们正常的习惯就是从1开始
    {
        cin>>L.data;

    }
    cout<<"请输入要查找的数据:"<<endl;
    cin>>e;
    LocateElem(L,e);
    //********************
    cout<<"请输入要插入的数据e,输入插入的位置i:"<<endl;
    cin>>e>>i;
    ListInsert(&L,i,e);
    cout<<"线性表当前长度为:"<<ListLenth(L)<<endl;
    PrintfList(L);
    //*******************
    cout<<"请输入要删除的位置i:"<<endl;
    cin>>i;
    ListDelete(&L,i,&e);
    PrintfList(L);
    cout<<"线性表当前长度为:"<<ListLenth(L)<<endl;
    //******************
    DestroyList(&L);


}


                                                             捕获.20.PNG


















想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-5-13 15:02:35 | 显示全部楼层
斜体的有些失误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 00:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表