Basic operations of the sequence table (super detailed)

Table of contents

foreword

1. Definition of Sequence Table

2. C language description of sequence table

3. Implementation of basic operations in the sequence table 

3.1 Structure initialization operation

3.1.1 Construct an empty linear table L

3.1.2 Construct a linear table L with n data elements

Time complexity: O(n)

3.2 Destruction structure operation

3.2.1 Destroy a sequence table  

3.3 Processing operations

3.3.1 Changing the value of a data element

3.3.2 Inserting data elements

3.3.3 Deleting Data Elements

3.3.4 Blank the linear table 

3.4 Reference operations

3.4.1 Empty judgment of linear table

3.4.2 Find the length of the linear table

3.4.3 Finding the predecessor

3.4.4 Find successor

3.4.5 Find an element in the linear table

3.4.6 Positioning function

3.4.7 Traversing a linear list

Fourth, the sequence table code implementation

5. Results

6. Summary 


foreword

This article refers to Mr. Wang Zhuo's data structure video and Mr. Yan Weimin's "Data Structure"

1. Definition of Sequence Table

A finite sequence consisting of n (n≥0) data elements of the same type is called a linear table. The number n of data elements in the linear table is called the length of the linear table. When n=0, it is called an empty list.

The sequential storage representation of the linear table refers to storing the data elements in the linear table sequentially with a group of storage units with continuous addresses, that is, the sequential table.

2. C language description of sequence table

The code is as follows (example):

//存储结构 #define  MAXSIZE 100     
//最大长度 typedef  struct 
{
    int *elem;
    int length;
} SqList;  // 顺序表

3. Implementation of basic operations in the sequence table 

3.1 Structure initialization operation

3.1.1 Construct an empty linear table L

Time complexity: O(1) 

void InitList(SqList &L)
{
	L.elem=new int[MAXSIZE];
	if (!L.elem) exit(0);//未分配成功 
	L.length=0;
	cout<<"初始化成功"<<endl;
}

3.1.2 Construct a linear table L with n data elements

时间复杂度:O(n)

空间复杂度:S(n)=O(1)

void CreateList(SqList &L,int n)
{
	int e;
	if(n>MAXSIZE)
	{
		cout<<"超出最大长度"<<endl; 
	}
	else
	{
		for(int i=0;i<n;i++)
		{
			cout<<"第"<<i+1<<"次的元素数据"<<endl;
			cin>>e;
			L.elem[i]=e;
			L.length++;
		}
	} 
}

3.2 Destruction structure operation

3.2.1 Destroy a sequence table  

The space is freed only if it is not empty, and it does not need to be released if it is empty

Note that new is used to open up space, and delete is used to free up space

And use malloc to open up space, use free to release space

void DestoryList(SqList& L)
{
	if (L.elem) delete L.elem;//不空才释放空间,空的话就不用了
}

3.3 Processing operations

3.3.1 Changing the value of a data element

random access

Time complexity: O(1)

Space complexity: O(1)

void PutElem(SqList &L,int i,int &e )
{
	int t;
	cout<<"输入你想改成的值"<<endl;
	cin>>t;
	if(i<1||i>L.length)	cout<<"i的位置不对"<<endl;
	else
	{
		e=L.elem[i-1];
		L.elem[i-1]=t;
		cout<<"修改成功"<<endl;
	}
}

3.3.2 Inserting data elements

Insert a new element e before the i-th element of the sequence list L

Time complexity T(n)=O(n)

The space complexity of the sequence table S(n)=O(1)

Does not take up auxiliary space

// 在顺序表L的第 i 个元素之前插入新的元素e,
void ListInsert(SqList &L,int i,int e)
{ 
	if(i<1||i>L.length+1) cout<<"位置不合法\n"<<endl; //位置不合法
	else
	{
		if(L.length == MAXSIZE) cout<<"空间已满\n"<<endl;  //空间已满
		else
		{
			for(int j=L.length-1;j>=i-1;j--)
				L.elem[j+1]=L.elem[j]; //插入位及之后元素下移
			L.elem[i-1]=e;
			L.length++; 
		}
	}
} 

3.3.3 Deleting Data Elements

Time complexity T(n)=O(n)
Space complexity of sequence table S(n)=O(1)

