#include <iostream> #include<windows.h>//exit header file using namespace std; #define LIST_INIT_SIZE 100//The initial allocation of linear table storage space class SqList{ private: int *elem;//Base address of storage space (can not necessarily be int int length;//Sequence table length int listsize;//Maximum length of sequence list public: SqList(); ~SqList(); void CreatList(int a[],int);//Build a sequence list from the elements in the a array void DispList(); int getLength(); bool searchLocal(int i,int &e);//Find the element value at the i-th position int searchElem(int e); bool ListInsert(int i,int e); bool ListDelete(int i,int&e);// return element value with e void ReverseList(SqList &L);//Reverse order list void emptyList();//empty list }; SqList::SqList(){ elem = new int[LIST_INIT_SIZE]; if(!elem) exit(-1);//In some compilers, if new space allocation fails, an error will be reported directly, no return value, this judgment is meaningless listsize = LIST_INIT_SIZE; length = 0; } SqList::~SqList(){ } //Build a sequence table from the elements in the a array (time complexity O(n)) void SqList::CreatList(int a[],int n){ length = n; for(int i=0; i<n; i++) elem[i] = a[i]; } //Output all elements in the sequence table L (time complexity O(n)) void SqList::DispList(){ for(int i=0; i<length; i++) cout << elem[i] << " "; cout << endl; } int SqList::getLength(){ return length; } //Find the element value at the i-th position (time complexity O(1)) Use e to store the element value and return the value to judge whether the search is reasonable bool SqList::searchLocal(int i,int &e){ //!引用 if(i<1 || i>length) return false; e = elem[i-1]; return true; } //Find the location of the element (time complexity O(n)) int SqList::searchElem(int e){ int i; for(i=0;i<length;i++) if(elem[i]==e){ return i+1; break;//Find the first position } return 0;//Indicates not found } //Insertion of sequence table (number of moving elements n-i+1) (time complexity O(n)) bool SqList::ListInsert(int i,int e){ int k; if(i<1 || i>length+1) return false;//Incorrect insertion position for(k=length-1;k>=i;k--) element [k + 1] = element [k]; elem[i-1]=e; length++; return true; } //Deletion of the sequence table (time complexity O(n)) bool SqList::ListDelete(int i,int&e){ if(i<1 || i>length) return false; int k; e = elem[i-1];//Save the deleted element for(k=i-1;k<length-1;k++) element [k] = element [k + 1]; length--; return true; } // flip the sequence table void SqList::ReverseList(SqList &L){ int temp; for(int i=0;i<length/2;i++){ int temp = elem[i]; elem[i] = elem[length-i-1]; elem[length-i-1] = temp; } } void SqList::emptyList(){ length=0; } int main(){ SqList list1; int arr[3] = {3,4,5}; list1.CreatList(arr,3);//Create table list1.DispList();//Output table cout << "list1 length is " << list1.getLength() << endl; // find the element at the second position you are; list1.searchLocal(2, e); cout <<"The 2 local is elem " << e << endl; // find the position of element 5 cout << "The elem 5 local is " << list1.searchElem(5) << endl; // insert element 6 at position 4 list1.ListInsert(4, 6); list1.DispList (); // delete data element at position 1 int a; list1.ListDelete(1,a); list1.DispList (); list1.ReverseList(list1); list1.DispList (); return 0; }
[record] Data structure----implementation of sequence table
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324891975&siteId=291194637
Recommended
Ranking