Order Table of Linear Tables
Sequence table definition
The sequential storage of linear tables is also known as sequential tables.It stores data elements in a linear table in turn using a set of memory cells with consecutive addresses.This makes two logically adjacent elements physically adjacent.The first element is stored at the beginning of the linear table, and the second element is immediately followed by the i+1 element.Therefore, a sequential table is characterized by the logical order of the elements in the table being the same as their physical order
Its sequential storage structure is as follows
Array table below | Sequence table | Memory Address |
---|---|---|
0 | a1 | LOC(A) |
1 | a2 | LOC(A)+sizeof(ElemType) |
2 | a3 | LOC(A)+2*sizeof(ElemType) |
3 | a4 | LOC(A)+3*sizeof(ElemType) |
... | ┊ | ┊ |
MaxSize-1 | ┊ | LOC(A)+(MaxSize-1)*sizeof(ElemType) |
Assuming that the starting location of linear table L storage is LOC(A),sizeof(ElemType) is the size of storage space occupied by each data element, ElemType represents the type of data, and if integer is stored, it is int.(Note that the bit order in the linear table starts at 1, and the following table of elements in the array is 0)
The order table type is described below
#define MaxSize 50 type struct { ElemType data[MaxSize]; int length = 0; }SqList;
Of course, one-bit arrays can be assigned statically or dynamically
#define MaxSize 50 type struct { ElemType *data; int length = 0; }SqList;
The C language initial dynamic assignment is as follows
L.data = (ElemType*)malooc(sizeof(ElemType)*MaxSize)
The basic operations on the sequence table are as follows, code above
Insert operation
bool ListInsert(SqList; &L, int i, ElemType e) { //This function implements inserting element e at the first position if(i < 1 || i > L.length + 1) //Determine if i is out of bounds return false; if(L.length >= MaxSize) //Storage is full and cannot be inserted return false; for(int j = L.length; j >= i; j--) // Move the f i rst and subsequent elements back to make room for element e L.data[j] = L.data[j-1]; L.data[i-1] = e; L.length++; return true; }
Delete operation
bool ListDelete(SqList; &L, int i, ElemType &e) { //This function deletes the ith element and returns the deleted element with the reference variable e if(i < 1 || i > L.length) //Determine if i is out of bounds return false e = L.data[i-1]; for(int j = i; j < L.length; j++) L.data[j-1] = L.data[j]; L.length--; return true; }
Find operation
int LocateElem(SqList; &L, ElemType e) { //This function implements the bit order of the search element e in the table, the search failure returns 0 for(int i = 0; i < L.length; i++) if(L.data[i] == e) return i+1; return e; }
Total Code
#include<stdio.h> #define MaxSize 100 typedef struct { int data[MaxSize]; int length = 0; }SqList; bool ListInsert(SqList &L, int i, int e) { //This function implements inserting element e at the first position if(i < 1 || i > L.length + 1) //Determine if i is out of bounds return false; if(L.length >= MaxSize) //Storage is full and cannot be inserted return false; for(int j = L.length; j >= i; j--) // Move the f i rst and subsequent elements back to make room for element e L.data[j] = L.data[j-1]; L.data[i-1] = e; L.length++; return true; }; bool ListDelete(SqList &L, int i, int &e) { //This function deletes the ith element and returns the deleted element with the reference variable e if(i < 1 || i > L.length) //Determine if i is out of bounds return false; e = L.data[i-1]; for(int j = i; j < L.length; j++) L.data[j-1] = L.data[j]; L.length--; return true; } int LocateElem(SqList &L, int e) { //This function implements the bit order of the search element e in the table, the search failure returns 0 for(int i = 0; i < L.length; i++) if(L.data[i] == e) return i+1; return 0; } int main() { int a; SqList L; ListInsert(L, 1, 10); ListInsert(L, 2, 20); ListInsert(L, 3, 15); ListInsert(L, 4, 18); printf("15 The bit order in the order table is: %d\n", LocateElem(L, 15)); ListDelete(L, 3, a); printf("The third element of the sequence table was deleted, Value is: %d\n", a); if(LocateElem(L, 15) == 0) printf("There is no value of 15 in the order table\n"); else printf("15 The bit order in the order table is: %d\n", LocateElem(L, 15)); return 0; } //Output: 15 The bit order in the order table is: 3 //The third element of the sequence table was deleted, with a value of: 15 //There is no value of 15 in the order table
The list will be explained later