Start learning the data structure. The road is still long. Come on !!
Use the C ++ template to implement the sequence table sequencelist. Upload the complete code for the first time and compare the dishes.
Sequencelist. h:
# Ifndef seq_list_h # define seq_list_h # include <iostream> # include <exception> // The template description and definition must be in the same header file. // create an ordered table class template <typename type> class sequencelist {public: sequencelist (INT length = 10 );~ Sequencelist (); // you can insert data void insert (const int index, const type * value) after inserting an element index element in a certain position ); // Delete void Delete (const int index, type * value); // query int getindex (const type * value); // modify void Update (const int index, type * value); // clear the array void output (STD: ostream & out) const; // return the length of int length (); bool isempty (); Private: type * slist; int maxsize, Len ;}; template <class type> sequencelist <type >:: sequencelist (I NT length = 10) {// constructor slist = new type [length]; maxsize = length; Len = 0;} template <class type> void sequencelist <type> :: insert (const int index, const type * value) {// Add if (index <0 | index> maxsize) {Throw STD: out_of_range ("out of bounds !! ");} If (LEN = maxsize) {Throw STD: out_of_range (" no memory !! ") ;}// Move back a position for (INT I = Len; I >= index; I --) {slist [I + 1] = slist [I];} slist [Index] = * value; Len ++;} template <class type> void sequencelist <type>: delete (const int index, type * value) {// store the k-th element in the sequence table into the value, and delete the k-th element if (index <0 | index> Len) Throw STD :: out_of_range ("out of bounds !! "); * Value = slist [Index]; for (INT I = index; I <= Len; I ++) {slist [I] = slist [I + 1];} Len --;} template <class type> int sequencelist <type >:: getindex (const type * value) {// the position where the query value is located is not the index value. If no index value is available, return 0for (INT I = 0; I <Len; I ++) {If (* value = slist [I]) Return (I + 1) ;}return 0 ;}template <class type> void sequencelist <type> :: update (const int index, type * value) {// modify if (index> maxsize | index <0) {Throw STD :: Out_of_range ("out of bounds !! ");} If (index> Len) return; slist [Index] = * value;} template <class type> void sequencelist <type >:: output (STD :: ostream & out) const {// For (INT I = 0; I <Len; I ++) {out <slist [I] <"";}} template <class type> sequencelist <type> ::~ Sequencelist () {// do not forget the Destructor: Delete [] Delete [] slist; slist = NULL;} template <class type> int sequencelist <type> :: length () {// return Len;} template <class type> bool sequencelist <type >:: isempty () {// determine whether it is null return (0 = Len);} # endif
Source file. cpp
// The template is actually compiled and generated during actual instantiation. // For ease of use, it almost always places all template statements and definitions in the header file # include <iostream> # include" sequencelist. H "using namespace STD; int * array = new int [20]; void Init (int * (& ARR), int Len) {for (INT I = 0; I <Len; I ++) {arr [I] = 2 * I ;}} int main () {sequencelist <int> slist (20 ); cout <"length =" <slist. length () <Endl; cout <"is empty? "<Slist. isempty () <Endl; Init (array, 6); slist. insert (0, & array [0]); slist. insert (1, & array [1]); slist. insert (2, & array [2]); slist. insert (3, & array [3]); slist. insert (4, & array [4]); slist. output (cout); int value = 4; cout <"index:" <slist. getindex (& Value) <Endl; value = 4; slist. delete (2, & value); slist. output (cout); cout <Endl; value = 100; slist. update (0, & value); slist. output (cout); cout <Endl; System ("pause ");}
Laugh! I wish you a smooth college entrance examination.