Advertisement
amsavchenko

Untitled

Sep 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. template <typename TElement>
  2. struct Node {
  3.     TElement field;
  4.     Node *next;
  5.     Node *prev;
  6. };
  7.  
  8. template <typename TElement>
  9. class ListSequence: public Sequence <TElement> { // <--- ПОДКЛАСС ПОСЛЕДОВАТЕЛЬНОСТЬ С двусвязным СПИСКОМ
  10.     Node<TElement> *First, *Last;
  11. public:
  12.     ListSequence();
  13.     ~ListSequence();
  14.     void Append(TElement item);
  15.     void Prepend(TElement item);
  16.     TElement GetFirst();
  17.     TElement GetLast();
  18.     void InsertAt(int index, TElement item);
  19.     TElement Get(int index);
  20.     void Remove(TElement item);
  21.     ListSequence<TElement> GetSubsequence(int startIndex, int endIndex);
  22.     void PrintAll ();
  23. };
  24.  
  25. template <typename TElement>
  26. ListSequence<TElement>:: ListSequence() : Sequence<TElement>() {
  27.     First = NULL;
  28.     Last = NULL;
  29. }
  30.  
  31. template <typename TElement>
  32. ListSequence<TElement>:: ~ListSequence(){
  33.     while (First) {
  34.         Last = First->next;
  35.         delete First;
  36.         First = Last;
  37.     }
  38. }
  39.  
  40. template <typename TElement>
  41. TElement ListSequence<TElement>:: GetFirst() {
  42.     if(First == NULL) {
  43.         throw ExceptionOutOfBounds(0);
  44.     }
  45.     return First->field;
  46. }
  47.  
  48. template <typename TElement>
  49. TElement ListSequence<TElement>:: GetLast() {
  50.     if (Last == NULL) {
  51.         throw ExceptionOutOfBounds(0);
  52.     }
  53.     return Last->field;
  54. }
  55.  
  56. template <typename TElement>
  57. void ListSequence<TElement>:: Append(TElement item) {
  58.     Node<TElement> *temp = new Node<TElement>;
  59.     temp->next = NULL;
  60.     temp->field = item;
  61.     if(this->length != 0){
  62.         temp->prev = Last;
  63.         Last->next = temp;
  64.         Last = temp;
  65.         this->length++;
  66.     }
  67.     else {
  68.         temp->prev = NULL;
  69.         First = temp;
  70.         Last = temp;
  71.         this->length++;
  72.         this->isEmpty = false;
  73.     }
  74. }
  75.  
  76. template <typename TElement>
  77. void ListSequence<TElement>:: Prepend(TElement item) {
  78.     Node<TElement> *temp = new Node<TElement>;
  79.     temp->prev = NULL;
  80.     temp->field = item;
  81.     if(this->length != 0) {
  82.         temp->next=First;
  83.         First->prev=temp;
  84.         First=temp;
  85.         this->length++;
  86.     }
  87.     else {
  88.         temp->next=NULL;
  89.         First=temp;
  90.         Last=temp;
  91.         this->length++;
  92.         this->isEmpty = false;
  93.     }
  94. }
  95.  
  96. template <typename TElement>
  97. void ListSequence<TElement>:: InsertAt(int index, TElement item) {
  98.     if(index < 0 || index > this->length) {
  99.         throw ExceptionOutOfBounds(index);
  100.     }
  101.     if(this->isEmpty == true) this->isEmpty = false;
  102.     if(index == 0) Prepend(item);
  103.     else if(index == this->length - 1) Append(item);
  104.     else {
  105.         this->length++;
  106.         Node<TElement> *temp=new Node<TElement>;
  107.         temp->field = item;
  108.         temp->next = First->next;
  109.         temp->prev = First;
  110.         while(index-2) {
  111.             temp->prev = temp->prev->next;
  112.             temp->next = temp->next->next;
  113.             index --;
  114.         }
  115.         temp->prev->next = temp;
  116.         temp->next->prev = temp;
  117.     }
  118. }
  119.  
  120. template <typename TElement>
  121. TElement ListSequence<TElement>:: Get(int index) {
  122.     if (index < 0 || index > this->length - 1) {
  123.         throw ExceptionOutOfBounds(index);
  124.     }
  125.     if(index == 0) return GetFirst();
  126.     if(index == this->length - 1) return GetLast();
  127.     else {
  128.         Node<TElement> *temp = new Node<TElement>;
  129.         temp->next = First->next;
  130.         while(index - 1) {
  131.             temp->next = temp->next->next;
  132.             index --;
  133.         }
  134.         return temp->next->field;
  135.     }
  136. }
  137.  
  138. template <typename TElement>
  139. void ListSequence<TElement>:: Remove(TElement item) {
  140.     int k = 0;
  141.     while(k < this->length - 1) {
  142.         Node<TElement> *temp = new Node<TElement>;
  143.         int index = k;
  144.         temp->next = First;
  145.         while(temp->next->field != item && index < this->length - 1) {
  146.             temp->next = temp->next->next;
  147.             index ++;
  148.             k ++;
  149.         }
  150.         if(temp->next->field == item) {
  151.             temp = temp->next;
  152.             if(temp->prev != 0) temp->prev->next = temp->next;
  153.             else First = temp->next;
  154.             if(temp->next != 0) temp->next->prev = temp->prev;
  155.             else Last = temp->prev;
  156.             if (this->length == 0) this->isEmpty = false;
  157.             this->length--;
  158.             delete temp;
  159.         }
  160.     }
  161. }
  162.  
  163. template <typename TElement>
  164. ListSequence<TElement> ListSequence<TElement>:: GetSubsequence(int startIndex, int endIndex) {
  165.     if (startIndex < 0 || startIndex > this->length + 1) {
  166.         throw ExceptionOutOfBounds(startIndex);
  167.     }
  168.     if (endIndex < 0 || endIndex >= this->length + 1) {
  169.         throw ExceptionOutOfBounds(endIndex);
  170.     }
  171.     if (endIndex - startIndex < 0) {
  172.         throw ExceptionOutOfBounds(endIndex - startIndex);
  173.     }
  174.     ListSequence<TElement> Subsequence;
  175.     Node<TElement> *temp = new Node<TElement>;
  176.     temp = First;
  177.     for (int i = 0; i < startIndex; i ++) temp = temp->next;
  178.     for (int i = startIndex - 1; i < endIndex; i++) {
  179.         Subsequence.Append(temp->field);
  180.         temp = temp->next;
  181.     }
  182.     return Subsequence;
  183. }
  184.  
  185. template <typename TElement>
  186. void ListSequence<TElement>:: PrintAll() {
  187.     Node<TElement> *temp = new Node<TElement>;
  188.     temp = First;
  189.     cout << "Последовательность: " ;
  190.     while (temp) {
  191.         cout << temp->field << " ";
  192.         temp = temp->next;
  193.     }
  194.     cout << endl;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement