<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                遞歸函數效率低, 常使用相應的迭代算法. mid, left, right均為元素下標, 如果當前表不為空, 則令x與l[mid]比較. 若兩者相等, 則搜索成功. 若前者小于后者, 則繼續查找左半部分, 否則查找右半部分. 下標范圍分別為[left, mid - 1], [mid + 1, right]. 如果當前搜索表為空表, 搜索失敗返回NotPresent. 實現代碼: ~~~ #include "iostream" #include "cstdio" #include "cstring" #include "algorithm" #include "assert.h" using namespace std; const int MAXN = 9999; enum ResultCode { Underflow, Overflow, Success, Duplicate, NotPresent }; template <class T> class DynamicSet { public: virtual ResultCode Search(T &x) const = 0; // 表中搜索與x關鍵字相同的元素, 若存在則賦值給x并且返回Success, 否則返回NotPresent virtual ResultCode Insert(T &x) = 0; // 表中搜索與x關鍵字相同的元素, 若存在該元素, 賦值給x返回Duplicate, 若表已滿返回Overflow, 若表未滿返回Success virtual ResultCode Remove(T &x) = 0; // 表中搜索與x關鍵字相同的元素, 若存在該元素, 賦值給x返回Success, 否則返回NotPresent virtual bool IsEmpty() const = 0; // 集合為空返回true virtual bool IsFull() const = 0; // 集合為滿返回true /* data */ }; template <class T> class ListSet: public DynamicSet<T> { public: ListSet( int mSize ); ~ListSet() { delete []l; } bool IsEmpty() const { return n == 0; } bool IsFull() const { return n == maxSize; } ResultCode Search(T &x) const; ResultCode Insert(T &x); ResultCode Remove(T &x); void Print(); private: T *l; int maxSize, n; /* data */ }; template <class T> void ListSet<T>::Print() { for(int i = 0; i < n; ++i) cout << l[i] << "\t"; cout << endl; } template <class T> ListSet<T>::ListSet(int mSize) { maxSize = mSize; l = new T[maxSize]; n = 0; } template <class T> ResultCode ListSet<T>::Insert(T &x) { assert(!IsFull()); l[n++] = x; l[n] = MAXN; return Success; } template <class T> ResultCode ListSet<T>::Remove(T &x) { } template <class T> ResultCode ListSet<T>::Search(T &x) const { int mid, left = 0, right = n -1; while(left <= right) { mid = (left + right) / 2; if(x < l[mid]) right = mid - 1; else if(x > l[mid]) left = mid + 1; else { x = l[mid]; return Success; } } return NotPresent; } int main(int argc, char const *argv[]) { ListSet<int> ls(20); int x = 21; ls.Insert(x); x = 30; ls.Insert(x); x = 36; ls.Insert(x); x = 41; ls.Insert(x); x = 52; ls.Insert(x); x = 54; ls.Insert(x); x = 66; ls.Insert(x); x = 72; ls.Insert(x); x = 83; ls.Insert(x); x = 97; ls.Insert(x); ls.Print(); x = 35; if(ls.Search(x) == Success) cout << "Found " << x << endl; else cout << "Not Found " << x << endl; return 0; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看