C++ implements various basic operations of the sequence table

Comprehend the storage structure of the sequence table and master the design of various basic operation algorithms in the sequence table

Realize various basic operations of the sequence table and the overall table building algorithm (assuming the element type ElemType of the sequence table is char)

Program requirements:

1. Initialize the sequence table L;

2. Insert A, B, C, D, E elements in sequence;

3. Output sequence table L;

4. Determine the length of the sequence table L;

5. Determine whether the sequence list L is an empty list;

6. According to the input position of the user, output the elements of the corresponding sequence list L;

7. According to the element input by the user, output the position of the L element in the corresponding sequence table;

8. Insert a new element into the sequence table L according to the position and element input by the user;

9. Output sequence table L;

10. According to the position input by the user, delete the corresponding element in the sequence table L;

11. Output sequence table L;

12. Release sequence table L;

-------------------------------------------------------------------------------------------------------------------

Notice:

1. The difference between ElemType e and ElemType &e

2. Pay attention to the conversion of logical serial number and physical serial number

----------------------------------------------------------------------------------------------

Define the structure type of the linear table

#include <iostream.h>
#define MaxSize 100
typedef char ElemType;

typedef struct 
{
	ElemType data[MaxSize];
   	int length;
} SqList;   //定义线性表结构体类型 

1. Initialize the linear table function

//初始化线性表
void InitList(SqList *&L)   //引用型指针
{
	L= new SqList;       /*分配存放线性表的空间*/
    L->length=0;
}

2. Create a linear table function

//建立线性表
void CreateList(SqList *&L,ElemType a[],int n)  
{
	for (int i=0;i<n;i++)
		L->data[i]=a[i];
	L->length=n;
}

3. Destroy the linear table function

//销毁线性表
void DestroyList(SqList *&L)
{
	delete L;
}

4. Determine whether the linear table is an empty table function 

If the linear table is an empty table return true

If the linear table is not an empty table return false

//判定是否为空表
bool ListEmpty(SqList *L)
{
	return(L->length==0);
}

5. Find the length function of the linear table

//求线性表的长度
int ListLength(SqList *L)
{
	return(L->length);
}

6. Output linear table function

void DispList(SqList *L)
{
	if (ListEmpty(L))
		return;
	for (int i=0;i<L->length;i++)
		cout<<L->data[i]<<"   ";
	cout<<endl;
} 

7. Find the value of a data element

At this time i is a logical sequence number

Convert to physical serial number through i-1

//求某个数据元素值
bool GetElem(SqList *L,int i,ElemType &e)
{
	if (i<1 || i>L->length) 
		return false;
	e=L->data[i-1];
	return true;
}

8. Find by element value

//按元素值查找
int LocateElem(SqList *L, ElemType e)
{
	int i=0;
	while (i<L->length && L->data[i]!=e) 
		i++;
	if (i>=L->length)
		return 0;
	else 
		return i+1;
}

9. Insert data elements

//插入数据元素
bool ListInsert(SqList *&L,int i,ElemType e)
{
    if (i<1 || i>L->length+1)
		return false;
    i--;  /*将顺序表逻辑位序转化为elem下标即物理位序*/
    for (int j=L->length;j>i;j--)/*将data[i]及后面元素后移一个位置*/
		L->data[j]=L->data[j-1];
                
	L->data[i]=e;   //将新元素插入该位置
	L->length++;      /*顺序表长度增1*/
	return true;
}

10. Delete data elements

//删除数据元素
bool ListDelete(SqList *&L,int i,ElemType &e)
{
     int j;
     if (i<1 || i>L->length)
		 return false;
     i--;	   /*将顺序表逻辑位序转化为elem下标即物理位序*/
     e=L->data[i];
     for (j=i;j<L->length-1;j++) L->data[j]=L->data[j+1];
            /*将data[i]之后的元素前移一个位置*/
     L->length--;	/*顺序表长度减1*/
     return true;
}

----------------------------------------------------------------------------------------------

main function

You can use the ListInsert function or the CreateList function to create a table

void main()
{
	SqList *L1;
	cout<<"1.初始化顺序表:"<<endl;InitList(L1);

	cout<<"\n2.尾插法建表:"<<endl;
	ElemType a[5]={'A','B','C','D','E'};
    for(int i=0;i<5;i++)
		if(!ListInsert(L1,i+1,a[i]))
			cout<<"插入失败!";
	cout<<"\n3.顺序表的元素为:";
	DispList(L1);


	cout<<"\n4.该顺序表的长度为:"<<ListLength(L1)<<endl;

	cout<<"\n5.该顺序表";
	if(ListEmpty(L1))
		cout<<"为空!"<<endl;
	else
		cout<<"不为空!"<<endl;


	cout<<"\n6.取元素";
	ElemType temp;cout<<"请输入取的位置:";int k;cin>>k;
	if(GetElem(L1,k,temp))
		cout<<"取值成功,该顺序表的第"<<k<<"个元素的值为:"<<temp<<endl;
	else
		cout<<"取值失败,你输入的位置"<<k<<"越界:"<<endl;

  
	cout<<"\n7.查找元素:"<<endl<<"请输入查找元素的值:";cin>>temp;
	if(LocateElem(L1,temp))
		cout<<"输出元素'"<<temp<<"'的位置为:"<<LocateElem(L1,temp)<<endl;
	else
		cout<<"元素'"<<temp<<"'不存在."<<endl;
  
	cout<<"\n8.在顺序表指定位置插入元素 :"<<endl;
	cout<<"请输入插入的位置:";cin>>k;
	cout<<"请输入插入元素的值:";cin>>temp;

	if(ListInsert(L1,k,temp))
		cout<<"插入成功"<<endl;
	else
		cout<<"插入失败!"<<endl;

	cout<<"\n9.输出顺序表"<<endl;
	DispList(L1);
	
	cout<<"\n10.删除顺序表指定位置的元素"<<endl;
	cout<<"请输入删除的位置:";cin>>k;
	if(ListDelete(L1,k,temp))
		cout<<"删除成功,删除的元素为:"<<temp<<endl;
	else
		cout<<"删除失败!"<<endl;

	cout<<"\n11.输出顺序表"<<endl;
	DispList(L1);
	cout<<"\n12.释放顺序表"<<endl;
	DestroyList(L1);

}

Guess you like

Origin blog.csdn.net/henry594xiaoli/article/details/123619337