recently read the "data structure, algorithms and Applications" This book, the book on the exercises summed up, with their own methods to achieve these questions, may be in the efficiency, coding and other aspects of a lot of problems, it may be wrong to achieve, if you look at this book when there are better ways to achieve, Also please a lot of message exchange more correct, thank you
9. Linear table implemented by C + +--sequential table
linearlist.h//linearlist////Created by cc on 14-8-21.//Copyright (c) 2014 CC. All Rights reserved.//sequential table #ifndef __linearlist__linearlist__#define __linearlist__linearlist__#include <iostream > #include <stdlib.h>using namespace std;template <class t>class linearlist {private://sequential table length int m_le Ngth; The maximum length of the order table int m_maxsize; Element array t* m_element; public:linearlist (int maxlistsize); Virtual ~linearlist (); /** * @brief Order table is empty * * @returntrue: null false: Non-empty */BOOL IsEmpty () const; /** * @brief Gets the length of the order table * * @return The length of the order table */int length () const; /** * @brief Get the maximum capacity of the order table * * @return The maximum capacity of the sequential table */INT maxSize () const; /** * @brief Find the k element in the order table and return the K element to X * @param k k element * @param x Save the k element * * @returntrue: Found K Element false: K element not found */bool Find (int k, t& x) const; /** * @brief Find element x in the order table and return x position * * @param x ElementX * * @return The position of element x in the order table */int Search (const t& x) const; /** * @brief Delete the k element and return it to X * @param k k element * @param x Save k element * * @return Modified Order table */Linea rlist<t>& deleteelement (int k, t& x); /** * @brief Insert the X element after the k element * * @param k k element * @param x Element x * * @return Modified Order table */linearlist& Lt t>& insertelement (int k, const t& x); /** * @brief Output Information * * @param out output stream */void output (ostream& out) const; };template <class t>linearlist<t>::linearlist (int maxlistsize): m_length (0), m_element (NULL) {this->m _maxsize = maxlistsize; This->m_element = new T[this->m_maxsize];} Template <class t>linearlist<t>::~linearlist () {delete [] this->m_element; This->m_element = NULL;} /** * @brief Order table is empty * * @returntrue: null false: Non-empty */template <class t>bool linearlist<t>::isempty () const {RE Turn This->m_length = = 0;} /** * @brief Gets the length of the sequential table * * @return The length of the order table */template <class t>int linearlist<t>::length () const {return this-& Gt;m_length;} /** * @brief Get the maximum capacity of the order table * * @return The maximum capacity of the sequential table */template <class t>int linearlist<t>::maxsize () const {return thi S->m_maxsize;} /** * @brief Find the k element in the order table and return the K element to X * @param k k element * @param x Save the k element * * @returntrue: found the K element false: The nth element was not found */tem Plate <class t>bool linearlist<t>::find (int k, t& x) const {if (k > This->m_length-1 | | K < 0) {cout << "Locate the" << K << "elements in the order table"; return false; } x = M_element[k]; return true;} /** * @brief Find the element x in the order table and return the position of X (position is subscript + 1) * @param x Element x * * @return The position of element x in the order table */template <class t>int linearlist& Lt T>::search (const t& x) const {for (int i = 0; i < this->m_length; i++) {if (This->m_element[i] = = x) {return i; }} return 0;} /** * @brief Delete the k element and return it to XIn * * @param k k elements * @param x Save k elements * * @return Modified order table */template <class t>linearlist<t>& linearlist<t ::d eleteelement (int k, t& x) {if (Find (k, x)) {//found k element for (int i = k; I < This->m_len Gth-1; i++) {This->m_element[i] = this->m_element[i + 1]; } this->m_length--; } else {//throws exception cout << "not Found" << k << "elements"; } return *this;} /** * @brief Insert the X element after the k element * * @param k k element * @param x Element x * * @return Modified order table */template <class t>linearlist<t> & Linearlist<t>::insertelement (int k, const t& x) {if (k > m_maxsize-1 | | k < 0) { cout << "Array subscript out of bounds!" << Endl; Throws OutOfBoundsException} else if (this->m_length = = this->m_maxsize) {cout << "has reached the maximum capacity of the array, request Add after memory! "<< Endl; Throws Nomemexception} else {//found k element for (int i = this->m_length-1; i > k; i--) {This->m_element[i] = this->m_element[i-1]; } this->m_length++; THIS->M_ELEMENT[K] = x; } return *this;} Template<class t>void Linearlist<t>::output (ostream& out) const {for (int i = 0; i < This->m_len Gth i++) out << This->m_element[i] << "";} Overload template <class t>ostream& operator<< (ostream& out, const linearlist<t>& x) { X.output (out); return out;} #endif/* Defined (__linearlist__linearlist__) */
main.cpp//linnerlist////Created by Chengchao on 14-8-29.//Copyright (c) 2014 CC. All rights reserved.//#include <iostream> #include <stdlib.h> #include "LinearList.h" int main (int argc, const char * argv[]) {linearlist<int> list (10); List.insertelement (0, +). Insertelement (1, 312). Insertelement (2, 134); cout << "The list is" << Endl; cout << list << Endl; Whether the sequential table is empty cout << "sequential table is empty:" << boolalpha << list.isempty () << Endl; cout << "Maximum capacity of the sequential table:" << list.maxsize () << Endl; int res = 0; int res2 = 0; Find cout << "find the No. 0 element input into the res variable, whether found:" << boolalpha << list.find (0, Res) << ", res=" << Res << Endl; Find 312 cout << "Find the 5th element entered into the RES variable, whether found:" << boolalpha << list.find (5, Res2) << ", res=" << ; Res2 << Endl; Removes element element list.deleteelement (1, res) on the 1th position; cout << List << Endl; Find element 134 cout << "search element 134, Location:" << List.search (134) << Endl; return 0;}
Output results such as:
Note that the templates in C + + do not support compilation separation and need to put the Declaration and implementation of the template class into the. h file
this article by cc original summary, if need reprint please indicate source:http://blog.csdn.net/oktears/article/details/27966399
Output results such as:
this article by cc original summary, if need reprint please indicate source:http://blog.csdn.net/oktears/article/details/27966399
"Data structure, algorithm and application" 9. (c + + implementation Order table)