简单实现顺序表

#include<iostream>
#define MAXSIZE 100
#define OVERLOW -1
using namespace std;

typedef struct{
	    int *elem;
		int length; 
}SqList;

bool InitList(SqList &L)
{//建立顺序表 
	L.elem = new int[MAXSIZE];
	if(!L.elem) 
	     return false;
	L.length = 0;
	return true;
}

bool GetElem(SqList L,int i,int &e)
{//取值 
	if(i<1 || i>L.length) 
	 {
	 	cout << "访问位置不合法" << endl;
		 return false; 
	 }
	 e = L.elem[i-1];
	 return true;
}

int LocateElem(SqList L,int e)
{//查找 
	for(int i=0;i<L.length;i++)
	  if(L.elem[i] == e)
	    return i+1;
	return 0;    
}

bool ListInsert(SqList &L,int i,int e)
{//插入 
	if(i<1 || i>L.length+1)  
	   return false;
	if(L.length==MAXSIZE)
	   return OVERLOW;
	for(int j=L.length-1;j>=i-1;j--)
	    L.elem[j+1] = L.elem[j];
	L.elem[i-1] = e;
	++L.length;
	return true;	  	
}

bool ListDelete(SqList &L,int i)
{//删除 
	if(i<1 || i>L.length)
	   return false;
	for(int j=i;j<=L.length-1;j++)
	   L.elem[j-1]=L.elem[j];
	--L.length;
	return true;      
}

void ClearList(SqList &L)
{//清空 
	L.length=0;
}

int main()
{
	cout << "简单实现顺序表" << endl;
	cout << "------------------------------------------------" << endl;
	int m,n,a,b,c,d,f,g,h,i,x,y;
	SqList L;
	m=InitList(L);
	
    cout << "①建立顺序表"  << endl; 
	if(!m)
	  cout << "顺序表创建失败"  << endl;
	else
	  cout << "顺序表创建成功"  << endl;
	cout << "请输入五个数:" << endl;
	for(int i=1;i<=5;i++)
	{
		cin >> x;
		ListInsert(L,i,x);
	  }  
	 cout << "自定义顺序表为:" << endl; 
	 for(int i=1;i<=L.length;i++)
	 {
	 	GetElem(L,i,a);
	 	cout << a << " ";
	 }
     cout << endl <<"------------------------------------------------" << endl;
	 
	 cout << "②插入" << endl;
	 cout << "请输入插入位置与插入值:";
	 cin >> b >>c;
	 m=ListInsert(L,b,c);
	 if(!m)
	 {
	 	cout << "插入失败" <<endl;
		 if(m==false)
		   cout << "插入位置不合法" << endl;
		 if(m==OVERLOW)
		   cout << "当前存储空间已满" << endl;    
	 }
	 else
	 {
	 	cout << "插入成功,当前顺序表如下:" << endl; 
	 }
	 for(int i=1;i<=L.length;i++)
	 {
	 		GetElem(L,i,d);
	 	    cout << d << " ";
	 }
	 cout << endl <<"------------------------------------------------" << endl;
	
	 cout << "③删除" << endl;
	 cout << "请输入删除元素的位置:" ;
	 cin  >> f;
	 m=ListDelete(L,f);
	 if(!m)
	   cout << "删除位置不合法" <<endl;
	 else
	 {cout <<"删除成功,当前顺序表如下:" << endl;
	   for(int i=1;i<=L.length;i++)
	   {
	 		GetElem(L,i,y);
	 	    cout << y << " ";
	   }
	 }
	 cout<<endl;
	  
	    
	 cout <<"------------------------------------------------" << endl;
	 
	 cout << "④查找" << endl;
	 cout << "请输入要查找的值:";
	 cin >> g;
	 m=LocateElem(L,g);
	 if(!m)
	   cout <<  "顺序表中没有这个值" << endl;
	  else
	    cout << "此值的位置在:" << " "<< m << endl;  
	cout <<"------------------------------------------------" << endl;
	    
	 cout << "⑤取值" << endl;
	 cout << "请输入要取第几个值:";
	 cin >> h;
	 cout <<endl;
	 m=GetElem(L,h,i);
	 if(m)
	cout <<"这个值是:" << i <<endl;
	 
	 cout <<"------------------------------------------------" << endl;
	 
	 cout << "⑥清空" << endl;
	 ClearList(L);
	 cout <<"------------------------------------------------" << endl;
	 
	 return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值