首页 > 代码库 > 顺序表 有序表
顺序表 有序表
template <typename T>class SeqList{protected: T *data; int maxSize; int last; void reSize(int newSize) //ok { maxSize = newSize; T *temp = new T[maxSize]; for (int i = 0; i <= last; i++) temp[i] = data[i]; delete[] data; data = temp; }public: SeqList(int sz = defaultSize) : maxSize(sz), last(-1) //ok { data = new T[maxSize]; } SeqList(SeqList<T> &L) //ok { maxSize = L.maxSize; last = L.last; data = new T[maxSize]; for (int i = 0; i <= last; i++) data[i] = L.data[i]; } ~SeqList() { delete[] data; } //ok int Size() const { return maxSize; } //ok int Length() const { return last + 1; } //ok int Search(T &x, int l = 0, int r = -1) const //ok { if(r == -1) r = last; int p = (l + r) >> 1; if(l == r) return data[l] == x ? l + 1 : 0; else if(data[p] >= x) return Search(x, l, p); else if(data[p] < x) return Search(x, p + 1, r); } int Locate(int i) const // ok { if(i >= 1 && i <= last + 1) return i; return -1; } bool getDate(int i, T &x) const //ok { if (i > 0 && i <= last + 1) { x = data[i - 1]; return true; } else return false; } void setData(int i, T &x) //ok { if (i > 0 && i <= last + 1) data[i - 1] = x; } bool Insert(T &x) //ok { int p = ++last; while(p && data[p - 1] > x) data[p] = data[p-- - 1]; data[p] = x; return true; } bool Remove(T &x) //ok { int t = Search(x); if(t != -1) for(int i = t; i < last; i++) data[i] = data[i + 1]; last--; } bool IsEmpty() { return (last == -1) ? true : false; } //ok bool IsFull() { return (last + 1 == maxSize) ? true : false; } //ok void input() //ok { int n, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; Insert(x); } } void output() //ok { cout << "{"; for (int i = 0; i < last; i++) cout << data[i] << ", "; cout << data[last] << "}" << endl; } SeqList<T> operator = (SeqList<T> &L) //ok { if (L.last >= maxSize) reSize(L.maxSize); last = L.last; for (int i = 0; i <= last; i++) data[i] = L.data[i]; }};
自己瞎写的(毕竟没怎么听过课...)改进的话等下次老师讲评时注意一下
顺序表 有序表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。