Initialization, value acquisition, search, insertion and deletion of linear tables (sequential tables) (c++)

1. Problem description:

Basic operations and descriptions in sequence tables (basic operations include initializing linear tables, taking values, finding elements, inserting elements, and deleting elements)

2. Function:

(1) Initialize linear table:

Construct an empty sequence table and set the length of the table to 0. The specific code is implemented as follows:

Status InitList(SqList &L)
{
	L.elem=new ElemType[MAXSIZE];
	if(L.elem==NULL) return ERROR;
	L.length=0;
	return OK;
}

(2) Store value

This use case only stores 5 pieces of data for convenience. You can change the number of loops to increase the number of elements initially stored in the linear table.

Use a loop to store 5 values ​​​​into the sequence table. Each time you save, add one to the length of the table. Remember that the L in the formal parameter must be quoted.

Use transfer, otherwise the linear table will still be an empty linear table after saving.

void Creat_n(SqList &L)//参数一定要使用引用
{
	ElemType c;
	printf("请输入线性表的5个值:\n");
	for(int i=1;i<=5;i++){
		scanf("%d",&c);
		L.elem[i]=c;
		L.length++;
	}
} 

(3) Value of element

Get the value by entering the position of the element to get the value, and assign the obtained result to the same variable type e. Here e also needs to be passed by reference, because after assigning the obtained value to the variable e, e The value has changed. At the same time, the input i value (the position of the element taken) needs to be judged. If the i value is reasonable, you can continue. Otherwise, you need to find a safe exit to exit (return ERROR). The specific code is as follows:

Status GetElem(SqList L,int i,ElemType &e) 
{
	if(i<1 || i>L.length) return ERROR;
	e=L.elem[i];
	return OK;
} 

(4) Search for elements

To search for elements, use one-by-one comparison. Starting from the first element, it is compared with the passed-in value e. If they are the same, i+1 is returned. If there is no value that is the same as the passed-in element, 0 is returned. The specific code is implemented as follows:

int LocateElem(SqList L,ElemType e)
{
	for(int i=1;i<=L.length;i++)
		if(L.elem[i]==e) return i;
	return 0;
}

(5) Insertion of elements

To insert an element, first determine whether the inserted position is reasonable, and (i<1 || i>L.length+1). If it is unreasonable, return the ERROR value. Secondly, determine whether the length of the linear table has reached the maximum value MAXSIZE. If Reached, the value ERROR is also returned. When the insertion position is appropriate and the length of the linear table does not reach the maximum value, the element is inserted, and all elements behind the position to be inserted are moved backward one position in sequence (starting from the back), and the The element is inserted into the specified position. After insertion, the length of the linear table is increased by 1. The specific code is implemented as follows:

Status ListInsert(SqList &L,int i,ElemType e)
{
	if(i<1 || i>L.length+1) return ERROR;
	if(L.length==MAXSIZE) return ERROR;
	for(int j=L.length;j>=i;j--)
		L.elem[j+1]=L.elem[j];
	L.elem[i]=e;
	++L.length;
	return OK;
} 

(6) Deletion of elements

Just like the insertion of elements, you need to judge whether the position of the deleted element is reasonable, that is (i<1 || i>L.length), but you do not need to judge whether the length of the linear table has reached the maximum value. When deleting elements, you need to Move the elements behind this element forward one position one by one. After deleting the element, reduce the length of the linear table by 1. The specific code is implemented as follows:

Status ListDelete(SqList &L,int i){
	if(i<1 || i>L.length) return ERROR;
	for(int j=i+1;j<=L.length;j++)
		L.elem[j-1]=L.elem[j];
	--L.length;
	return OK;
} 

(7) Display of linear table

The display of the linear table can directly use a loop to traverse each element in the linear table. The L here does not need to be passed as a parameter, because it only simply traverses the output elements without changing the values ​​of the linear table elements. The specific code is as follows:

void Display(SqList L)
{
	printf("当前线性表为:\n");
	for(int i=1;i<=L.length;i++)
		 printf("%d ",L.elem[i]);
	printf("\n");
}

3. Overall code (comments and running screenshots)

code

#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 10
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct
{
	ElemType *elem;
	int length; 
}SqList;
 
//线性表的初始化 
Status InitList(SqList &L)
{
	L.elem=new ElemType[MAXSIZE];
	if(L.elem==NULL) return ERROR;
	L.length=0;
	return OK;
}
 
//线性表的存值
void Creat_n(SqList &L)
{
	ElemType c;
	printf("请输入线性表的5个值:\n");
	for(int i=1;i<=5;i++){
		scanf("%d",&c);
		L.elem[i]=c;
		L.length++;
	}
} 

//线性表的取值
Status GetElem(SqList L,int i,ElemType &e) 
{
	if(i<1 || i>L.length) return ERROR;
	e=L.elem[i];
	return OK;
} 
 
//线性表的查找元素
int LocateElem(SqList L,ElemType e)
{
	for(int i=1;i<=L.length;i++)
		if(L.elem[i]==e) return i;
	return 0;
}
 
//线性表的插入元素
Status ListInsert(SqList &L,int i,ElemType e)
{
	if(i<1 || i>L.length+1) return ERROR;
	if(L.length==MAXSIZE) return ERROR;
	for(int j=L.length;j>=i;j--)
		L.elem[j+1]=L.elem[j];
	L.elem[i]=e;
	++L.length;
	return OK;
} 
 
//线性表的删除元素
Status ListDelete(SqList &L,int i){
	if(i<1 || i>L.length) return ERROR;
	for(int j=i+1;j<=L.length;j++)
		L.elem[j-1]=L.elem[j];
	--L.length;
	return OK;
} 	
 
//打印线性表
void Display(SqList L)
{
	printf("当前线性表为:\n");
	for(int i=1;i<=L.length;i++)
		 printf("%d ",L.elem[i]);
	printf("\n");
}
 
int main()
{
	SqList L;
	ElemType e;
	int v,k,opt,p;
	p=InitList(L);
	if(p==0) printf("初始化失败!");
	else{
		printf("初始化成功\n");
		printf("请存入5个元素到线性表中\n"); 
		Creat_n(L); 
		printf("1:取出线性表中的某个值\n");
		printf("2:查找线性表中的元素\n");
		printf("3:将线性表中插入一个元素\n");
		printf("4:将线性表中删除一个元素\n");
		printf("5:展示当前线性表\n");
		printf("6:退出\n"); 
		
		while(1){	
			printf("请输入您的选择:");
			cin>>opt;
			if(opt==1){
				//取出线性表中的某个值 
				printf("请输入要取出值的位置:"); 
				cin>>v;
				p=GetElem(L,v,e);
				if(p==0) printf("输入位置非法\n");
				else
					printf("该值为:%d\n",e); 
			}
			else if(opt==2){
				//进行元素的查找
				printf("请输入您要查找的元素:");
				cin>>v;
				k=LocateElem(L,v);
				if(k==0) printf("输入位置非法\n");
				else
					printf("您所查找的元素所在位置为:%d\n",k);
			}
			else if(opt==3){
				//线性表元素的插入
				printf("请输入您要插入的元素及插入的位置:");
				cin>>v>>k;
				p=ListInsert(L,k,v);
				if(p==0) printf("输入位置非法\n");
				else
					Display(L);
			}
			else if(opt==4){ 
				//线性表元素的删除 
				printf("请输入您要删除第几个元素:");
				cin>>v; 
				p=ListDelete(L,v);
				if(p==0) printf("输入位置非法\n");
				else
					Display(L);
			}
			else if(opt==5){
				Display(L);
			}
			else if(opt==6){
				printf("退出成功");
				break; 
			}
		}
	}
}

Run screenshot

3. Analysis of the advantages and disadvantages of linear tables (sequential tables):

Advantages: Sequential tables can randomly access any element in the table

Disadvantages: Inserting and deleting elements is too troublesome, requires moving a large amount of data, the operation process is relatively complicated, and will lead to a waste of storage space.

Guess you like

Origin blog.csdn.net/m0_74472474/article/details/132817651