C++基于模板顺序表的实现(带排序)

说明:代码是可以运行的,但是发表在博客上后复制到编译器里面报N多错误,找了半天原因是网页里面生成了一些空白字符,这些字符编译器无法识别。

因此使用了2种插入格式插入代码。

第一个带注释解释的代码不可复制,最下面的第二个代码可以正常复制

代码如下:

#include <iostream>

#include<cstdlib>       //rand()函数产生随机值

#include<string>        //bool函数赋值

#include<iomanip>       //输出格式控制头文件



using namespace std;



const int defaultsize = 100;



template <typename T>

class seqlist

{

public:

    seqlist(int size = defaultsize);        //默认形参的构造函数

    seqlist(seqlist<T> &);

    ~seqlist();



//function

    int lengthlist(void);           //顺序表当前的元素个数

    int listsize(void);             //顺序表最大元素容纳个数

    int searchlist(T i);            //搜索顺序表

    bool getdata(int i, T & t);     //取顺序表第i个元素后赋值到引用t中

    bool setdata(int i, T t);       //设置第i个元素值为t

    bool insertdate(T t);           //插入到顺序表尾

    bool removedate(int i, T & t);  //将第i个元素保存到t后移出顺序表,

    bool isempty(void);             //判断表空

    bool isfull(void);              //判断表满

    void sortlist(void);            //表排序



private:

    T * lists;      //size of sequence list.                  //表存储的内存

    int maxsize;    //the maximum number of sequence list.    //表的最大容纳元素个数

    int last;       //indicating sequence list last element.   //当前表元素个数指示

};



template <typename T>

seqlist<T>::seqlist(int size)

{

    if (size > 0)

    {

        maxsize = size;

        last = -1;

        lists = new T[size];

        if (lists == NULL)

        {

            cout << "fatal error:can NOT allocate memory!" << endl;

            exit(-1);

        }

    }

    else

        cout << "incorrect size!" << endl;

}



template <typename T>

seqlist<T>::seqlist(seqlist<T> &p)

{

    maxsize = p.listsize();

    last = p.lengthlist() - 1;

    lists = new T[maxsize];

    if (lists == NULL)

    {

        cout << "memory error." << endl;

        exit(-1);

    }

    for (int i = 0; i <= last; i++)

    {

        lists[i] = p.lists[i];

    }

}



template <typename T>

seqlist<T>::~seqlist()

{

    cout << "free memory." << endl;

    delete [] lists;

}



template <typename T>

int seqlist<T>::lengthlist(void)

{

    return last + 1;

}



template <typename T>

int seqlist<T>::listsize(void)

{

    return maxsize;

}



template <typename T>

int seqlist<T>::searchlist(T t)

{

    if (!isempty())

    {

        cout << "search data=" << t << endl;

        for (int i = 0; i <= last; i++)

            if (t - lists[i] < 0.00000001)         //使用差值判断2个元素是否相等,防止精度导致的浮点类型不相等,例如1.23456与1.23457默认精度的情况

                return i + 1;                      //成功则返回位置

    }

    return -1;                                     //失败返回-1

}



template <typename T>

bool seqlist<T>::getdata(int i, T & t)

{

    if (i > 0 && i <= last + 1)

    {

        t = lists[i - 1];

        return true;

    }

    else

        return false;

}



template <typename T>

bool seqlist<T>::setdata(int i, T t)

{

    if (i >= 1 && i <= last + 1)

    {

        lists[i - 1] = t;

        return true;

    }

    else

        return false;

}



template <typename T>

bool seqlist<T>::insertdate(T t)

{

    if (isfull())

    {

        cout << "The lists is full!" << endl;

        return false;

    }

    else

    {

        lists[last + 1] = t;                        //由于顺序表的元素排列未排序,处于无须状态,指定插入顺序无意义,因此默认插在顺序表尾

        last++;

        return true;

    }

}



template <typename T>

bool seqlist<T>::removedate(int i, T & t)

