线性表的操作

   

    #include<iostream>
        #include<stdlib.h> 
        using namespace std;
        typedef int ElemType; 
        struct List{
        	ElemType *list;
        	int size;
        	int MaxSize;
        };
    	
    void InitList (List &L)
    
    {
    	L.MaxSize = 10;//初始定义数组长度为10,以后可以增减,或者附加一个形参给定初始数组长度
    	L.list=new ElemType[L.MaxSize];//动态存储空间分配 
    	 if(L.list == NULL){
    	 	cout<<"动态可分配的存储空间用完,退出运行!"<<endl;
    		 exit(1);
    		  
    	 }
    	 L.size = 0;//线性表长度为0,即为空表 
    	
    }//初始化线性表
    void ClearList(List &L){
    	if(L.list!= NULL) {
    		delete [] L.list;
    		L.list= NULL;
    	}
    	L.MaxSize = 0;
    	L.size = 0;
    } //删除线性表中的所有元素,使之成为一个空表
    
    int LenthList (List &L) {
    	return L.size;
    } //得到线性表的长度
    
    bool EmptyList ( List &L) {
    	return L.size == 0;
    } //检查线性表是否为空
    
    ElemType GetList (List &L, int pos) {
    	if(pos<1||pos>L.size)  //若pos越界则退出程序
    	{
    	cerr<<"pos is out range!"<<endl;
    	exit(1);
    	 
    } 
    return L.list[pos-1];}  //返回线性表中指定序号为pos的元素 
     //得到线性表指定序号为pos的元素 
     
     void TraverseList (List &L)
     {
     	for (int i = 0; i<L.size; i++)
     	cout<<L.list[i]<<' ';
      cout<<endl;
      } 
    //遍历线性表
    
    bool FindList(List &L ,ElemType& item)
    {
    	for (int i = 0; i<L.size; i++)
    	 if(L.list[i]==item){
    	 	item  = L.list[i];
    	 	return true;
    	 }
    	 return false;
    	} //从线性表中查找具有给定值的第一个元素
    	 
     
     
     bool UpdateList (List &L, const ElemType& item){
     	for(int i = 0; i<L.size; i++)
     	  if(L.list[i]==item){
     	  	L.list[i]=item ;    //进行修改更新赋值操作 
     	  	return true;
     	  	
    	   }
    	   return false;
     }//更新线性表中具有给定值的第一个元素 
     
     bool InsertList(List &L, ElemType item, int pos)
     {
     	//检查pos值是否有效,若无效则无法插入,返回假
    	if(pos<-1||pos>L.size+1){
    		cout<<"pos值无效!"<<endl; 
    		return false; 
    	} 
    	//求出按值有序插入时item的插入位置,使之保存到pos中
    	int i;
    	if(pos==0){
    		for(i = 0; i<L.size; i++)
    		if(item<L.list[i]) 
    		break;
    		pos= i + 1; //pos中保存新插入的元素的序号 
    		} 
    		//得到表为插入位置,被保存在pos中
    		else if(pos == -1) 
    		pos = L.size + 1;
    		//若线性表存储空间用完,则重新分配大一倍的存储空间
    		if(L.size == L.MaxSize) {
    			int k = sizeof(ElemType);//计算每个元素存储空间的长度
    			L.list = (ElemType*)realloc (L.list, 2*L.MaxSize*k);
    			//线性表存储空间扩展为原来的两倍,原内容不变
    			if(L.list ==NULL){
    				cout<<"动态分配的存储空间用完,退出运行!"<<endl;
    				exit(1); 
    			}  
    			L.MaxSize = 2*L.MaxSize;//把线性表的空间大小修改为新的长度 
    		}
    
    	//待插入位置及所有后续位置元素,从后往前一次后移一个位置
    	for(i = L.size-1; i>pos -1; i--)
    	   L.list[i+1] = L.list[i];
    	   //把item的值赋给已空出的、下标为pos-1的位置,他为第Pos各元素
    	   L.list[pos - 1] = item;
    	   //线性表长度增一
    	   L.size++;
    	   //返回真表示插入成功
    	   return true; 
    	} 
    	//向线性表中按给定条件插入一个元素
    	
    	bool DeleteList(List &L, ElemType& item, int pos)
    	{
    		//检查线性表是否为空,若是则无法删除,返回假
    		if(L.size == 0){
    			cout<<"线性表为空,删除无效!"<<endl;
    			return false; 
    		}
    		//检查pos表是否有效,若无效则无法删除,返回假
    		if(pos<-1 || pos>L.size) {
    			cout<<"pos值无效"<<endl;
    			return false; 
    		}
    		//求出按值删除时item的删除位置,使之保存到pos里
    		int i = 0;
    		if(pos == 0){
    			for(i= 0; i<L.size; i++)
    			if(item == L.list[i])
    			break;
    			if(i == L.size)
    			return false;//无元素删除返回假
    			pos = i+ 1; 
    		} 
    		
    		//得到表尾元素的序号,被保存再pos中
    		else if (pos == -1) 
    		pos = L.size;
    		//把被删除的元素的值赋值给变参item带回
    		item = L.list[pos - 1];
    		//将待删除的元素位置后面的所有元素,从前向后依次前移一个位置
    		for(i = pos; i<L.size; i++)
    		L.list[i-1] = L.list[i];
    		//线性表长度减一
    		L.size--;
    		//若线性表存储空间空余太多,则进行适当削减
    		if(float(L.size)/L.MaxSize<0.4 && L.MaxSize>10){
    			int k = sizeof(ElemType);
    			L.list = (ElemType*)realloc(L.list, L.MaxSize*k/2);
    			//线性表动态存储空间缩减为原来的一半
    			L.MaxSize = L.MaxSize/2;
    			//把线性表的空间大小修改为新的长度 
    		} 
    		return true;
    		//返回真表示删除成功 
    		
    	 } 
    	 
    	 
    	 void SortList(List &L){
    	 	//将L中的所有元素按给定条件排序
    		 int i , j ;
    		 ElemType x;
    		 for(i= 1 ; j<L.size; i++) //共循环n-1次
    		 {
    		 x = L.list[i];  //把无序表中的第一个元素暂存x 
    		 for(j= i-1;j>=0; j--)    //向前顺序进行比较和移动
    		  if(x<L.list[j]) 
    		  L.list[j+1] = L.list[j];
    		  else break;
    		  L.list[j+1]= x;
    	 }
    }
    	int main()
    	 {
    	 	int a[12] = {3,6,9,12,15,18,21,24,27,30,33,36};
    	 	int i ; 
    	 	ElemType x ; 
    	 	List t; 
    	 	InitList(t);
    	 	for(i = 0 ; i<12 ; i++)
    	 	InsertList(t,a[i],i+1);
    	 	InsertList(t,48,13);InsertList(t,40,0);
    	 	cout<<GetList(t,4)<<' '<<GetList(t,9)<<endl;
    		 TraverseList(t);
    		 cout<<"输入待查找的元素值:";
    		 cin>>x;
    		 if(FindList(t,x))
    		 cout<<"查找成功!"<<endl;
    		 else cout<<"查找失败!"<<endl;
    		 cout<<"输入待删除元素的值:";
    		 cin>>x;
    		 if(DeleteList(t,x,0))
    		 cout<<"删除成功!"<<endl;
    		 else cout<<"删除失败!"<<endl;
    		 for(i = 0 ; i<6 ; i++)
    		    DeleteList(t,x,i+1);
    		  TraverseList(t);
    		  cout<<"按值插入,输入待插入元素的值:";
    		  cin>>x;
    		  if(InsertList(t,x,0))
    		  cout<<"插入成功!"<<endl;
    		  else cout<<"插入失败!"<<endl;
    		  TraverseList(t);
    		  cout<<"线性表长度:"<<LenthList(t)<<endl;
    		  if(EmptyList(t))
    		  cout<<"线性表为空!"<<endl;
    		  else cout<<"线性表不空!"<<endl; 
    		  ClearList(t); 
    	 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值