SqList class 实现

SqList.h
 1 #ifndef SQLIST_H
 2 #define SQLIST_H
 3 
 4 //#include 
 5 
 6 #define LIST_INIT 100
 7 #define LIST_INCREMENT 100
 8 
 9 template <typename T> class SqList
10 {
11 public:
12     SqList();
13     ~SqList();
14     
15     bool isEmpty() const { return (m_length == 0); }
16     bool isFull() const { return (m_length >= m_size); }
17     
18     int getLength() const { return m_length; }
19     int getSize() const { return m_size; }
20     
21     T getElem(int i);
22     
23     int find(T val);//if exist in list, return index; else return -1
24     bool insert(int i, T val);
25     //bool insertAfter(int i, T val);
26     T remove(int i);
27     bool push_back(T val);
28     
29     void visit();
30 
31     void resize(){}//no implement
32     void clear();
33     
34     T operator [](int i);
35     
36 private:
37     T  *m_elem;
38     int m_length;
39     int    m_size;
40 
41 };
42 
43 #include "SqList.cpp"
44 
45 #endif
46 
47 
48 

SqList.cpp
  1 template <typename T> SqList<T>::SqList()
  2 {
  3     T *ptr = new T[LIST_INIT];
  4     if( ptr != NULL)
  5     {
  6         m_elem = ptr;
  7         m_length = 0;
  8         m_size = LIST_INIT;
  9     }
 10     else 
 11     {
 12         cout << "malloc failed";
 13     }
 14 
 15 }
 16 
 17 template <typename T> SqList<T>::~SqList()
 18 {
 19     if(m_elem != NULL)
 20         delete [] m_elem;
 21     m_elem = NULL;
 22 }
 23 
 24 template <typename T> T SqList<T>::getElem(int i)
 25 {
 26     if(i < 0 || i >= m_length)
 27     {
 28         cout << "Invailed index"<<endl;
 29         exit(-1);
 30     }
 31     
 32     return m_elem[i];
 33     
 34 }
 35 
 36 template <typename T> int SqList<T>::find(T val)
 37 {
 38     int i;
 39     for(i = 0; i < m_length; ++i)
 40     {
 41         if(m_elem[i] == val)
 42         {
 43             return i;
 44         }
 45     }
 46     
 47     return -1;
 48     
 49 }
 50 
 51 
 52 template <typename T> bool SqList<T>::insert(int i, T val)//insert before
 53 {
 54     int j;
 55     if(isFull())
 56         resize();
 57     
 58     if(i < 0 || i >= m_length)
 59     {
 60         cout << "Invailed index" << endl;
 61         return false;
 62     }
 63     
 64     for(j = m_length - 1; j >= i; j--)
 65     {
 66         m_elem[j+1= m_elem[j];
 67     }
 68     
 69     m_elem[i] = val;
 70     m_length++;
 71     
 72     return true;
 73 
 74 }
 75 
 76 /*
 77 template <typename T> bool SqList<T>::insertAfter(int i, T val)//insert before
 78 {
 79     int j;
 80     if(isFull())
 81         resize();
 82     
 83     if(i < 0 || i >= m_length)
 84     {
 85         cout << "Invailed index" << endl;
 86         return false;
 87     }
 88     
 89     for(j = m_length - 1; j > i; j--)
 90     {
 91         m_elem[j+1] = m_elem[j];
 92     }
 93     
 94     m_elem[i+1] = val;
 95     m_length++;
 96     
 97     return true;
 98 
 99 }
100 */
101 
102 template <typename T> T SqList<T>::remove(int i)// index: 0 ~ m_length-1
103 {
104     int j;
105     T val;
106     if(i < 0 || i >= m_length)
107     {
108         cout << "Invailed index" << endl;
109         exit(-1);
110     }
111 
112     val = m_elem[i];
113     
114     for(j = i; j < m_length; j++)
115     {
116         m_elem[j] = m_elem[j+1];
117     }
118     
119 //    m_elem[m_length - 1] = 0;
120     m_length--;
121     
122     return val;
123 }
124 
125 template <typename T> bool SqList<T>::push_back(T val)
126 {
127     if(isFull())
128     {
129         cerr << "full, failed to push";
130         return false;
131     }
132     m_elem[m_length] = val;
133     m_length++;
134 
135     return true;
136 }
137 
138 template <typename T> void SqList<T>::visit()
139 {
140     int i;
141     for(i = 0; i < m_length; ++i)
142         cout << m_elem[i] << " ";
143     cout << endl;
144 }
145 
146 template <typename T> void SqList<T>::clear()
147 {
148     int i;
149     for(i = 0; i < m_length; ++i)
150         m_elem[i] = 0;
151     
152     m_length = 0;
153     
154 }
155 
156 template <typename T> T SqList<T>::operator [](int i)
157 {
158     if(i < 0 || i >= m_length)
159     {
160         cerr << "Invailed index" << endl;
161         return;
162     }
163     
164     return m_elem[i];
165     
166 }

测试main.cpp
 1 #include <iostream>
 2 #include <string>
 3 #include "SqList.h"
 4 using namespace std;
 5 
 6 
 7 int main()
 8 {
 9     SqList<int> L;
10     int val;
11     
12     L.push_back(0);
13     L.push_back(1);
14     L.push_back(2);
15     L.push_back(3);
16     L.push_back(4);
17     L.push_back(5);
18     L.push_back(6);
19     L.push_back(7);
20     L.push_back(8);
21     
22     cout << "print the current elements in list: \n";
23     L.visit();
24     
25     val = L.remove(4);
26 
27     cout << "print the current elements in list: \n";
28     L.visit();
29 
30     L.insert(2, val);
31 
32     cout << "print the current elements in list: \n";
33     L.visit();
34     
35     
36     SqList<string> strL;
37     string str;
38     
39     strL.push_back("I");
40     strL.push_back("am");
41     strL.push_back("a");
42     strL.push_back("graduate");
43     strL.push_back("student");
44     strL.push_back("in");
45     strL.push_back("Shanghai");
46     strL.push_back("Jiao");
47     strL.push_back("Tong");
48     strL.push_back("University");
49     
50     cout << "print the current elements in list: \n";
51     //cout << strL.getElem(2);
52     strL.visit();
53     
54     str = strL.remove(4);
55 
56     cout << "print the current elements in list: \n";
57     strL.visit();
58 
59     strL.insert(2, str);
60 
61     cout << "print the current elements in list: \n";
62     strL.visit();
63     
64 
65     return 0;
66     
67 }
68 
69 
70 
posted @ 2007-06-03 01:16  中土  阅读(4929)  评论(0编辑  收藏  举报
©2005-2008 Suprasoft Inc., All right reserved.