Algorithms & Data Structures - Linear tables and their sequential storage structures

This article mainly introduces the theory and examples related to linear tables, including the addition and deletion operations of linear tables, sequential storage structures, and the code in this article.

Table of contents

linear table

Linear table definition

Linear table abstract data type

Linear table sequential storage structure

definition

sequential storage

Sequential Storage Structure Operations

get element

display elements

insert operation

delete operation

Advanced operation


linear table

Linear table, the full name is linear storage structure . It is a very important type of data structure, the most commonly used and the simplest.

Linear table definition

a finite sequence of zero or more data elements

        The relationship between data elements in a linear table is a one-to-one relationship, that is, except for the first and last data elements, other data elements are connected end to end.

        Note 1: This sentence only applies to most linear tables, not all. For example, a circular list (which will be mentioned later) is also a linear list at the logical level (it belongs to chain storage at the storage level, but the tail pointer of the last data element points to the first node).

        Note 2: Looking back at what was said before, a data element is not necessarily the smallest unit, and a data element can consist of several data items.

        Note 3: The number n of elements in the linear table is defined as the length of the linear table. When n=0, it is called an empty table.

Linear table abstract data type

Knowing the definition of the linear table, what kind of operations does the linear table pharmacy have? We can familiarize ourselves with the common basic operations first: (just get a general understanding, bookmark articles, and read them as you use them)

  1. MakeEmpty(L) This is a method that turns L into an empty list
  2. Length(L) returns the length of the table L, that is, the number of elements in the table
  3. Get(L,i) This is a function whose value is the element at position i in L (1≤i≤n)
  4. Prior(L, i) takes the predecessor element of i
  5. Next(L, i) takes the successor element of i
  6. Locate(L, x) This is a function whose value is the position of element x in L
  7. Insert(L, i, x) inserts element x at position i of table L, and pushes back the element that originally occupied position i and the following elements by one position
  8. Delete(L,p) deletes the element at position p from the list L
  9. IsEmpty(L) returns true if the table L is an empty table (length is 0), otherwise returns false
  10. Clear(L) clears all elements
  11. Init (L) is the same as the first one, the initialization linear table is empty
  12. Traverse(L) traverses and outputs all elements
  13. Find(L, x) finds and returns an element
  14. Update(L, x) modifies the element
  15. Sort(L) reorders all elements according to the given condition
  16. strstr(string1, string2) is used to find the first address of string2 in string1 in character array

Linear table sequential storage structure

There are two physical structures of linear tables. Here we will first talk about simple sequential storage.

definition

The data elements of the linear table are sequentially stored in a storage unit with continuous addresses.

The storage structure that stores the data with a one-to-one  logical relationship sequentially in an entire physical space is a sequential storage structure.

sequential storage

Look at the sequential storage structure code:

#define MAXSIZE 20          /* 存储空间初始分配量 */
typedef int ElemType;       /* ElemType类型根据实际情况而定,这里假设为int */
typedef struct
{
    ElemType data[MAXSIZE]; /* 数组,存储数据元素 */
    int length;             /* 线性表当前长度 */
}SqList;

Therefore, the length of the array is not necessarily equal to the length of the linear table. The length of the linear table should be less than or equal to the length of the array.

/* 初始化顺序线性表 */
Status InitList(SqList *L) 
{ 
    L->length=0;
    return OK;
}

/* 初始条件:顺序线性表L已存在。操作结果:若L为空表,则返回TRUE,否则返回FALSE */
Status ListEmpty(SqList L)
{ 
	if(L.length==0)
		return TRUE;
	else
		return FALSE;
}

/* 初始条件:顺序线性表L已存在。操作结果:将L重置为空表 */
Status ClearList(SqList *L)
{ 
    L->length=0;
    return OK;
}

/* 初始条件:顺序线性表L已存在。操作结果:返回L中数据元素个数 */
int ListLength(SqList L)
{
	return L.length;
}

Sequential Storage Structure Operations

get element

/* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L) */
/* 操作结果:用e返回L中第i个数据元素的值,注意i是指位置,第1个位置的数组是从0开始 */
Status GetElem(SqList L,int i,ElemType *e)
{
    if(L.length==0 || i<1 || i>L.length)
            return ERROR;
    *e=L.data[i-1];

    return OK;
}

display elements

/* 初始条件:顺序线性表L已存在 */
/* 操作结果:依次对L的每个数据元素输出 */
Status ListTraverse(SqList L)
{
	int i;
    for(i=0;i<L.length;i++)
            visit(L.data[i]);
    printf("\n");
    return OK;
}

insert operation

Let's think about it together:

  1. The length of the linear table cannot be greater than that of the array
  2. Insert unreasonable exceptions to be able to handle
  3. All elements after the insertion position are moved backwards
  4. insert element
  5. table length +1

The C language code is as follows:

/* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L), */
/* 操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1 */
Status ListInsert(SqList *L,int i,ElemType e)
{ 
	int k;
	if (L->length==MAXSIZE)  /* 顺序线性表已经满 */
		return ERROR;
	if (i<1 || i>L->length+1)/* 当i比第一位置小或者比最后一位置后一位置还要大时 */
		return ERROR;

	if (i<=L->length)        /* 若插入数据位置不在表尾 */
	{
		for(k=L->length-1;k>=i-1;k--)  /* 将要插入位置之后的数据元素向后移动一位 */
			L->data[k+1]=L->data[k];
	}
	L->data[i-1]=e;          /* 将新元素插入 */
	L->length++;

	return OK;
}

delete operation

Ideas:

  1. Unreasonable exceptions can be handled
  2. remove element
  3. The element after the deleted element moves forward one position
  4. table length-1

The C language code is as follows:

/* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L) */
/* 操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1 */
Status ListDelete(SqList *L,int i,ElemType *e) 
{ 
    int k;
    if (L->length==0)               /* 线性表为空 */
		return ERROR;
    if (i<1 || i>L->length)         /* 删除位置不正确 */
        return ERROR;
    *e=L->data[i-1];
    if (i<L->length)                /* 如果删除不是最后位置 */
    {
        for(k=i;k<L->length;k++)/* 将删除位置后继元素前移 */
			L->data[k-1]=L->data[k];
    }
    L->length--;
    return OK;
}

Advanced operation

Insert all data elements in the linear list Lb but not in La into La

Combined with the above operations, let’s think about it, the code is as follows:

void unionL(SqList *La,SqList Lb)
{
	int La_len,Lb_len,i;
	ElemType e;                        /*声明与La和Lb相同的数据元素e*/
	La_len=ListLength(*La);            /*求线性表的长度 */
	Lb_len=ListLength(Lb);
	for (i=1;i<=Lb_len;i++)
	{
		GetElem(Lb,i,&e);              /*取Lb中第i个数据元素赋给e*/
		if (!LocateElem(*La,e))        /*La中不存在和e相同数据元素*/
			ListInsert(La,++La_len,e); /*插入*/
	}
}

Welcome to like, collect, and comment in the comment area, and reprint to indicate the source.

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

Above link:

Algorithm & Data Structure - Algorithm Explanation Encyclopedia_Haohao's Blog-CSDN Blog This article mainly introduces the theory related to algorithms, including the definition and characteristics of algorithms; time and space complexity calculations, etc., a small amount of code in this article. https://blog.csdn.net/qq_52213943/article/details/125686761

Links below:

Algorithm & Data Structure - Linear Table and Its Chained Storage Structure . https://blog.csdn.net/qq_52213943/article/details/125824927

Guess you like

Origin blog.csdn.net/qq_52213943/article/details/125709931