{

    if (isempty())

    {

        cout << "the lists is empty!" << endl;

        return false;

    }

    else if (i > 0 && i <= last + 1)

    {

        t = lists[i - 1];                            //先保存即将要移出表元素的值

        for (int j = i; j <= last + 1; j++)          //移出后,将后面的表元素填充到前面的位置

            lists[j - 1] = lists[j];

        last--;

        return true;

    }

    else

    {

        cout << "incorrect number!" << endl;

        return false;

    }

}



template <typename T>

bool seqlist<T>::isempty(void)

{

//   cout << "last" << last << endl;

    return (last == -1) ? true : false;

}



template <typename T>

bool seqlist<T>::isfull(void)

{

    return (last == maxsize - 1) ? true : false;

}



template <typename T>

void seqlist<T>::sortlist(void)

{

    if (isempty())

        cout << "lists do not need to sort." << endl;

    else

        for (int i = 0; i < last; i++)                        //无须顺序表的排序,从第一个元素开始与后面所有的元素比较

        {

            int k = i;                                        //用k指示最小的元素

            for (int j = i + 1; j < last + 1; j++)            //用当前最小的与整个顺序表的所有元素比较

                if (lists[j] < lists [k])

                    k = j;

            if (k != i)

            {

                T temp = lists[i];

                lists[i] = lists[k];

                lists[k] = temp;

            }

        }

}



int main(void)

{

    int x1 = 0, x2 = 0 , x3 = 42 ;

    double y1 = 97.2775, y2 = 0, y3 = 0 ;

    bool status;



    seqlist<int> seq1(30);

    for (int i = 0; i < 30; i++)

        seq1.insertdate(rand() % 998);

    cout << "排序前:" << endl;

    for (int i = 0; i < 30; i++)

    {

        seq1.getdata(i + 1, x2);

        cout.fill('0');

        cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << "  ";

        if ((i + 1) % 5 == 0)

            cout << endl;

    }

    seq1.sortlist();

    cout << endl;

    cout << "排序后:" << endl;

    for (int i = 0; i < 30; i++)

    {

        seq1.getdata(i + 1, x2);

        cout.fill('0');

        cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << "  ";

        if ((i + 1) % 5 == 0)

            cout << endl;

    }

    cout << endl;

    x1 = seq1.listsize();

    cout << "lists seq1 size:" << x1 << endl;

    status = seq1.isempty();

    if (!status)

        cout << "lists seq1 is not empty." << endl;



    x1 = seq1.lengthlist();

    cout << "lists seq1 length:" << x1 << endl;

    x1 = seq1.searchlist(x3);

    cout << "lists seq1 search:" << x1 << endl;



    cout << endl << endl;

    cout << "building seq2." << endl;

    seqlist<double> seq2(30);

    for (int i = 0; i < 30; i++)

        seq2.insertdate(rand() / (double)(RAND_MAX / 100));



    status = seq2.isfull();

    if (status)

        cout << "lists seq2 is full." << endl;

    cout << endl;



    for (int i = 0; i < 20; i++)

    {

        seq2.getdata(i + 1, y2);

        cout << "list[" << setw(2) << i + 1 << "]=" << setw(7) << y2 << "  ";

        if ((i + 1) % 5 == 0)

            cout << endl;

    }



    cout << endl;



    x1 = seq2.lengthlist();

    cout << "lists seq2 length:" << x1 << endl;



    x1 = seq2.searchlist(y1);

    cout << "lists seq2 search:" << x1 << endl;



    seq2.setdata(7, 99);

    seq2.getdata(7, y2);

    cout << "seq2 setdata=" << y2 << endl;



    seq2.removedate(7, y3);

    cout << "remove data=" << y3 << endl;

    cout << endl;

    for (int i = 0; i < 19; i++)

    {

        seq2.getdata(i + 1, y2);

        cout << "list[" << setw(2) << i + 1 << "]=" << setw(7) << y2 << "  ";

        if ((i + 1) % 5 == 0)

            cout << endl;

    }

    cout << endl;

    cout << endl;



    seqlist<int> seq3(seq1);

    for (int i = 0; i < 30; i++)

    {

        seq1.getdata(i + 1, x2);

        cout.fill('0');

        cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << "  ";

        if ((i + 1) % 5 == 0)

            cout << endl;

    }





    return 0;

}

 

可正常编译代码:

  1 #include <iostream>

  2 #include<cstdlib>

  3 #include<string>

  4 #include<iomanip>

  5 

  6 using namespace std;

  7 

  8 const int defaultsize = 100;

  9 

 10 template <typename T>

 11 class seqlist

 12 {

 13 public:

 14     seqlist(int size = defaultsize);

 15     seqlist(seqlist<T> &);

 16     ~seqlist();

 17 

 18 //function

 19     int lengthlist(void);

 20     int listsize(void);

 21     int searchlist(T i);

 22     bool getdata(int i, T & t);

 23     bool setdata(int i, T t);

 24     bool insertdate(T t);

 25     bool removedate(int i, T & t);

 26     bool isempty(void);

 27     bool isfull(void);

 28     void sortlist(void);

 29 

 30 private:

 31     T * lists;

 32     int maxsize;

 33     int last;

 34 };

 35 

 36 template <typename T>

 37 seqlist<T>::seqlist(int size)

 38 {

 39     if (size > 0)

 40     {

 41         maxsize = size;

 42         last = -1;

 43         lists = new T[size];

 44         if (lists == NULL)

 45         {

 46             cout << "fatal error:can NOT allocate memory!" << endl;

 47             exit(-1);

 48         }

 49     }

 50     else

 51         cout << "incorrect size!" << endl;

 52 }

 53 

 54 template <typename T>

 55 seqlist<T>::seqlist(seqlist<T> &p)

 56 {

 57     maxsize = p.listsize();

 58     last = p.lengthlist() - 1;

 59     lists = new T[maxsize];

 60     if (lists == NULL)

 61     {

 62         cout << "memory error." << endl;

 63         exit(-1);

 64     }

 65     for (int i = 0; i <= last; i++)

 66     {

 67         lists[i] = p.lists[i];

 68     }

 69 }

 70 

 71 template <typename T>

 72 seqlist<T>::~seqlist()

 73 {

 74     cout << "free memory." << endl;

 75     delete [] lists;

 76 }

 77 

 78 template <typename T>

 79 int seqlist<T>::lengthlist(void)

 80 {

 81     return last + 1;

 82 }

 83 

 84 template <typename T>

 85 int seqlist<T>::listsize(void)

 86 {

 87     return maxsize;

 88 }

 89 

 90 template <typename T>

 91 int seqlist<T>::searchlist(T t)

 92 {

 93     if (!isempty())

 94     {

 95         cout << "search data=" << t << endl;

 96         for (int i = 0; i <= last; i++)

 97             if (t - lists[i] < 0.00000001)

 98                 return i + 1;

 99     }

100     return -1;

101 }

102 

103 template <typename T>

104 bool seqlist<T>::getdata(int i, T & t)

105 {

106     if (i > 0 && i <= last + 1)

107     {

108         t = lists[i - 1];

109         return true;

110     }

111     else

112         return false;

113 }

114 

115 template <typename T>

116 bool seqlist<T>::setdata(int i, T t)

117 {

118     if (i >= 1 && i <= last + 1)

119     {

120         lists[i - 1] = t;

121         return true;

122     }

123     else

124         return false;

125 }

126 

127 template <typename T>

128 bool seqlist<T>::insertdate(T t)

129 {

130     if (isfull())

131     {

132         cout << "The lists is full!" << endl;

133         return false;

134     }

135     else

136     {

137         lists[last + 1] = t;

138         last++;

139         return true;

140     }

141 }

142 

143 template <typename T>

144 bool seqlist<T>::removedate(int i, T & t)

145 {

146     if (isempty())

147     {

148         cout << "the lists is empty!" << endl;

149         return false;

150     }

151     else if (i > 0 && i <= last + 1)

152     {

153         t = lists[i - 1];

154         for (int j = i; j <= last + 1; j++)

155             lists[j - 1] = lists[j];

156         last--;

157         return true;

158     }

159     else

160     {

161         cout << "incorrect number!" << endl;

162         return false;

163     }

164 }

165 

166 template <typename T>

167 bool seqlist<T>::isempty(void)

168 {

169 //   cout << "last" << last << endl;

170     return (last == -1) ? true : false;

171 }

172 

173 template <typename T>

174 bool seqlist<T>::isfull(void)

175 {

176     return (last == maxsize - 1) ? true : false;

177 }

178 

179 template <typename T>

180 void seqlist<T>::sortlist(void)

181 {

182     if (isempty())

183         cout << "lists do not need to sort." << endl;

184     else

185         for (int i = 0; i < last; i++)

186         {

187             int k = i;

188             for (int j = i + 1; j < last + 1; j++)

189                 if (lists[j] < lists [k])

190                     k = j;

191             if (k != i)

192             {

193                 T temp = lists[i];

194                 lists[i] = lists[k];

195                 lists[k] = temp;

196             }

197         }

198 }

199 

200 int main(void)

201 {

202     int x1 = 0, x2 = 0 , x3 = 42 ;

203     double y1 = 97.2775, y2 = 0, y3 = 0 ;

204     bool status;

205 

206     seqlist<int> seq1(30);

207     for (int i = 0; i < 30; i++)

208         seq1.insertdate(rand() % 998);

209     cout << "排序前:" << endl;

210     for (int i = 0; i < 30; i++)

211     {

212         seq1.getdata(i + 1, x2);

213         cout.fill('0');

214         cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << "  ";

215         if ((i + 1) % 5 == 0)

216             cout << endl;

217     }

218     seq1.sortlist();

219     cout << endl;

220     cout << "排序后:" << endl;

221     for (int i = 0; i < 30; i++)

222     {

223         seq1.getdata(i + 1, x2);

224         cout.fill('0');

225         cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << "  ";

226         if ((i + 1) % 5 == 0)

227             cout << endl;

228     }

229     cout << endl;

230     x1 = seq1.listsize();

231     cout << "lists seq1 size:" << x1 << endl;

232     status = seq1.isempty();

233     if (!status)

234         cout << "lists seq1 is not empty." << endl;

235 

236     x1 = seq1.lengthlist();

237     cout << "lists seq1 length:" << x1 << endl;

238     x1 = seq1.searchlist(x3);

239     cout << "lists seq1 search:" << x1 << endl;

240 

241     cout << endl << endl;

242     cout << "building seq2." << endl;

243     seqlist<double> seq2(30);

244     for (int i = 0; i < 30; i++)

245         seq2.insertdate(rand() / (double)(RAND_MAX / 100));

246 

247     status = seq2.isfull();

248     if (status)

249         cout << "lists seq2 is full." << endl;

250     cout << endl;

251 

252     for (int i = 0; i < 20; i++)

253     {

254         seq2.getdata(i + 1, y2);

255         cout << "list[" << setw(2) << i + 1 << "]=" << setw(7) << y2 << "  ";

256         if ((i + 1) % 5 == 0)

257             cout << endl;

258     }

259 

260     cout << endl;

261 

262     x1 = seq2.lengthlist();

263     cout << "lists seq2 length:" << x1 << endl;

264 

265     x1 = seq2.searchlist(y1);

266     cout << "lists seq2 search:" << x1 << endl;

267 

268     seq2.setdata(7, 99);

269     seq2.getdata(7, y2);

270     cout << "seq2 setdata=" << y2 << endl;

271 

272     seq2.removedate(7, y3);

273     cout << "remove data=" << y3 << endl;

274     cout << endl;

275     for (int i = 0; i < 19; i++)

276     {

277         seq2.getdata(i + 1, y2);

278         cout << "list[" << setw(2) << i + 1 << "]=" << setw(7) << y2 << "  ";

279         if ((i + 1) % 5 == 0)

280             cout << endl;

281     }

282 

283     cout << endl;

284     cout << endl;

285 

286     seqlist<int> seq3(seq1);

287     for (int i = 0; i < 30; i++)

288     {

289         seq1.getdata(i + 1, x2);

290         cout.fill('0');

291         cout << "list[" << setw(2) << i + 1 << "]=" << setw(3) << x2 << "  ";

292         if ((i + 1) % 5 == 0)

293             cout << endl;

294     }

295 

296     return 0;

297 }
View Code

 

你可能感兴趣的:(C++)