顺序表实现(第一部分)

拷贝下面的代码,按要求完成其中顺序表的ListLength,ListEmpty和ListInsert操作,其他地方不得改动。

#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)
{
   /****在此下面完成代码***************/


   /***********************************/
}

bool ListEmpty(SqList L)
{
   /****在此下面完成代码***************/


   /***********************************/
}


Status ListInsert(SqList &L, int i, ElemType e)   //算法2.4 顺序表的插入
{
   /****在此下面完成代码***************/


   /***********************************/
}

void ListPrint(SqList L)
{
   for(int i = 0; i < L.length; i++)
      cout << L.elem[i] << ((i == L.length - 1) ? '\n' : ' ');
}

int main()
{
   SqList L;
   string op;
   InitList(L);
   while(cin >> op) {
      if(op == "Empty")
         cout << (ListEmpty(L) ? "Empty" : "Not empty") << endl;
      else if(op == "Insert") {
         int i, e;
         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;
      }
   }
   DestroyList(L);
   return 0;
}

输入

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

输出

如果输入为"Empty", 则根据表是否为空输出"Empty"或 "Not empty"。
如果输入为"Length",则输出表长。
如果输入为"Insert i e",插入失败则输出"Insert failed",否则在i位置插入e后输出插入后表中的所有元素。
具体参见样例。

样例输入

Empty
Insert 1 7
Empty
Insert 2 3
Length
Insert 1 -100
Length
Insert 10 100
Length
Insert 2 10000
Empty

样例输出 

Empty
7
Not empty
7 3
List length is 2
-100 7 3
List length is 3
Insert failed
List length is 3
-100 10000 7 3
Not empty

解决问题代码

#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 ListInsert(SqList &L, int i, ElemType e)   //算法2.4 顺序表的插入
{
   /****在此下面完成代码***************/
	int j;
	if((i<1)||(i>L.length+1 )){
		return ERROR;
	}
	if (L.length==MAXSIZE) 
		return ERROR;
	for(j=L.length-1;j>=i-1;j--){
		L.elem [j+1]=L.elem [j];
	}
	L.elem [i-1]=e;
	++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()
{
   SqList L;
   string op;
   InitList(L);
   while(cin >> op) {
      if(op == "Empty")
         cout << (ListEmpty(L) ? "Empty" : "Not empty") << endl;
      else if(op == "Insert") {
         int i, e;
         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;
      }
   }
   DestroyList(L);
   return 0;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

久长愿长久

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值