随笔-13  评论-0  文章-2  trackbacks-0

/**
  Linear List
  Member function as follow:
  Size()
  Insert(T)        // inset an elm
  Delete(T)        // delete an elm
  Empty()         // if it is an empty list
  Print()
  Find(T&)          // find an elm
  
  Use C++ template
**/

 1#include<iostream>
 2#include<string>
 3using namespace std;
 4const int MAXSIZE = 100;
 5template<typename T>
 6class Sqlist
 7{
 8       private:
 9          T elem[MAXSIZE];
10          int size;
11      public:
12           Sqlist(){ size = 0;}
13           Sqlist(T*int);
14           virtual ~Sqlist();
15           void Delete(int);
16           bool Empty()return (size == 0? true : false; };
17           void Insert(T);
18           int Size()return size; }
19           void Print();
20          int Find(T);                
21}
;
22
23
24template<typename T> Sqlist<T>::Sqlist(T* a, int s)
25{
26         forint i = 0; i < s; i++ )
27         elem[i] = a[i];
28         size = s;
29}
      
30
31template<typename T> Sqlist<T>::~Sqlist(){};
32
33template<typename T> void Sqlist<T>::Delete(int pos)
34{
35          forint i = pos; i < size; i++ )
36          elem[i] = elem[i + 1];
37          size--;
38}

39
40template<typename T> void Sqlist<T>::Insert(T s)
41{
42          elem[size] = s;
43          size++;
44}

45
46template<typename T> void Sqlist<T>::Print()
47{
48          forint i = 0; i < size - 1; i++ )
49          cout<< elem[i] << " ";
50          cout <<  elem[size - 1<< endl;
51}

52
53template<typename T> int Sqlist<T>::Find(T s)
54{
55          int i;
56          for(i = 0; i < size; i++ )
57          if( elem[i] == s )return i;
58          cout << "No such an element" << endl;
59          system("pause");
60          exit(0);
61}
测试代码:
// Test function

int main()
{
     cout 
<< "Please input a number which Linearlist's length:" << endl;
     
int size;
     
int c[100];
     cin 
>> size;
     cout 
<< "Please input the number which you want to add in this Line:" << endl;
     
forint i = 0 ; i < size; i++ )
    
{
          
int a;
          cin 
>> a;
          c[i] 
= a;
    }

    Sqlist
<int> list(c, size); 
    cout 
<< "-------------------------------------------------------\n"<< endl;
    cout 
<< "Linearlist's information:" << endl;
    cout 
<< "Size: " << list.Size();
    cout 
<< "\nDo you want to delete some elements ? " << endl;
    cout 
<< "yes / no ?"  << endl;
    
string s;
    cin 
>> s;
    
if( s == "yes" )
    
{
         cout 
<< "the position of this List:" << endl;
        
int p;
        cin 
>> p;
        list.Delete(p);
        cout 
<< "Now there has " << list.Size() << " elements " << endl;
        list.Print();
    }

    cout 
<< "\nDo you want to find some elements ? " << endl;
    cout 
<< "yes / no ?" << endl;
    cin 
>> s;
    
if( s == "yes" )
    
{
         
int ele;
         cout 
<< "which element ?" << endl;
         cin 
>> ele;
         cout 
<< "It is in" << list.Find(ele) <<" position"<< endl;
    }

    cout 
<< "-------------------------------------------------------\n"<< endl;
    system(
"pause");
    
return 0;
}
 
posted on 2009-02-18 23:27 亦夏 阅读(320) 评论(0)  编辑 收藏 引用 所属分类: DataStruct

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理