allekco

черновик

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