// 被删除元素之后的元素左移
// ListDelete
//T(n)=O(n)
//顺序表的空间复杂度S(n)=O(1)
void ListDelete(SqList &L, int i,int &e) 
{
	if((i<1)||(i>L.length))	cout<<"删除位置不对"<<endl; 
	else
	{
		e=L.elem[i-1];
		for(int j=i;j<L.length;j++)
			L.elem[j-1]=L.elem[j];
		L.length--;
	}
} 

3.3.4 Blank the linear table 

It still occupies the original space, but the table length is set to 0, and the original data is cleared

//清空线性表
void ClearList(SqList& L)
{
	L.length = 0;//线性表长度置为0,但是还占用空间
}

3.4 Reference operations

3.4.1 Empty judgment of linear table

Use L.length to judge whether it is empty, if the length is 0, it will be empty, return 1, if it is not empty, return 0

//判断线性表是否为空
int ListEmpty(SqList L)
{
	if (!L.length)	return 1;//L.length==0返回true
	else return 0;
}

3.4.2 Find the length of the linear table

Return L.length to get the length

//求线性表长度
int ListLength(SqList& L)
{
	return L.length;
}

3.4.3 Finding the predecessor

cur_e is the serial number, which is the number of elements, then the subscript of the element is cur_e-1, then its predecessor element is subscripted as cur_e-2

// (求前驱)
void PriorElem(SqList L,int cur_e,int &pre_e)
{
	if(cur_e<1||cur_e>L.length)	cout<<"cur_e的位置不对"<<endl;
	else
	{
		pre_e=L.elem[cur_e-2];
		cout<<"求前驱成功"<<endl;
	}
}

3.4.4 Find successor

cur_e is the serial number, which is the number of the element, then the subscript of the element is cur_e-1, then the subscript of its successor element is cur_e

//(求后继)
void NextElem(SqList L,int cur_e,int &next_e )
{
	if(cur_e<1||cur_e>L.length)	cout<<"cur_e的位置不对"<<endl;
	else
	{
		next_e=L.elem[cur_e];
		cout<<"求后驱成功"<<endl; 
	}
}

3.4.5 Find an element in the linear table

i represents the serial number, which means the number of elements, and the corresponding subscript is i-1

//取值(根据位置i获取相应位置数据元素的内容)
//i表示的是序号,是第几个元素,对应下标是i-1 
void GetElem(SqList L,int i,int &e)
{
	if(i<1||i>L.length)	cout<<"i的位置不对"<<endl;
	else
	{
		e=L.elem[i-1];//随机存取, 
	}
} 

3.4.6 Positioning function

The implementation of the search operation on the sequence table
Query the first data element that meets the judgment condition in the sequence table.
If it exists, return its bit sequence, otherwise return 0. 
Best case: T(n)=O(1) Most Bad case: T(n)=O(n)
Average case: Assume each element has an equal lookup probability.
ASL=(1+2+...n)/n=(n+1)/2
T(n)= O(n) 

//顺序表上的查找操作的实现
//在顺序表中查询第一个满足判定条件的数据元素,
//若存在,则返回它的位序,否则返回 0 
// 最好情况: T(n)=O(1)    最坏情况: T(n)=O(n)
//平均情况:假设每个元素的查找概率相等.
//ASL=(1+2+……n)/n=(n+1)/2
//T(n)=O(n) 
int LocateElem(SqList L,int e) 
{	
	for (int i=0;i<L.length;i++)
      if(L.elem[i]==e) return i+1;
	return 0;
} 

3.4.7 Traversing a linear list

Time complexity: O(n)

Space complexity: S(n)=O(1)

void ListTraverse(SqList L)
{
	for(int i=0;i<L.length;i++)
		cout<<i+1<<"位置的数据为"<<L.elem[i]<<endl;
	cout<<"遍历成功"<<endl;
}

Fourth, the sequence table code implementation

The code is as follows (example):

