顺序表简单模版

  1 #pragma once
  2 #include<iostream>
  3 template<class T1>
  4 class CLineList{
  5 private:
  6     T1* pData;
  7     T1  nCount;
  8     int  nTabLenth;
  9 
 10 public:
 11     CLineList():nCount(0), nTabLenth(10){
 12         pData = new T1[nTabLenth];
 13         memset(pData, 0, sizeof(T1)*nTabLenth);
 14     }
 15     ~CLineList()
 16     {
 17         delete[] pData;
 18     }
 19 
 20 public:
 21     void Initialization(){
 22         nCount = 0;
 23         nTabLenth = 10;
 24         pData = new T1[nTabLenth];
 25         memset(pData, 0, sizeof(T1)*nTabLenth);
 26     }
 27     void InsertData(T1 nData) {
 28         if (nCount == nTabLenth){
 29             ExpandSpace();
 30         }
 31         pData[nCount] = nData;
 32         nCount++;
 33 
 34     }
 35     void InsertData(T1 nData, int nPos){
 36         if (nCount == nTabLenth){
 37             ExpandSpace();
 38         }
 39         if (nPos > nCount){
 40             pData[nCount] = nData;
 41             nCount++;
 42         }
 43         else{    //插入方式使用位置方式
 44             T1 *pTempData = new T1[nCount - nPos + 1];
 45             memcpy_s(pTempData, sizeof(T1)*(nCount - nPos + 1),
 46                 pData + (nPos - 1), sizeof(T1)*(nCount - nPos + 1));
 47             pData[nPos - 1] = nData;
 48             memcpy_s(pData + nPos, sizeof(T1)*(nCount - nPos + 1),
 49                 pTempData, sizeof(T1)*(nCount - nPos + 1));
 50             delete[] pTempData;
 51             nCount++;
 52         }
 53     }
 54     T1  GetElem(int nPos){
 55         return pData[nPos - 1];
 56     }
 57     int  FindData(T1 nData){
 58         for (int i = 0; i < nTabLenth; i++){
 59             if (pData[i] == nData){
 60                 return i + 1;
 61             }
 62         }
 63         return -1;
 64     }
 65     void DeleteData(T1 n1Data){
 66         if (FindData(nData)){
 67             int temp = FindData(nData) - 1;
 68             memcpy(pData + temp, pData + temp + 1, (nCount - temp - 1)*sizeof(T1));
 69             nCount--;
 70         }
 71         else{
 72             printf("没找到!");
 73         }
 74         if (nCount < nTabLenth / 2){
 75             //缩小空间
 76             NarrowSpace();
 77         }
 78     }
 79     void ClearLinTable(){
 80         memset(pData, 0, nTabLenth);
 81         nCount = 0;
 82     }
 83     int  GetLength(){
 84         return nCount;
 85     }
 86     bool ExpandSpace(){
 87         T1 *pTemp = new T1[nTabLenth * 2];
 88         memset(pTemp, 0, (nTabLenth * 2)*sizeof(T1));
 89         memcpy(pTemp, pData, (nTabLenth * 2)*sizeof(T1));
 90         delete[]pData;
 91         pData = pTemp;
 92         nTabLenth *= 2;
 93         return true;
 94     }
 95     bool NarrowSpace(){
 96         T1 *pTemp = new T1[nTabLenth / 2];
 97         memset(pTemp, 0, (nTabLenth / 2)*sizeof(T1));
 98         memcpy(pTemp, pData, (nTabLenth / 2)*sizeof(T1));
 99         delete[]pData;
100         pData = pTemp;
101         nTabLenth *= 2;
102         return true;
103     }
104 
105 };

 

posted @ 2016-01-21 11:28  天还是那么蓝  阅读(218)  评论(0编辑  收藏  举报