BHXSpecter

Templates

Nov 16th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #ifndef H_LISTTYPE
  2. #define H_LISTTYPE
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template <class elemType>
  10. class listType
  11. {
  12.     public:
  13.         bool isEmpty() const;
  14.         bool isFull() const;
  15.         int getLength() const;
  16.         int getMaxSize() const;
  17.         void sort();
  18.         void print() const;
  19.         void insertAt(const elemType& item, int postion);
  20.         listType(int listSize = 50);
  21.         ~listType();
  22.     private:
  23.         int maxSize;
  24.         int length;
  25.         elemType *list;
  26. };
  27.  
  28. template <class elemType>
  29. bool listType<elemType>::isEmpty() const
  30. {
  31.     return (length == 0)
  32. }
  33.  
  34. template <class elemType>
  35. bool listType<elemType>::isFull() const
  36. {
  37.     return (length == maxSize);
  38. }
  39.  
  40. template <class elemType>
  41. int listType<elemType>::getLength() const
  42. {
  43.     return length;
  44. }
  45.  
  46. template <class elemType>
  47. int listType<elemType>::getMaxSize() const
  48. {
  49.     return maxSize;
  50. }
  51.  
  52. template <class elemType>
  53. listType<elemType>::listType(int listSize)
  54. {
  55.     maxSize = listSize;
  56.     length = 0;
  57.     list = new elemType[maxSize];
  58. }
  59.  
  60. template <class elemType>
  61. listType<elemType>::~listType()
  62. {
  63.     delete [] list;
  64. }
  65.  
  66. template <class elemType>
  67. void listType<elemType>::sort()
  68. {
  69.     int i, j;
  70.     int min;
  71.     elemType temp;
  72.    
  73.     for (i = 0; i < length; i++)
  74.     {
  75.         min = i;
  76.         for (j = i + 1; j < length; ++j)
  77.             if (list[j] < list[min])
  78.                 min = j;
  79.            
  80.             temp = list[i];
  81.             list[i] = list[min];
  82.             list[min] = temp;
  83.     }
  84. }
  85.  
  86. template <class elemType>
  87. void listType<elemType>::print() const
  88. {
  89.     int i;
  90.     for (i = 0; i < length; ++i)
  91.         cout << list[i] << " ";
  92.     cout ,< endl;
  93. }
  94.  
  95. template <class elemType>
  96. void listType<elemType>::insertAt(const elemType& item, int position)
  97. {
  98.     assert(position >= 0 && position < maxSize)
  99.     list[position] = item;
  100.     length++;
  101. }
  102.  
  103. #endif
  104.  
Advertisement
Add Comment
Please, Sign In to add comment