Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. ListArray.cpp : ------------->>>>>>>>>>>>>>
  2.  
  3. #ifndef LIST_ARRAY_CPP
  4. #define LIST_ARRAY_CPP
  5.  
  6. #include "ListArray.h"
  7.  
  8. template<typename DataType>
  9. List<DataType>::List(int maxNumber){
  10. dataItems = new DataType[maxNumber];
  11. maxSize = maxNumber;
  12. size = 0;
  13. cursor = 0;
  14. }
  15. template<typename DataType>
  16. List<DataType>::List(const List<DataType> &source):List(){
  17. *this = source;
  18. }
  19. template<typename DataType>
  20. List<DataType>& List<DataType>::operator=( const List<DataType>& source ){
  21. if(&source == this){
  22. return *this;
  23. }
  24. maxSize = source.maxSize;
  25. size = source.size;
  26. cursor = source.cursor;
  27. delete[] dataItems;
  28. dataItems = new DataType[maxSize];
  29. for(int i = 0;i<size;i++){
  30. dataItems[i] = source.dataItems[i];
  31. }
  32. return *this;
  33. }
  34.  
  35. template<typename DataType>
  36. List<DataType>::~List(){
  37. clear();
  38. }
  39.  
  40. template<typename DataType>
  41. void List<DataType>::clear(){
  42. if(maxSize != -1){
  43. maxSize = -1;
  44. delete[] dataItems;
  45. size = 0;
  46. cursor = 0;
  47. }
  48. }
  49.  
  50. template<typename DataType>
  51. void List<DataType>::insert(const DataType &newDataItem)throw (logic_error){
  52. if(isFull()){
  53. throw logic_error("List Is Full");
  54. }
  55. if(size == cursor){
  56. dataItems[size++] = newDataItem;
  57. cursor++;
  58. return;
  59. }
  60. if(size != cursor){
  61. size++;
  62. for(int i = size;i>cursor;i--){
  63. dataItems[i] = dataItems[i-1];
  64. }
  65. dataItems[cursor++] = newDataItem;
  66. }
  67. }
  68.  
  69. template<typename DataType>
  70. void List<DataType>::remove()throw (logic_error){
  71. if(isEmpty()){
  72. throw logic_error("List Is Empty");
  73. }
  74. for(int i = cursor-1;i<size-1;i++){
  75. dataItems[i] = dataItems[i+1];
  76. }
  77. size--;
  78. }
  79.  
  80. template<typename DataType>
  81. void List<DataType>::replace(const DataType &newDataItem)throw (logic_error){
  82. if(isEmpty()){
  83. throw logic_error("List Is Empty");
  84. }
  85. dataItems[cursor-1] = newDataItem;
  86. }
  87. template<typename DataType>
  88. bool List<DataType>::isEmpty()const{
  89. return size == 0;
  90. }
  91. template<typename DataType>
  92. bool List<DataType>::isFull()const{
  93. return size >= maxSize;
  94. }
  95. template<typename DataType>
  96. void List<DataType>::gotoBeginning()throw ( logic_error ){
  97. if(isEmpty()){
  98. throw logic_error("List Is Empty");
  99. }
  100. cursor = 1;
  101. }
  102. template<typename DataType>
  103. void List<DataType>::gotoEnd()throw ( logic_error ){
  104. if(isEmpty()){
  105. throw logic_error("List Is Empty");
  106. }
  107. cursor = size;
  108. }
  109. template<typename DataType>
  110. bool List<DataType>::gotoNext()throw ( logic_error ){
  111. if(isEmpty()){
  112. throw logic_error("List Is Empty");
  113. }
  114. if(cursor >= size){
  115. return false;
  116. }
  117. cursor++;
  118. return true;
  119. }
  120. template<typename DataType>
  121. bool List<DataType>::gotoPrior()throw ( logic_error ){
  122. if(isEmpty()){
  123. throw logic_error("List Is Empty");
  124. }
  125. if(cursor <= 1){
  126. return false;
  127. }
  128. cursor--;
  129. return true;
  130. }
  131. template<typename DataType>
  132. DataType List<DataType>::getCursor () const throw ( logic_error ){
  133. if(isEmpty()){
  134. throw logic_error("List Is Empty");
  135. }
  136. return dataItems[cursor-1];
  137. }
  138.  
  139. template<typename DataType>
  140. void List<DataType>::moveToNth ( int n )throw ( logic_error ){
  141. if(isEmpty()){
  142. throw logic_error("List Is Empty");
  143. }
  144. if(n < 0 || n >= size){
  145. throw logic_error("Index Out Of Bound");
  146. }
  147. cursor = n+1;
  148. }
  149.  
  150. template<typename DataType>
  151. bool List<DataType>::find ( const DataType& searchDataItem )throw ( logic_error ){
  152. if(isEmpty()){
  153. throw logic_error("List Is Empty");
  154. }
  155. for(int i = 0;i<size;i++){
  156. if(searchDataItem == dataItems[i]){
  157. return true;
  158. }
  159. }
  160.  
  161. return false;
  162. }
  163.  
  164. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement