当前位置:网站首页>Class question: how to ensure that line table storage can be inserted at any time?

Class question: how to ensure that line table storage can be inserted at any time?

2022-06-11 18:03:00 Hold on to the idea and cut the river

Today, the teacher's data structure class , I asked this question in class , When a linear table is stored sequentially , How to ensure that it can be inserted at any time ? The sequence table insertion code is as follows :

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

Problem solving

Divided into two steps , Mainly aimed at ERROR To resolve the errors that have occurred

1、ERROR1:i<1 || i> Length+1

If i<1 Then let him i = 1
If i>Length + 1 Then let him Length+1

2、ERROR2:L.length>=MAXSIZE

Use this time c Linguistic realloc, This function is to grow the array on its original basis .malloc And c++ Of new You need to rewind the array , therefore realloc better .

原网站

版权声明
本文为[Hold on to the idea and cut the river]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011854321413.html