线性表(数组实现)代码记录

#include <stdio.h>
#include <malloc.h> //添加malloc函数的头文件
#include <iostream>

/*
    InitList(&L):初始化表。构造一个空的线性表。
    Length(L):求表长。返回线性表L的长度,即L中元素的个数。
    LocateElem(L,e):按值查找操作。在表L中查找具有给定关键字值的元素。
    GetElem(L,e):按位查找操作。获取表L中第i个位置的元素的值。
    ListInsert(&L, i, e):插入操作。在表L中第i个位置上插入指定元素e。
    ListDelete(&L, i , &e):删除操作。删除表L中第i个位置的元素,并用e返回删除元素的值。
    PrintList(L):输出操作。按前后顺序输出线性表L的所有元素值。
    Empty(L):判空操作。若L表为空,则返回true,否则返回false。
    DestoryList(&L):销毁操作。销毁线性表,并释放线性表L所占用的内存空间。
*/

using namespace std;

#define LIST_INIT_SIZE 100
typedef int ElemType;
typedef struct{
    ElemType* data;
    int length;
    int MaxSize;
}SeqList;

/*初始化线性表*/
bool InitList(SeqList &L)
{
    L.data = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    L.length = 0;
    L.MaxSize = LIST_INIT_SIZE;
    return true;
}

/*线性表的长度*/
int Length(SeqList L)
{
    return L.length;
}

/*按值查找操作。在表L中查找具有给定关键字值的元素。*/
int LocateElem(SeqList L, ElemType e)
{
    int i;
    for(i=0; i<L.length;i++)
    {
        if(L.data[i] == e)
        {
            return i+1;
        }
    }
    if(i == L.length) return 0;
}

/*GetElem(L,e):按位查找操作。获取表L中第i个位置的元素的值。*/
ElemType GetElem(SeqList L, int i)
{
    if(i<1 || i>L.length)
    {
        cout<<"i不在合法的范围内"<<endl;
    }
    return L.data[i-1];
}

/*ListInsert(&L, i, e):插入操作。在表L中第i个位置上插入指定元素e。*/
bool ListInsert(SeqList &L, int i, ElemType e)
{
    if(i<1 || i>L.length+1)
    {
        return false;
    }
    if(L.length > L.MaxSize)
    {
        return false;
    }
    for(int j=L.length-1; j>=i; j--)
    {
        L.data[j] = L.data[j-1];
    }
    L.data[i-1] = e;
    L.length++;
    return true;
}

/*ListDelete(&L, i , &e):删除操作。删除表L中第i个位置的元素,并用e返回删除元素的值。*/
bool ListDelete(SeqList &L, int i, ElemType &e)
{
    if(i<1 || i>L.length)
    {
        return false;
    }
    e = L.data[i-1];
    for(int j=i; j<L.length; j++)
    {
        L.data[j-1] = L.data[j];
    }
    L.length--;
    return true;
    
}

/*PrintList(L):输出操作。按前后顺序输出线性表L的所有元素值。*/
void PrintList(SeqList L)
{
    for(int i=0; i<L.length; i++)
    {
        if(i == L.length-1)
        {
            cout<<L.data[i];
        }else{
            cout<<L.data[i]<<"-";
        }
        
    }
}

/*Empty(L):判空操作。若L表为空,则返回true,否则返回false。*/
bool Empty(SeqList L)
{
    if(L.length == 0)
    {
        return true;
    }else{
        return false;
    }
}

/*DestoryList(&L):销毁操作。销毁线性表,并释放线性表L所占用的内存空间。*/
bool DestoryList(SeqList &L)
{
    free(L.data);
    L.length = 0;
    L.MaxSize = 0;
    return true;
}

int main()
{
    SeqList L;
    int i;
    InitList(L);
    for(i=0; i<10; i++)
    {
        ListInsert(L,i+1,i+20);
    }
    cout<<"线性表:";
    PrintList(L);
    bool empty = Empty(L);
    cout<<endl;
    int length = Length(L);
    cout<<"线性表长度是:"<<length<<endl;
    cout<<"线性表是否为空:"<<empty<<endl;
    int j = LocateElem(L,22);
    cout<<"表中值为22的是第"<<j<<"位"<<endl;
    ElemType temp = GetElem(L, 5);
    cout<<"表中第5位的值是:"<<temp<<endl;
    cout<<"删除表中第7位"<<endl;
    ElemType e;
    ListDelete(L,7,e);
    cout<<"删除后的线性表是:";
    PrintList(L);
    cout<<endl;
    cout<<"删除的元素是:"<<e<<endl;
    length = Length(L);
    cout<<"线性表长度是:"<<length<<endl;
    DestoryList(L);
    PrintList(L);
    length = Length(L);
    cout<<"线性表长度是:"<<length<<endl;
    empty = Empty(L);
    cout<<"线性表是否为空:"<<empty<<endl;
    getchar();
    return 1;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值