顺序表
来源:互联网 发布:淘宝收信用卡1%手续费 编辑:程序博客网 时间:2024/04/30 21:26
顺序表
//// 顺序表// TestList//// Created by chenshang on 14-2-7.// Copyright (c) 2014年 chenshang. All rights reserved.//#ifndef TestList_SeqList_h#define TestList_SeqList_h#include <iostream>using namespace std;typedef int T;const int DefaultSize=100;class SeqList{public: SeqList(int sz=DefaultSize):maxSize(sz),currentSize(-1){ if (sz>0) { arr = new T[maxSize]; } } ~SeqList(){ delete[] arr; }public: int length() const{ return currentSize+1; } int find(T x)const; // 找到值为x的位置 bool isElement(T x) const; //is it in the list bool insert(T x,int i); bool remove(T x); bool isEmpty(){ return currentSize==-1; } bool isFull(){ return currentSize == maxSize-1; } int Get(int i){ return i<0||i>currentSize ? (cout<<"can not find the element"<<endl,0):arr[i]; } void print(); private: int *arr; int maxSize; int currentSize;};int SeqList::find(T x)const{ for (int i=0; i<currentSize; i++) { if (arr[i]==x) { return i; } } cout<<"can not find the element you want to find"<<endl; return -1;}bool SeqList::isElement(T x)const{ if (find(x)==-1) { return false; } return true;}bool SeqList::insert(T x,int i){ if (i<0||i>currentSize+1||currentSize==maxSize-1) { cout<<"the operate is illegal"<<endl; return false; } currentSize++; for (int j=currentSize; j>i; j--) { arr[currentSize]=arr[j-1]; } arr[i]=x; return true;}bool SeqList::remove(T x){ int size = currentSize; for (int i=0; i<currentSize; i++) { if (arr[i]==x) { for (int j=i; j<currentSize; j++) { arr[j]=arr[j+1]; } currentSize--; continue; } } if (size==currentSize) { cout<<"can not find the element you want to remove"<<endl; return false; } return true;}void SeqList::print(){ for (int i=0; i<currentSize; i++) { cout<<i+1<<":\t"<<arr[i]<<endl; } cout<<endl<<endl;}#endif
0 0
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- 顺序表
- RDO、SAD、SATD、λ(转贴) - [h.264]
- linux内核0.12之MINIX文件系统初探
- Codeforces 389A Fox and Number Game
- c++编程经验
- 英语听力网站,很难找的(赶紧收藏一下)
- 顺序表
- 2014-2-4TextView及其子类2
- 惠普Slate 7 Extreme平板,¥1206
- Intent.ACTION_VIEW,Intent.ACTION_SENDTO
- 凌乱的职业生涯,我将把你拿下!
- Codeforces Round #228(Div.2) A,B,C,E
- 世界这么大,我竟不知联系谁
- UML-用例图
- uva - 10905 - Children's Game(贪心,qsort排序)