PubMed 408 Data Structure Linear Table Review
#include<iostream> #include<algorithm> const int initlength = 100; ///The initial length of the sequence table using namespace std; typedef struct { int *data; int MaxSize, length; ///Maximum storage space of the sequence table, length; } SeqList; ///*****Basic operation of the sequence table void initList(SeqList &L, int n = initlength) ///Initialization sequence table { L.data = new int [n]; L.length = 0; L.MaxSize = n; for(int i=0;i<L.MaxSize;++i) { cin>>L.data[i]; L.length++; } } int Length(SeqList L) ///Returns the length of the sequence list { return L.length; } bool EmptyList(SeqList L) ///Determine whether the sequence table is empty { if(L.length == 0) return true; return false; } bool ListInsert(SeqList &L, int i, int value) ///Insert elements, i is the position of the element, value is the value of the element { if(i<1 || i>L.length+1) return false; if(i >= L.MaxSize) return false; for(int j=L.length; j>=i; --j) L.data[j] = L.data[j-1]; L.data[i-1] = value; L.length++; return true; } bool ListDelete(SeqList &L, int i, int &e) ///Delete elements, i is the position of the deleted element, e is the value of the element at the deleted position { if(i<1 || i>L.length) return false; e = L.data[i-1]; for(int j=i-1; j<L.length-1; ++j) L.data[j] = L.data[j+1]; L.length--; return true; } int LocateElem(SeqList L, int e) ///Find the bit sequence of the first element value e by value { for(int i=0; i<L.length; ++i) { if(e == L.data[i]) return i+1; } return 0; } void PrintList(SeqList L) ///Print out the entire sequence list { for(int i=0;i<L.length;++i) cout<<L.data[i]<<" "; cout<<endl; } ///*** Sequence table advanced operation, with title description ///1. Delete the element with the smallest value in the sequence table (assuming unique), and return the value of the deleted element by the function /// The vacant position is filled by the last element. If the sequence table is empty, display an error message and exit bool DeleteMinList(SeqList &L, int &e) { if(EmptyList(L)) //if (L.length == 0) { cout<<"The List is empty, can't delete the min value!"<<endl; return false; } int ListMin = L.data[0]; int LocalMin = 0; for(int i=0;i<L.length;++i) { if(ListMin > L.data[i]) { ListMin = L.data[i]; LocalMin = i; } } e = ListMin; L.data[LocalMin] = L.data[L.length-1]; L.length--; return true; } /// Design an efficient algorithm that reverses all elements of the sequence table, requiring the space complexity of the algorithm to be O(1) void ReverseList(SeqList &L) { int temp; for(int i=0;i<L.length/2;++i) { temp = L.data[i]; L.data[i] = L.data[L.length-i-1]; L.data[L.length-i-1] = temp; } } /// For a sequential table L of length n, write a time complexity of O(n) and space complexity of O(1), this algorithm deletes all data elements whose value is x in the linear table. /// Can reconstruct the sequence table void DeleteEqualElement(SeqList &L, int x) { int k = 0; for(int i=0; i<L.length; ++i) { if(L.data[i] != x) { L.data[k] = L.data[i]; k++; } } L.length = k; } /// or the second algorithm /* void DeleteEqualElement(SeqList &L, int x) { int k=0, i=0; while(i<L.length) { if(L.data[i] == x) k++; else L.data[i-k] = L.data[i]; i++; } L.length -= k; } */ intmain() { ios::sync_with_stdio(false); SeqList L; int n; cin>>n; initList(L, n); ListInsert(L, 5, 8); PrintList(L); cout<<"The List Length is "<<Length(L)<<endl; you are; ListDelete (L, 5, e); PrintList(L); cout<<"e's value is "<<e<<endl; cout<<"The List Length is "<<Length(L)<<endl; int order = LocateElem(L, 6); if(order != 0) cout<<"The element's order is "<<order<<endl; else cout<<"sorry, we can't find the value's order!!!"<<endl; DeleteMinList (L, e); PrintList(L); cout<<"e's value is "<<e<<endl; ReverseList(L); PrintList(L); return 0; }