欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单实现顺序表

程序员文章站 2022-03-10 19:54:56
...
#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;
}

简单实现顺序表

相关标签: 数据结构