Advertisement
allekco

lab0 double_connected lists ver 3.0

Nov 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <ostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct Node //узел
  8. {
  9.     int value;
  10.     Node* next;
  11.     Node* prev;//Указатели на адреса следующего и предыдущего элементов списка
  12. };
  13.  
  14. class ListSequence {
  15. private:
  16.     int Length;
  17.     Node* head;
  18.     Node* tail;
  19. public:
  20.     void Init() {
  21.         head = NULL;
  22.         tail = NULL;
  23.         Length = 0;
  24.     }
  25.    
  26.     void Append(int item) {
  27.         struct Node* temp;
  28.         temp = (struct Node*)malloc(sizeof(Node));
  29.         temp->value = item;
  30.         if (head == NULL) { //если список пустой
  31.             head = temp;
  32.             tail = temp;
  33.             head->prev = NULL;
  34.             tail->next = NULL;
  35.             Length = 1;
  36.         }
  37.         else { //список не пустой
  38.             temp->prev = tail;
  39.             tail->next = temp;
  40.             tail = temp;
  41.             tail->next = NULL;
  42.             Length += 1;
  43.         }
  44.     }
  45.  
  46.     void Prepend(int item) {   
  47.         struct Node* temp;
  48.         temp = (struct Node*)malloc(sizeof(Node));
  49.         temp->prev = NULL;
  50.         temp->value = item;
  51.         if (head == NULL) { //если список пустой
  52.             head = temp;
  53.             tail = temp;
  54.             temp->next = NULL;
  55.             Length = 1;
  56.         }
  57.         else { //список не пустой
  58.             temp->next = head;
  59.             head = temp;
  60.             Length += 1;
  61.         }
  62.     }
  63.  
  64.     void InsertAt(int index, int item) {
  65.         if (index == 1) return Prepend(item);
  66.         if (index == Length + 1) return Append(item);
  67.         if (index < 1 || index > Length + 1)
  68.             cout << "This index out of length" << endl;
  69.         else {
  70.             struct Node* temp;
  71.             struct Node* p;
  72.             temp = (struct Node*)malloc(sizeof(Node));
  73.             int i;
  74.             p = head;
  75.             cout << p->value << endl;
  76.             for (i = 1; i < index; i++) {
  77.                 p = p->next;
  78.                 cout << p->value << endl;
  79.             }
  80.             temp->value = item;
  81.             temp->prev = p->prev;
  82.             temp->next = p;
  83.             p=p->prev;
  84.             p->next = temp;
  85.             p = temp->next;
  86.             p->prev = temp;
  87.             Length += 1;
  88.         }
  89.     }
  90.  
  91.     int getLength() {
  92.         cout << "Lenght of sequence: ";
  93.         cout << Length << endl;
  94.         return Length;
  95.     }
  96.  
  97.     int getIsEmpty() {
  98.         if (Length == 0) {
  99.             cout << "Sequence is empty" << endl;
  100.             return 1;
  101.         }
  102.         else {
  103.             cout << "Sequence isn't empty" << endl;
  104.             return -1;
  105.         }
  106.     }
  107.  
  108.     int Get(int index) {
  109.         if ((Length <= index) || (index < 0)) {
  110.             cout << "Element with index " << index << " didn't find" << endl;
  111.             return -1;
  112.         }
  113.         else {
  114.             int i;
  115.             Node* temp;
  116.             temp = head;
  117.             for (i = 0; i < index; i++) {
  118.                 temp = temp->next;
  119.             }
  120.             cout << "Element with index " << index << ": ";
  121.             cout << temp->value << endl;
  122.             return temp->value;
  123.         }
  124.     }
  125.  
  126.     int GetFirst() {
  127.         cout << "First element. ";
  128.         return Get(0);
  129.     }
  130.  
  131.     int GetLast() {
  132.         cout << "Last element. ";
  133.         return Get(Length - 1);
  134.     }
  135.  
  136.     void Remove(int item) {
  137.         int i;
  138.         Node* p = head;
  139.         Node* p2 = head;
  140.         if (head->value == item) {
  141.             RemoveFirst();
  142.             Length -= 1;
  143.         }
  144.         else {
  145.             for (i = 1; i < Length; i++) {
  146.                 if ((p->next->value == item) && (p->next->next != NULL)) {
  147.                     Length -= 1;
  148.                     p->next = p->next->next;
  149.                     p = p->next;
  150.                     p->prev = p2;
  151.                     break;
  152.                 }
  153.                 if ((p->next->value == item) && (p->next->next == NULL)) {
  154.                     RemoveLast();
  155.                     break;
  156.                 }
  157.                 p = p->next;
  158.                 p2 = p2->next;
  159.             }
  160.         }
  161.     }
  162.  
  163.     void RemoveFirst() {
  164.         if (head == tail) {
  165.             head = NULL;
  166.             tail = NULL;
  167.         }
  168.         else {
  169.             Node* q = head;
  170.             head = q->next;
  171.             q->next->prev = head;
  172.         }
  173.     }
  174.  
  175.     void RemoveLast() {
  176.         Node* l = tail;
  177.         tail = l->prev;
  178.         l->prev->next = tail;
  179.         tail->next = NULL;
  180.     }
  181.  
  182.     ListSequence GetSubsequence(int startIndex, int endIndex) {
  183.         ListSequence B;
  184.         B.Init();
  185.         Node* p = head;
  186.         if ((startIndex < 0) || (endIndex < 0) || (endIndex < startIndex) || (endIndex >= Length)) {
  187.             cout << "Error with index" << endl;
  188.         }
  189.         else {
  190.             for (int i = 0; i < startIndex; i++) {
  191.                 p = p->next;
  192.             }
  193.             for (int j = startIndex; j <= endIndex; j++) {
  194.                 B.Append(p->value);
  195.                 p = p->next;
  196.             }
  197.         }
  198.         return B;
  199.     }
  200.  
  201.     void Show() {
  202.         Node* temp = head;
  203.         while (temp != NULL) {
  204.             cout << temp->value << " ";
  205.             temp = temp->next;
  206.         }
  207.         cout << endl;
  208.     }
  209. };
  210.  
  211. int main() {
  212.     ListSequence A;
  213.     A.Init();
  214.     A.getIsEmpty();
  215.     A.getLength();
  216.     cout << endl;
  217.     A.Append(23);
  218.     A.getLength();
  219.     A.GetFirst();
  220.     A.GetLast();
  221.     A.Get(0);
  222.     A.Get(-1);
  223.     A.Get(1);
  224.     cout << endl;
  225.     A.Append(43);
  226.     A.Show();
  227.     A.getLength();
  228.     A.GetFirst();
  229.     A.GetLast();
  230.     A.Get(0);
  231.     A.Get(1);
  232.     A.Get(-1);
  233.     A.Get(2);
  234.     cout << endl;
  235.     A.Prepend(53);
  236.     A.getLength();
  237.     A.GetFirst();
  238.     A.GetLast();
  239.     A.Get(0);
  240.     A.Get(1);
  241.     A.Get(-1);
  242.     A.Get(3);
  243.     cout << endl;
  244.     ListSequence A2;
  245.     A2 = A.GetSubsequence(1, 1);
  246.     A2.getLength();
  247.     A2.GetFirst();
  248.     A2.GetLast();
  249.  
  250.     return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement