当前位置:网站首页>Basic operation implementation of sequence table
Basic operation implementation of sequence table
2022-07-28 14:58:00 【Swlaaa】
Catalog
1. Definition of sequence table
2. Implementation of basic operation of sequence table
(3) According to the value lookup ( In order to find )
1. Definition of sequence table
The sequential storage of linear table is also called sequential table . It is a group of storage units with continuous addresses that successively store the data elements in the linear table , Thus, two logically adjacent elements are also adjacent in physical position .
Assume that the element type of the linear table is ElemType , Then the sequential storage type of the linear table is described as :
#define MaxSize 50 // Defines the maximum length of a linear table
typedef struct{
ElemType data[MaxSize]; // Elements of a sequence table
int length; // The current length of the sequence table
}SqList; // Type definition of sequence table One dimensional arrays can be statically allocated , It can also be dynamically allocated . In static allocation , Because the size and space of the array have been fixed in advance , Once the space is full , Adding new data will create an overflow , And then cause the program to crash .
And in dynamic allocation , The space for storing arrays is allocated by dynamically storing statements during the execution of the program , Once the data space is full , Just open up a bigger storage space , To replace the original storage space , So as to achieve the purpose of expanding the storage array space , There is no need to divide all spaces for the linear table at one time .
#define MaxSize 100 // The initial definition of table length
typedef struct{
ElemType *data; // Pointer to dynamically allocate array
int MaxSize,length; // The maximum capacity and current number of arrays
}SeqList; // Type definition of dynamic allocation array order table C The initial dynamic allocation statement of is :
L.data = (ElemType*)malloc(sizeof(ElemType)*InitSize);C++ The initial dynamic allocation statement of is :
L.data = new ElemType[InitSize];2. Implementation of basic operation of sequence table
The basic operation of a linear table refers to its core 、 The most basic operation . Other complex operations can be realized by calling their basic operations . The main operations of the linear table are as follows :
(1) initialization
For the sequence table L Dynamically allocate an array space of predefined size , send data Point to the base address of this space . Set the current length of the table to 0.
bool InitList(SqList &L){
// This algorithm is used to initialize the sequence table L
L.data = new ElemType[MaxSize]; // Assign a size of... To the sequential table MaxSize The array space of
if(!L.data)
exit(OVERFLOW); // Storage allocation failed exit
l.length = 0; // The length of the table is 0
return true;
}(2) Value
Take order table L Of the 1 <= i <= L.length Position element values , And use e return . if i Invalid input for , Then return to false , Indicates that the value failed .
bool GetElem(SqList L, int i, ElemType e){
// This algorithm is used to obtain the sequence table L pass the civil examinations i The value of the elements
if(i <= 1||i >= L.length) // Judge i Whether the value is reasonable
return false;
e = L.data[i-1];
return true;
}(3) The insert
In the sequence table L Of the 1 <= i <= L.length+1 Place to insert new elements e. if i Invalid input for , Then return to false , Indicates that the insertion failed ; otherwise , Put the first... Of the sequence table i Elements and all elements after them move one position to the right , Make an empty space to insert new elements e, Length of sequence table plus 1, Insert the success , return true .
bool ListInsert(SqList &L, int i, ElemType e){
// This algorithm realizes the element e Insert into the sequence table L pass the civil examinations i A place
if(i < 1||i > L.length+1) // Judge i Whether the scope of the
return false;
if(L.length >= MaxSize) // Determine whether the maximum range of the array is exceeded
return false;
for(int j = L.length; j >= i; j--) // take i Elements and subsequent elements are moved back
L.data[j] = L.data[j-1];
L.data[i-1] = e; // stay i Insert... At position e
L.length++; // Linear meter length plus 1
return true;
}The best situation : Insert in epitope , The time complexity is O(1).
The worst : Insert... In the header , The time complexity is O(n).
On average : Suppose it's on the i The probability of inserting a node at a location , When the length is n When inserting a node into a linear table of , The average number of mobile nodes required is O((1+n)/2)
therefore , The time complexity of linear table insertion algorithm is O(n).
(4) Delete operation
The order sheet L Of the 1 <= i <= L.length Location elements , Return if successful true, And use the deleted element as a reference variable e return , Otherwise return to false .
bool ListDlelete(SqList &L, int i, ElemType &e){
// This algorithm can delete the order table L pass the civil examinations i Elements of position
if(i < 1||i > L.length) // Judge i Whether the scope of the
return false;
e = L.data[i-1]; // Assign the deleted element to e
for(int j = i; j < L.length; j++) // Will be the first i The element after one position moves forward
L.data[j-1] = L.data[j];
L.length--; // The length of the linear table minus 1
return true;
}The best situation : Delete the footer element , The time complexity is O(1).
The worst : Delete header element , The time complexity is O(n).
On average : Suppose you delete the i The probability of a node at a location , When the length is n When a node is deleted from the linear table of , The time complexity required is O(n).
(5) According to the value lookup ( In order to find )
In the sequence table L Find the value of the first element equal to e The elements of , And return to its order .
int LocateElem(SqList L, ElemType e){
// This algorithm realizes that the median value of the lookup order table is e The elements of , If the search is successful , Returns the element order , Otherwise return to 0
int i;
for(i = 0; i<L.length; i++)
if(e == L.data[i])
return i+1; // Subscript to be i The element value of is equal to e, Returns its bit order i+1
return 0;
}The best situation : The element to find is in the header , The time complexity is O(1).
The worst : The element found is at the end of the table , The time complexity is O(n).
On average : Suppose that the element to be searched is in 1 <= i <= L.length Probability at one location , When the length is n The lookup value in the linear table of is e The average number of element comparisons required (n+1)/2
therefore , The time complexity of the linear table lookup algorithm is O(n).
边栏推荐
- VTK notes - picker picker summary
- 8、 Picker usage drop-down box selection effect
- 5、 C language judgment statement
- 18、 ROS topic name setting
- Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
- Process finished with exit code-1073740791(0xC0000409)
- Bulk Rename Utility
- Redis-配置文件讲解
- I am using a blog creation tool
- 58子站安居,经纪人营销管理平台登录接口加密逆向
猜你喜欢

35道MySQL面试必问题图解,这样也太好理解了吧

How to use the C language library function getchar ()

国产数据库的红利还能“吃”多久?

多商户商城系统功能拆解17讲-平台端订单列表

MITK create module

Deploy flask on Alibaba cloud server

Establishment and traversal of binary tree (implemented in C language)

The 35 required questions in MySQL interview are illustrated, which is too easy to understand

MITK creates plug-ins and generates plug-ins

linux安装mysql
随机推荐
7、 Detailed explanation of C language function definition
Why can the anonymous functions of JQ access the methods inside
Establishment and traversal of binary tree (implemented in C language)
String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]
21、 TF coordinate transformation (I): coordinate MSG message
Installing redis in Linux
10、 C enum enumeration
C callback function, interface function pointer as function parameter, function pointer as structure member
58子站安居,经纪人营销管理平台登录接口加密逆向
Examples of Pareto optimality and Nash equilibrium
Namespace conflict problem
[Tanabata] Tanabata lonely little frog research edition? The final chapter of Tanabata Festival!
(function(global,factory){
SwiftUI 的动画机制
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
Redis configuration file explanation
&0xffffffff(0x08)
Tensorflow GPU installation process record
The second pre class exercise
Store and guarantee rancher data based on Minio objects