template <class list entry> class List ipublic: //methods of the List ADT 遍历顺序表 st(); int size const bool fullo const 将x插入表中 令其成为第 bool emptylconst position个元素 void clear(; void traversevoid (*visit) (List entr Error_code retrieve(int positi entry &x); Error_code replace(int position, const List_entry &x); Error_code remove(in* position, List_entry &x); Error code insert int position, const List entry &x);
template <class List_entry> class List { public: // methods of the List ADT List( ); int size( ) const; bool full( ) const; bool empty( ) const; void clear( ); void traverse(void (*visit)(List_entry &)); Error_code retrieve(int position, List_entry &x); Error_code replace(int position, const List_entry &x); Error_code remove(int position, List_entry &x); Error_code insert(int position, const List_entry &x); 求顺序表的长度 判顺序表是否已"满" 判顺序表是否为“空" 清空顺序表 遍历顺序表 查找表中第position 个元素存入x 将表中第position 删除表中第 个元素值修改为x position 个元素,其值存入x 将x插入表中 令其成为第 position个元素
protected: //data members for a contiguous list implementation nt count: List entry entrymax list]: }; template <class list entry> int List<List entry>: sizeconst //Post: The function returns the number of entries in the list i return count;1
protected: // data members for a contiguous list implementation int count; List_entry entry[max_list]; }; template <class List_entry> int List<List_entry>::size( ) const // Post: The function returns the number of entries in the List . { return count; }
template <class List entry> Error code List<List_ entry> insert(int position, const List entry &x) Post: If the List is not full and s position s n; where n is the number of entries in the List, the function succeeds: any entry formerly at position and all later entries have their position numbers increased by 1 and x is inserted at position of the list Else: The function fails with a diagnostic Error code. * I if (full()return overflow; if (position <0 position count) return range error for (int i=count-1; i>= position; i--)entry[i +1]=entry[i]: entrylposition]=X; count++. return success:
template <class List_entry> Error_code List<List_entry> ::insert(int position, const List_entry &x) /* Post: If the List is not full and 0 ≤ position ≤ n; where n is the number of entries in the List , the function succeeds: Any entry formerly at position and all later entries have their position numbers increased by 1 and x is inserted at position of the List . Else: The function fails with a diagnostic Error_code. */ { if (full( )) return overflow; if (position < 0 || position > count) return range_error; for (int i=count-1; i >= position; i--) entry[i +1] = entry[i]; entry[position] = x; count++; return success; }
template <class List_entry? void List<List entry>: traverse void visit)(List entry &) / Post: The action specified by function *visit) has been performed on every entry of the List, beginning at position 0 and doing each in turn. * for ( int i=0; i<count; i++) visit)entry[i); Performance of methods In processing a contiguous list with n entries o insert and remove operate in time approximately proportional to n List, clear, empty, full, size, replace, and retrieve operate in constant time
template <class List_entry> void List<List_entry>::traverse(void (*visit)(List_entry &)) /* Post: The action specified by function(*visit) has been performed on every entry of the List , beginning at position 0 and doing each in turn. */ { for (int i=0; i<count; i++) (*visit)(entry[i]); } Performance of Methods In processing a contiguous list with n entries: insert and remove operate in time approximately proportional to n. List, clear, empty, full, size, replace, and retrieve operate in constant time
Please follow the Error_ code insert(int position, const List entry &x) write by yourselves: Error code retrieve(int position, List entry &x); Error- code replace (int position, const List entry &x); Error code remove ( int position, List entry &x) Application: Contiguous List 设顺序表la和b分别表示整数集合A和B,求 A三4J 集合中的元素是无重复的,可将Lb表中的 元素从表尾起逐个删除,并在La表查找被删元素b 若找不到,则将元素b插入La表,否则不必插入,完 成后集合B为空
Please follow the Error_code insert(int position, const List_entry &x) write by yourselves: Error_code retrieve(int position, List_entry &x); Error_code replace(int position, const List_entry &x); Error_code remove(int position, List_entry &x); Application: Contiguous List 设顺序表la和lb分别表示整数集合A和B,求: A=A∪B。 算法思路:集合中的元素是无重复的,可将Lb表中的 元素从表尾起逐个删除,并在La表查找被删元素b, 若找不到,则将元素b插入La表,否则不必插入,完 成后集合B为空