#include <iostream>
using namespace std;
#define  MAXSIZE 100     //最大长度
typedef  struct sqlist{
	int *elem;
	int length;
}SqList;  // 顺序表
//初始化顺序表
//时间复杂度:O(1) 
void InitList(SqList &L)
{
	L.elem=new int[MAXSIZE];
	if (!L.elem) exit(0);//未分配成功 
	L.length=0;
	cout<<"初始化成功"<<endl;
}
//构造一个含 n 个数据元素的线性表 L
void CreateList(SqList &L,int n)
{
	int e;
	if(n>MAXSIZE)
	{
		cout<<"超出最大长度"<<endl; 
	}
	else
	{
		for(int i=0;i<n;i++)
		{
			cout<<"第"<<i+1<<"次的元素数据"<<endl;
			cin>>e;
			L.elem[i]=e;
			L.length++;
		}
	} 
}    
//销毁一个线性表  
void DestoryList(SqList& L)
{
	if (L.elem) delete L.elem;//不空才释放空间,空的话就不用了
}

//清空线性表
void ClearList(SqList& L)
{
	L.length = 0;//线性表长度置为0,但是还占用空间
}
//求线性表长度
int ListLength(SqList& L)
{
	return L.length;
}
//判断线性表是否为空
int ListEmpty(SqList L)
{
	if (!L.length)	return 1;//L.length==0返回true
	else return 0;
}
//( 改变数据元素的值 )
void PutElem(SqList &L,int i,int &e )
{
	int t;
	cout<<"输入你想改成的值"<<endl;
	cin>>t;
	if(i<1||i>L.length)	cout<<"i的位置不对"<<endl;
	else
	{
		e=L.elem[i-1];
		L.elem[i-1]=t;
		cout<<"修改成功"<<endl;
	}
}
// (求前驱)
void PriorElem(SqList L,int cur_e,int &pre_e)
{
	if(cur_e<1||cur_e>L.length)	cout<<"cur_e的位置不对"<<endl;
	else
	{
		pre_e=L.elem[cur_e-2];
		cout<<"求前驱成功"<<endl;
	}
}
//(求后继)
void NextElem(SqList L,int cur_e,int &next_e )
{
	if(cur_e<1||cur_e>L.length)	cout<<"cur_e的位置不对"<<endl;
	else
	{
		next_e=L.elem[cur_e];
		cout<<"求后驱成功"<<endl; 
	}
}
//( 遍历线性表)
void ListTraverse(SqList L)
{
	for(int i=0;i<L.length;i++)
		cout<<i+1<<"位置的数据为"<<L.elem[i]<<endl;
	cout<<"遍历成功"<<endl;
}
//取值(根据位置i获取相应位置数据元素的内容)
//i表示的是序号,是第几个元素,对应下标是i-1 
void GetElem(SqList L,int i,int &e)
{
	if(i<1||i>L.length)	cout<<"i的位置不对"<<endl;
	else
	{
		e=L.elem[i-1];//随机存取, 
	}
} 
//顺序表上的查找操作的实现
//在顺序表中查询第一个满足判定条件的数据元素,
//若存在,则返回它的位序,否则返回 0 
// 最好情况: T(n)=O(1)    最坏情况: T(n)=O(n)
//平均情况:假设每个元素的查找概率相等.
//ASL=(1+2+……n)/n=(n+1)/2
//T(n)=O(n) 
int LocateElem(SqList L,int e) 
{	
	for (int i=0;i<L.length;i++)
      if(L.elem[i]==e) return i+1;
	return 0;
} 
// ListInsert
// 在顺序表L的第 i 个元素之前插入新的元素e,
void ListInsert(SqList &L,int i,int e)
{ 
	if(i<1||i>L.length+1) cout<<"位置不合法\n"<<endl; //位置不合法
	else
	{
		if(L.length == MAXSIZE) cout<<"空间已满\n"<<endl;  //空间已满
		else
		{
			for(int j=L.length-1;j>=i-1;j--)
				L.elem[j+1]=L.elem[j]; //插入位及之后元素下移
			L.elem[i-1]=e;
			L.length++; 
		}
	}
} 
// 被删除元素之后的元素左移
// ListDelete
//T(n)=O(n)
//顺序表的空间复杂度S(n)=O(1)
void ListDelete(SqList &L, int i,int &e) 
{
	if((i<1)||(i>L.length))	cout<<"删除位置不对"<<endl; 
	else
	{
		e=L.elem[i-1];
		for(int j=i;j<L.length;j++)
			L.elem[j-1]=L.elem[j];
		L.length--;
	}
} 
void menu()
{
	cout<<"**********************************************************"<<endl;
	cout<<"1.构造一个空的线性表 L"<<endl;
	cout<<"2.构造一个含 n 个数据元素的线性表 L"<<endl;
	cout<<"3.销毁线性表 L"<<endl;
	cout<<"4.改变数据元素的值"<<endl;
	cout<<"5.插入数据元素"<<endl;
	cout<<"6. 删除数据元素"<<endl;
	cout<<"7.线性表置空"<<endl;
	cout<<"8.线性表判空"<<endl;
	cout<<"9.求线性表的长度"<<endl;
	cout<<"10.求前驱"<<endl;
	cout<<"11.求后继"<<endl;
	cout<<"12.求线性表中某个元素"<<endl;
	cout<<"13.定位函数"<<endl;
	cout<<"14.遍历线性表"<<endl;
	cout<<"15.退出"<<endl;
	cout<<"**********************************************************"<<endl;
}
int main()
{
	int choice;
	SqList L;
	while(1)
	{
		menu();
		cout<<"请输入你的选择"<<endl;
		cin>>choice;
		switch(choice)
		{
			case 1:
				InitList(L);
				break;
			case 2:
				int n;
				cout<<"输入你需要多少个节点"<<endl;
				cin>>n;
				CreateList(L,n);
				break;
			case 3:
				DestoryList(L);
				break;
			case 4:
				int i1,e1;
				cout<<"输入要改变的元素序号"<<endl;
				cin>>i1;
				PutElem(L,i1,e1);
				cout<<"原来数据的值为"<<e1<<endl;
				break;
			case 5:
				int i2,e2;
				cout<<"输入要插入的元素位置跟数据"<<endl;
				cin>>i2>>e2;
				ListInsert(L,i2,e2);
				break;
			case 6:
				int i3,e3;
				cout<<"输入要删除的元素位置"<<endl;
				cin>>i3;
				ListDelete(L,i3,e3);
				cout<<"原来删除的元素数据:"<<e3<<endl;
				break;
			case 7:
				ClearList(L);
				break;
			case 8:
				if(ListEmpty(L))	cout<<"顺序表为空"<<endl;
				else cout<<"顺序表不为空"<<endl;
				break;
			case 9:
				cout<<"顺序表的长度为:"<<ListLength(L)<<endl;
				break;
			case 10:
				int i4,e4;
				cout<<"输入元素序号"<<endl;
				cin>>i4;
				PriorElem(L,i4,e4);
				cout<<"该元素的前驱节点的数据:"<<e4<<endl;
				break;
			case 11:
				int i5,e5;
				cout<<"输入元素序号"<<endl;
				cin>>i5;
				NextElem(L,i5,e5);
				cout<<"该元素的后继节点的数据:"<<e5<<endl;
				break;
			case 12:
				int i6,e6;
				cout<<"输入元素序号"<<endl;
				cin>>i6;
				GetElem(L,i6,e6); 
				cout<<"该元素的数据:"<<e6<<endl;
				break;
			case 13:
				int e7;
				cout<<"输入定位的元素数据"<<endl;
				cin>>e7;
				cout<<"定位到的位置为:"<<LocateElem(L,e7)<<endl;
				break;
			case 14:
				ListTraverse(L);
				break;
			case 15:
				exit(0);
			default:
				cout<<"输入错误,请重新输入"<<endl;
				break;	
		}
	}
	return 0;
} 

5. Results

Figure 1

Figure II

Figure three

Figure four

Figure five

Figure six

 Figure seven

Figure eight

 

Figure 9

Figure ten 

 Figure Eleven

 ​​​

 Figure 12

Figure 13

 

Figure Fourteen

 

Figure 15

 ​​​

 Figure sixteen

6. Summary 

        After watching Mr. Wang Zhuo's video, I realized all the basic operations of the sequence table, which gave me a better understanding of the sequence table. The sequence table has a good side, such as high storage density, and any element in the table can be randomly accessed, but it also has a bad side, such as when inserting or deleting an element, a large number of elements need to be moved to waste storage space, which belongs to the static storage form , the number of data elements cannot be freely expanded. In response to this shortcoming, I will introduce the linked list I learned to you in the next article.

Guess you like

Origin blog.csdn.net/m0_53679998/article/details/125080983