顺序表实现(第二部分)

输入

为线性表的操作系列,每个操作一行,具体见样例。

输出

如果输入为"Empty", 则根据表是否为空输出"Empty"或 "Not empty"。
如果输入为"Length",则输出表长。
如果输入为"Insert i e",插入失败则输出"Insert failed",否则在i位置插入e后输出插入后表中的所有元素。
如果输入为"GetElem i ",参数i错误输出"Out of index",否则输出在i位置的元素。
如果输入为"LocateElem e",如果未发现输出"e  is not found in list",否则输出e在表中的位置。
如果输入为"Delete i",如果失败输出"Delete failed",否则删除i位置的元素后输出插入后表中的所有元素。
具体参见样例。

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
 
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
 
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
 
#define MAXSIZE 100 //顺序表可能达到的最大长度
 
typedef struct {
   ElemType *elem; //存储空间的基地址
   int length; //当前长度
} SqList;
 
Status InitList(SqList &L)   //算法2.1 顺序表的初始化
{
   //构造一个空的顺序表L
   L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
   if(!L.elem)
      exit(OVERFLOW); //存储分配失败退出
   L.length = 0; //空表长度为0
   return OK;
}
void DestroyList(SqList &L)
{
   if(L.elem)
      delete []L.elem;     //释放存储空间
}
 
int ListLength(SqList L)
{
   /****在此下面完成代码***************/
    return L.length;
   /***********************************/
}
bool ListEmpty(SqList L)
{
   /****在此下面完成代码***************/
    if(L.length==0)
    return OK;
    else
    return ERROR;
   /***********************************/
}
Status GetElem(SqList L, int i, ElemType & e)   //算法2.2 顺序表的取值
{
   /****在此下面完成代码***************/
    if(i<1||i>L.length)
    return ERROR;
    e=L.elem[i-1];
    return OK;
   /***********************************/
}
int LocateElem(SqList L, ElemType e)   //算法2.3 顺序表的查找
{
   /****在此下面完成代码***************/
    int i;
    for(i=0;i<=L.length-1;i++)
    if(L.elem[i]==e)
    return i+1;
    return ERROR;
   /***********************************/
}
Status ListInsert(SqList & L, int i, ElemType e)   //算法2.4 顺序表的插入
{
   /****在此下面完成代码***************/
    if((i<1)||(i>L.length+1))
    return ERROR;
    if(L.length==MAXSIZE)
    return ERROR;
    int j=L.length-1;
    while(j>=i-1){
        L.elem[j+1]=L.elem[j];
        j--;
    }
    L.elem[i-1]=e;
    ++L.length;
    return OK;
   /***********************************/
}
Status ListDelete(SqList & L, int i)   //算法2.5 顺序表的删除
{
   /****在此下面完成代码***************/
    if(i<1||i>L.length)
    return ERROR;
    int j=i-1;
    while(j<L.length-1){
        L.elem[j]=L.elem[j+1];
        j++;
    }
    --L.length; 
    return OK;
 
   /***********************************/
}
void ListPrint(SqList L)
{
   for(int i = 0; i < L.length; i++)
      cout << L.elem[i] << ((i == L.length - 1) ? '\n' : ' ');
}
int main()
{
   int i;
   ElemType e;
   SqList L;
   string op;
   InitList(L);
   while(cin >> op) {
      if(op == "Empty")
         cout << (ListEmpty(L) ? "Empty" : "Not empty") << endl;
      else if(op == "Insert") {
         cin >> i >> e;
         if(ListInsert(L, i, e) == ERROR)
            cout << "Insert failed" << endl;
         else
            ListPrint(L);
      } else if(op == "Length") {
         cout << "List length is " << ListLength(L) << endl;
      } else if(op == "GetElem") {
         cin >> i;
         if(GetElem(L, i, e) == ERROR)
            cout << "Out of index" << endl;
         else
            cout << "The elem at position " << i << " is " <<  e << endl;
      } else if(op == "LocateElem") {
         cin >> e;
         i = LocateElem(L, e);
         if(i == 0)
            cout << e << " is not found in list" << endl;
         else
            cout << e  << " is found at the position " << i << endl;
      } else if(op == "Delete") {
         cin >> i;
         if(ListDelete(L, i) == ERROR)
            cout << "Delete failed" << endl;
         else
            ListPrint(L);
      }
   }
   DestroyList(L);
   return 0;
}
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值