I haven't updated it for a long time. I started to learn data structure.
The data elements with linear distribution in logical structure are also close to each other in the actual physical storage structure, which is called the sequential storage structure of linear tables.
That is to say, the logically linear data are stored in a whole continuous memory space in the order before and after, and there is no gap between them. Such a storage structure is called sequential storage structure.
Using sequential storage structure to store data, the address of the first element is the first address of the storage space. Through the first address, you can easily access all the stored data, as long as the first address is not lost, the data can always be found (a grasshopper on a rope, there must be).
The tables generated by the sequential storage structure of linear tables are called sequential tables.
Figure 1 Schematic structure of sequence table
Implementation of Sequence Table
The characteristics of the data stored in the sequence table and the data type of array are completely consistent, so the implementation of the sequence table uses arrays.
When implementing the storage structure of sequential tables, we must pay attention to pre-request enough memory space to avoid data overflow caused by insufficient storage space, resulting in unnecessary program errors or even crashes.
Storage structure of sequential table
In addition to pre-requesting memory space, we also need to record the length of the sequence table and the size of memory applied by the sequence table itself in real time, so as to facilitate the later retrieval of data elements in the sequence table.
The following is a code implementation directly attached to the sequential structure of linear tables:
#include<iostream> #Include < fstream >// Write and read data from files #Include < string >// Header file of string #Include < iomanip >// Control output accuracy using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 typedef int Status; //Define a synonym for int typedef int ElemType; //Synonyms of defined int #define MAXSIZE 100 // Maximum Length Possible for Sequential Lists struct Book { string id;//ISBN string name;//Title double price;//Price }; typedef struct { Book *elem; //Base address of storage space int length; //Current length } SqList; Status InitList_Sq(SqList &L) { //Initialization of algorithm 2.1 sequential table //Construct an empty sequential table L L.elem = new Book[MAXSIZE]; //Allocate an array space of MAXSIZE size for the sequence table if (!L.elem) exit(OVERFLOW); //Storage allocation failure exit() denotes exit L.length = 0; //Null table length is 0 return OK; } Status GetElem(SqList L, int i, Book &e) {//Algorithms 2.2 Value of Sequence Table if (i < 1 || i > L.length) return ERROR; //Determine whether the i value is reasonable, if not, return to ERROR e = L.elem[i - 1]; //elem[i-1] cell stores the first data element return OK; } int LocateElem_Sq(SqList L, double e) { //Algorithms 2.3 Search for Sequence Table //Search for Sequence Table for (int i = 0; i < L.length; i++) if (L.elem[i].price == e) return i + 1;//Find successfully, return the serial number i+1 return 0;//Find failed, return 0 } Status ListInsert_Sq(SqList &L, int i, Book e) { //Insertion of algorithm 2.4 sequential table //Insert a new element e before the position i in the order table L //The legal range of I value is 1<=i<=L.length+1 if ((i < 1) || (i > L.length + 1)) return ERROR; //Illegal value i if (L.length == MAXSIZE) return ERROR; //The current storage space is full for (int j = L.length - 1; j >= i - 1; j--) L.elem[j + 1] = L.elem[j]; //Position of insertion and subsequent element postshift L.elem[i - 1] = e; //Put new element e in position i ++L.length; //Table length increased by 1 return OK; } Status ListDelete_Sq(SqList &L, int i) { //Algorithms 2.5 Sequential Table Deletion //Delete the first element in the order table L and return its value with e //The legal range of I value is 1<=i<=L.length. if ((i < 1) || (i > L.length)) return ERROR; //Illegal value i for (int j = i; j <= L.length; j++) L.elem[j - 1] = L.elem[j]; //The element moves forward after the deleted element --L.length; //Table length reduced by 1 return OK; } int main() { SqList L; int i = 0, temp, a, c, choose; double price; Book e; string head_1, head_2, head_3; cout << "1. establish\n"; cout << "2. input\n"; cout << "3. Value\n"; cout << "4. lookup\n"; cout << "5. insert\n"; cout << "6. delete\n"; cout << "7. output\n"; cout << "0. Sign out\n\n"; choose = -1; while (choose != 0) { cout << "Please choose:"; cin >> choose; switch (choose) { case 1://Create a Sequence Table if (InitList_Sq(L)) cout << "Successful establishment of sequence table\n\n"; else cout << "Sequence table establishment failed\n\n"; break; case 2: {//Sequence table information input i = 0; L.elem = new Book[MAXSIZE]; if (!L.elem) exit(OVERFLOW); L.length = 0; fstream file; file.open("book.txt"); if (!file) { cout << "Wrong! No file was found!" << endl; exit(ERROR); } file >> head_1 >> head_2 >> head_3; while (!file.eof()) { file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price; i++; } cout << "input book.txt End of information\n\n"; L.length = i; file.close(); } break; case 3://Value of Sequence Table cout << "Please enter a location to get the value:\n"; cin >> i; temp = GetElem(L, i, e); if (temp != 0) { cout << "Search success\n"; cout << "The first" << i << "The information in this book is:\n"; cout << left << setw(15) << e.id << "\t" << left << setw(50) << e.name << "\t" << left << setw(5) << e.price << endl << endl; } else cout << "Find failed! Location out of range\n\n"; break; case 4: //Search for Sequence Table cout << "Please enter the price you want to find.:"; cin >> price; temp = LocateElem_Sq(L, price); if (temp != 0) { cout << "Search success\n"; cout << "The titles of the books corresponding to the price are:" << L.elem[temp - 1].name << endl << endl; } else cout << "Find failed! There are no books at this price.\n\n"; break; case 5: //Insertion of Sequence Table cout << "Please enter the insertion location and Book information, including: Number book title price (separated by spaces):"; cin >> a; cin >> e.id >> e.name >> e.price; //Input A and b, a represents the insertion position, B represents the insertion value (book information) if (ListInsert_Sq(L, a, e)) cout << "Insert success.\n\n"; else cout << "Insert failure.\n\n"; break; case 6: //Deletion of Sequence Table cout << "Please enter the location of the book you want to delete:"; cin >> c; if (ListDelete_Sq(L, c)) cout << "Delete successful.\n\n"; else cout << "Delete failed.\n\n"; break; case 7: //Output of Sequence Table cout << "Readout of Current Book System Information (Sequence Table):\n"; for (i = 0; i < L.length; i++) cout << left << setw(15) << L.elem[i].id << "\t" << left << setw(50) << L.elem[i].name << "\t" << left << setw(5) << L.elem[i].price << endl; cout << endl; break; } } return 0; }