本文主要是介绍线性表(顺序存储)结构与功能的简易实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
"SqList.h"
#pragma once
#define LIST_INIT_SIZE 10 //初始分配量
#define LISTINC 5 //增长量template<class T>
class SqList
{
public:typedef T ElemType;SqList(void){elem = (T *)malloc(LIST_INIT_SIZE*sizeof(T));if(!elem){throw bad_alloc();}else{length = 0;listsize = LIST_INIT_SIZE;}
}
//数组初始化--------------------OK!SqList(T *q,int n){elem = (T *)malloc(n*sizeof(T));if(!elem){throw bad_alloc();}else{length = n;listsize = n;for(size_t i = 0;i<size_t(n);i++){elem[i] = q[i];}}
}
//深拷贝-----------------OK!SqList(SqList & s){size_t l = s.GetLength();elem = (T *)malloc(l*sizeof(T));if(!elem){throw bad_alloc();}else{length = l;listsize = l;}for(size_t j = 0;j<length ;j++){elem[j] = s[j];}}~SqList(void){free(elem);}//插入数据-------------------------OK!bool insert(int i,ElemType e){if(i<1||size_t(i)>length){ return false;}T * newbase = 0;if(length>=listsize){newbase = (T *)realloc(elem,(listsize + LISTINC)*sizeof(T));if(!newbase){throw bad_alloc();}elem = newbase;listsize += LISTINC;}//获取插入点T * q = &elem[i-1];//开始向后移动数据for(int j = length -1 ;j >= (i-1);j--){elem[j+1] = elem[j];}//插入数据*q = e;length++;return true;
}
//增加元素-----------------------------OK!bool add(ElemType e){T * newbase;if(length>=listsize){newbase = (T *)realloc(elem,(listsize + LISTINC)*sizeof(T));if(!newbase){throw bad_alloc();}elem = newbase;listsize += LISTINC;}//增加元素elem[length] = e;length++;return true;
}
//删除元素-----------------------------------------OK!bool del(int i,ElemType &e){if(i <1||i>length){return false;}//找到删除点T * q = &elem[i-1];e = *q;//开始向前移动for(int j = i ;j<=length+1;j++){elem[j-1] = elem[j];}length--;return true;
}
//查找第一个于e相等的元素------------------------OK!int find_first(T e )
{size_t i = 1;for(;(i<=length)&&(e!=elem[i-1]);i++);if(i<=length) return i-1;return 0;
}
//查找元素(查找第一个与e满足compare关系的元素)-------------------OK!int find_first(T e ,bool (* compare )(T,T)){size_t i = 1;for(;(i<=length)&&(!(*compare)(e,elem[i-1]));i++);if(i<=length) return i;return 0;
}
//重载[]操作符函数---------------------------------OK!T & operator[](int i)
{if((i<0)||size_t(i)>(length-1)){throw range_error("range_error");}return elem[i];
}
//赋值操作符重载------------------------------------OK!SqList & operator=(SqList & s){size_t l = s.GetLength();T * newbase;if(l>listsize){newbase = (T *)realloc(elem ,l*sizeof(T));if(!newbase){throw bad_alloc();}elem = newbase;listsize = l;length = l;}length = l;for(size_t j = 0;j<length ;j++){elem[j] = s[j];}return *this;}
//有序合并-----------------------------OK!bool MergeByOrder(SqList & b,SqList & c ){SqList inis;size_t i =0;size_t j =0;while((i<length) &&(j<b.GetLength())){if(elem[i]<=b[j]) {inis.add(elem[i]);i++;}else {inis.add(b[j]);j++; }}while(i<length) {inis.add(elem[i]);i++;}while(j<b.GetLength()){inis.add(b[j]);j++;}c = inis;return true;}
//无序合并------------------------------OK!bool Merge(SqList & b,SqList & c){c = (*this);size_t i = 0;for(;i<b.GetLength();i++){c.add(b[i]);}return true;}
//得到长度-------------------------------------------------OK!size_t GetLength(){return length;
}
//排序(排成非降序列)void sort(){for(int i =length-1;i>0;i--){for(int j = 0;j<i;j++){if(elem[j]>=elem[j+1]) {T t = elem[j];elem[j] = elem[j+1];elem[j+1]=t;}}}}
//重载排序void sort(bool (* compare)(T ,T)){for(int i =length-1;i>0;i--){for(int j = 0;j<i;j++){if((*compare)(elem[j],elem[j+1])) {T t = elem[j];elem[j] = elem[j+1];elem[j+1]=t;}}}}
private:T * elem;size_t length;//实际长度size_t listsize;//当前存储容量
};
源.cpp
#include "SqList.h"
#include<iostream>
using namespace std;
bool fun(int a,int b ){return a<=b;}
bool fun1(int a,int b ){return a=b;}int main()
{/**三种初始化方式*///建立一个空的线性表SqList<double> Sad;SqList<float> Saf; //用数组初始化线性表int a[5] = {2,3,1,0,9}; SqList<int> sb1(a,5);//赋值构造一个线性表SqList<int> sb2(sb1);SqList<int> sb3 = sb1;/**支持直接赋值操作*/SqList<int> sb4;sb4 = sb3;/**支持返回元素个数的操作,注意返回值类型是size_t类型*/size_t ll = sb1.GetLength();cout<<"sb1的长度:"<<ll<<endl;/**支持下标操作*/cout<<"sb1序列(用数组初始化线性表):"<<flush;for(size_t i =0;i<sb1.GetLength();i++)//输出sb1{cout<<sb1[i]<<" "<<flush;}cout<<endl;cout<<"sb2序列(赋值构造一个线性表):"<<flush;for(size_t i =0;i<sb2.GetLength();i++)//输出sb2{cout<<sb2[i]<<" "<<flush;}cout<<endl;cout<<"sb3序列(赋值构造一个线性表):"<<flush;for(size_t i =0;i<sb3.GetLength();i++)//输出sb3{cout<<sb3[i]<<" "<<flush;}cout<<endl;cout<<"sb4序列(直接赋值操作):"<<flush;for(size_t i =0;i<sb4.GetLength();i++)//输出sb4{cout<<sb4[i]<<" "<<flush;}cout<<endl;/**支持增加元素*/sb1.add(8);cout<<"sb1增加元素8:"<<flush;for(size_t i =0;i<sb1.GetLength();i++)//输出sb1{cout<<sb1[i]<<" "<<flush;}cout<<endl;/**支持定点插入操作*/cout<<"sb1插入元素7:"<<flush;sb1.insert(1,7);//在位置1之前插入数据7for(size_t i =0;i<sb1.GetLength();i++)//输出sb1{cout<<sb1[i]<<" "<<flush;}cout<<endl;/**支持删除元素操作 */int de ;sb1.del(1,de);cout<<"被删除的元素:"<<de<<endl;//输出被删除的元素值/**查找第一个于e相等的元素位置下标*/int ii;ii=sb1.find_first(3);cout<<"查找元素3:"<<sb1[ii]<<endl;/**查找第一个与e符合函数关系的元素位置下标(函数重载)*/ii=sb1.find_first(3,fun1);cout<<"查找元素3(重载):"<<sb1[ii]<<endl;/**支持线性表的排序操作*/sb1.sort();cout<<"sb1序列排序后(升序):"<<flush;for(size_t i =0;i<sb1.GetLength();i++)//输出sb1{cout<<sb1[i]<<" "<<flush;}cout<<endl;/**支持线性表的排序操作(关系函数重载)*/sb1.sort(fun);cout<<"sb1序列排序后(重载)(降序):"<<flush;for(size_t i =0;i<sb1.GetLength();i++)//输出sb1{cout<<sb1[i]<<" "<<flush;}cout<<endl;/**支持线性表的无序合并操作*/int b[4]={1,4,3,2};SqList<int> bs(b,4);SqList<int> is;sb1.Merge(bs,is);cout<<"与序列“{1,4,3,2}合并”:"<<flush;cout<<"合并后的序列(无序合并):"<<flush;for(size_t i =0;i<is.GetLength();i++)//输出合并后的序列{cout<<is[i]<<" "<<flush;}cout<<endl;/**支持线性表的有序合并操作(函数重载)*/sb1.sort();//先排序bs.sort();//先排序sb1.MergeByOrder(bs,is);cout<<"与序列“{1,4,3,2}合并”:"<<flush;cout<<"合并后的序列(有序合并):"<<flush;for(size_t i =0;i<is.GetLength();i++)//输出合并后的序列{cout<<is[i]<<" "<<flush;}cout<<endl;}
这篇关于线性表(顺序存储)结构与功能的简易实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!