DS004-Structure template description dynamic sequence table

1. Introduction to dynamic sequence table

Data with a one-to-one relationship constitutes a linear structure logically, and is logically represented by a linear table.

The concept and characteristics of the linear table, I am not wordy.

The physical storage of linear tables can be realized by sequential tables (occupying continuous space) and linked lists,

The sequence table can be divided into static sequence table and dynamic sequence table.

The static sequence table is an array, once defined, the size is fixed, because the array name is a constant in many languages;

The dynamic sequence table is a dynamic array, and the first address of the applied space is stored with a pointer variable, which can be changed.

2. In what way to express the dynamic sequence table

If students have studied C++, it is most appropriate to choose C++ template class to represent the abstract data type on the data structure;

If the student has not studied C++, there are two options

One is selected by most textbooks, using the structure of C language;

One is what I recommend here, using structure templates in C++.

Only member variables can be defined in the structure of C language; member variables and member functions can be defined in the structure of C++;

C++ structures can also take the form of templates to handle common types.

The syntax of C++ structure is simpler than that of class.

Three, the simple realization of the sequence table

Here we use the C++ structure template as an abstract data type to implement part of the operation of the dynamic sequence table .

Enable beginners to grasp the idea of ​​sequence table from the level of code.

#include<iostream>//C++的输入输出
using namespace std; 
template<typename ElemType> struct SqList
{
	ElemType *elem;//存储空间首地址
	int length;//线性表中元素个数
	int listsize;//当前线性表的最大容量
	
	int InitList()
	{//对顺序表进行初始化
		elem=new ElemType[16];//这个正整数随便设置 
		if(0==elem)
			return 0;
		listsize=16;//初始时,有16个空间 
		length=0; //没有数据元素 
	}
	
	int enlarge()
	{//当L的空间不足时,本函数功能:将L的空间翻倍
	//如果成功返回1,失败返回0
	
		ElemType *newbase=new ElemType[listsize*2];
		if(0==newbase)
			return 0;
		for(int i=0;i<length;i++)
			newbase[i]=elem[i];
		delete []elem;
		elem=newbase;
		listsize=listsize*2;
		return 1;		
	} 
	
	int ListInsert(int i,ElemType x)
	{//在顺序表的第i个位置插入元素x
	//如果成功,返回1,否则返回0

		if(i<0||i>length) return 0;
		if(length==listsize)
		{
			int flag=enlarge();
			if(flag==0)return 0;
		}
		int j;
		for(j=length-1;j>=i;j--)
			elem[j+1]=elem[j];
		elem[i]=x;
		length++;
		return 1;
	}
	
	int pop_i(int i)
	{//删除下标为 i 的数据 
		if(i<0 || i>=length)//如果删除位置不合法
			return -1;
		else
		{
			for(int j=i+1;j<=length-1;j++)
				elem[j-1]=elem[j];//元素前移 一位
			length--;
			return 1;
		}
	}
	
	int find(ElemType e)
	{//在顺序表中查找元素e是否存在,
		//如果存在返回对应的下标
		//否则返回-1
		for(int i=0;i<=length-1;i++)
		{
			if(elem[i]==e)
				return i;
		}
		return -1;
	} 
		
	
	void print()
	{
		for(int i=0;i<length;i++)
			cout<<elem[i]<<" ";
		cout<<endl;
	}
	

};

int main()
{
	SqList<int> LA;	
	LA.InitList(); 
	for(int i=0;i<20;i++)
	{
		LA.ListInsert(i,i);		
	}
	
	LA.print();
	LA.pop_i(0);
	LA.print();
	
	cout<<LA.find(5)<<endl;
	

	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43917370/article/details/108416342