顺序表

来源:互联网 发布:淘宝收信用卡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
原创粉丝点击