2016年数据结构:顺序表的使用

#include <iostream>
#include <string>
#include <cstring>
#include <malloc.h>
using namespace std;

typedef struct
{
    char data[1000];
    int length;
}SqList;
void CreateList(SqList *&L,char a[],int n)
{
    int i;
    L=(SqList *)malloc(sizeof(SqList));
    for(i=0;i<n;i++)
    {
        L->data[i]=a[i];
    }
    L->length=n;
}
void InitList(SqList *&L)
{
    L=(SqList *)malloc(sizeof(SqList));
    L->length=0;
}
void DestoryList(SqList * &L)
{
    free(L);
}
bool ListEmpty(SqList * &L)
{
    return (L->length==0);
}
int ListLength(SqList * &L)
{
    return(L->length);
}
void DispList(SqList * &L)
{
    int i;
    for(i=0;i<L->length;i++)
    {
        cout<<L->data[i];

    }
    cout<<endl;
}
bool GetElem(SqList * &L,int i,char &e)
{
    if(i<1||i>L->length)
        return false;
    e=L->data[i-1];
    return true;
}
int LocateElem(SqList * L,char e)
{
    int i=0;
    while(i<L->length&&L->data[i]!=e)
    {
        i++;
    }
    if(i>=L->length)
        return 0;
    else
        return i+1;
}
bool ListInsert(SqList * &L,int i,char e)
{
    int j;
    if(i<1||i>L->length+1)
        return false;
    i--;
    for(j=L->length;j>i;j--)
    {
        L->data[j]=L->data[j-1];
    }
    L->data[i]=e;
    L->length++;
    return true;
}
bool ListDelete(SqList * &L,int i,char &e)
{
    int j;
    if(i<1||i>L->length)
        return false;
    i--;
    e=L->data[i];
    for(j=i;j<L->length-1;j++)
    {
        L->data[j]=L->data[j+1];
    }
    L->length--;
    return true;
}
int main()
{
    SqList *L;
    InitList(L);
   for(int i=1;i<6;i++)
   {
      ListInsert(L,i,'a'+i-1);
   }
    DispList(L);
    cout<<ListLength(L)<<endl;
    if(ListEmpty(L))
        cout<<"这个顺序表是空的"<<endl;
    else
        cout<<"这个顺序表不是空的"<<endl;
    cout<<L->data[2]<<endl;
    cout<<LocateElem(L,'a')<<endl;
    ListInsert(L,4,'f');
    DispList(L);
    ListDelete(L,3,L->data[2]);
    DispList(L);
    DestoryList(L);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值