当前位置:网站首页>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).
边栏推荐
- Process finished with exit code-1073740791(0xC0000409)
- 17、 Solutions to duplicate names of ROS function packages and nodes
- linear transformation
- Crawler: from entry to imprisonment (II) -- Web collector
- Four basic data types
- How many ways can multithread run in sequence?
- 5、 C language judgment statement
- The 35 required questions in MySQL interview are illustrated, which is too easy to understand
- Redis persistence
- Using keras to visualize the network model, failed to import pydot appears
猜你喜欢

linux安装redis

【七夕】七夕孤寡小青蛙究极版?七夕节最终章!

@Solution to DS ('slave') multi data source compatible transaction problem

Qt development tips

Getting started with scottplot tutorial: getting and displaying values at the mouse
![[thread safety] what risks may multithreading bring?](/img/79/112ab7e586b0bceb296dfddb2728be.png)
[thread safety] what risks may multithreading bring?

6、 C language circular statement

Hcip day 10

Simple data analysis using Weka and excel

C language related programming exercises
随机推荐
用 Table 在 SwiftUI 下创建表格
Why can the anonymous functions of JQ access the methods inside
@Solution to DS ('slave') multi data source compatible transaction problem
企鹅一面:为什么不建议使用SELECT * ?
Install biological sequence de redundancy software CD hit
How does core data save data in SQLite
Some problems encountered in the development of Excel VBA, solutions, and continuous updates
Animation mechanism of swiftui
使用Weka与Excel进行简单的数据分析
Bulk Rename Utility
18、 ROS topic name setting
ssh服务
Read the introduction tutorial of rainbow
Chapter 3 stack, queue and array
7、 Detailed explanation of C language function definition
10、 Timestamp
如何只降3D相机不降UI相机的分辨率
17、 Solutions to duplicate names of ROS function packages and nodes
Several methods of opening URL in swiftui view
VTK notes - picker picker summary