Advertisement
allekco

lab0 ver 5.0

Dec 7th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <ostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. template <typename TElement> class Sequence {
  9. public:
  10.     virtual void Append(TElement item) = 0;
  11.     virtual void Prepend(TElement item) = 0;
  12.     virtual void InsertAt(int index, TElement item) = 0;
  13.     virtual int getLength() = 0;
  14.     virtual int getIsEmpty() = 0;
  15.     virtual TElement Get(int index) = 0;
  16.     virtual TElement GetFirst() = 0;
  17.     virtual TElement GetLast() = 0;
  18.     virtual void Remove(TElement item) = 0;
  19.  
  20. };
  21.  
  22. template <typename TElement> struct Node //узел
  23. {
  24.     TElement value;
  25.     Node<TElement>* next;
  26.     Node<TElement>* prev;//Указатели на адреса следующего и предыдущего элементов списка
  27. };
  28.  
  29. template <typename TElement> class ListSequence: public Sequence<TElement> {
  30. private:
  31.     int Length;
  32.     Node<TElement>* head;
  33.     Node<TElement>* tail;
  34. public:
  35.     void Init() {
  36.         head = NULL;
  37.         tail = NULL;
  38.         Length = 0;
  39.     }
  40.     void Append(TElement item) {
  41.         struct Node<TElement>* temp;
  42.         temp = (struct Node<TElement>*)malloc(sizeof(Node<TElement>));
  43.         temp->value = item;
  44.         if (head == NULL) { //если список пустой
  45.             head = temp;
  46.             tail = temp;
  47.             head->prev = NULL;
  48.             tail->next = NULL;
  49.             Length = 1;
  50.         }
  51.         else { //список не пустой
  52.             temp->prev = tail;
  53.             tail->next = temp;
  54.             tail = temp;
  55.             tail->next = NULL;
  56.             Length += 1;
  57.         }
  58.     }
  59.     void Prepend(TElement item) {
  60.         struct Node<TElement>* temp;
  61.         temp = (struct Node<TElement>*)malloc(sizeof(Node<TElement>));
  62.         temp->prev = NULL;
  63.         temp->value = item;
  64.         if (head == NULL) { //если список пустой
  65.             head = temp;
  66.             tail = temp;
  67.             temp->next = NULL;
  68.             Length = 1;
  69.         }
  70.         else { //список не пустой
  71.             temp->next = head;
  72.             head = temp;
  73.             Length += 1;
  74.         }
  75.     }
  76.     void InsertAt(int index, TElement item) {
  77.         if (index == 1) return Prepend(item);
  78.         if (index == Length + 1) return Append(item);
  79.         if (index < 1 || index > Length + 1)
  80.             cout << "This index out of length" << endl;
  81.         else {
  82.             struct Node<TElement>* temp;
  83.             struct Node<TElement>* p;
  84.             temp = (struct Node<TElement>*)malloc(sizeof(Node<TElement>));
  85.             int i;
  86.             p = head;
  87.             cout << p->value << endl;
  88.             for (i = 1; i < index; i++) {
  89.                 p = p->next;
  90.                 cout << p->value << endl;
  91.             }
  92.             temp->value = item;
  93.             temp->prev = p->prev;
  94.             temp->next = p;
  95.             p=p->prev;
  96.             p->next = temp;
  97.             p = temp->next;
  98.             p->prev = temp;
  99.             Length += 1;
  100.         }
  101.     }
  102.     int getLength() {
  103.         cout << "Lenght of sequence: ";
  104.         cout << Length << endl;
  105.         return Length;
  106.     }
  107.     int getIsEmpty() {
  108.         if (Length == 0) {
  109.             cout << "Sequence is empty" << endl;
  110.             return 1;
  111.         }
  112.         else {
  113.             cout << "Sequence isn't empty" << endl;
  114.             return -1;
  115.         }
  116.     }
  117.     TElement Get(int index) {
  118.         if ((Length <= index) || (index < 0)) {
  119.             cout << "Element with index " << index << " didn't find" << endl;
  120.             return -1;
  121.         }
  122.         else {
  123.             int i;
  124.             Node<TElement>* temp;
  125.             temp = head;
  126.             for (i = 0; i < index; i++) {
  127.                 temp = temp->next;
  128.             }
  129.             cout << "Element with index " << index << ": ";
  130.             cout << temp->value << endl;
  131.             return temp->value;
  132.         }
  133.     }
  134.     TElement GetFirst() {
  135.         cout << "First element. ";
  136.         return Get(0);
  137.     }
  138.     TElement GetLast() {
  139.         cout << "Last element. ";
  140.         return Get(Length - 1);
  141.     }
  142.     void Remove(TElement item) {
  143.         int i;
  144.         Node<TElement>* p = head;
  145.         Node<TElement>* p2 = head;
  146.         if (head->value == item) {
  147.             RemoveFirst();
  148.             Length -= 1;
  149.         }
  150.         else {
  151.             for (i = 1; i < Length; i++) {
  152.                 if ((p->next->value == item) && (p->next->next != NULL)) {
  153.                     Length -= 1;
  154.                     p->next = p->next->next;
  155.                     p = p->next;
  156.                     p->prev = p2;
  157.                     break;
  158.                 }
  159.                 if ((p->next->value == item) && (p->next->next == NULL)) {
  160.                     RemoveLast();
  161.                     break;
  162.                 }
  163.                 p = p->next;
  164.                 p2 = p2->next;
  165.             }
  166.         }
  167.     }
  168.     void RemoveFirst() {
  169.         if (head == tail) {
  170.             head = NULL;
  171.             tail = NULL;
  172.         }
  173.         else {
  174.             Node<TElement>* q = head;
  175.             head = q->next;
  176.             q->next->prev = head;
  177.         }
  178.     }
  179.     void RemoveLast() {
  180.         Node<TElement>* l = tail;
  181.         tail = l->prev;
  182.         l->prev->next = tail;
  183.         tail->next = NULL;
  184.     }
  185.     ListSequence GetSubsequence(int startIndex, int endIndex) {
  186.         ListSequence B;
  187.         B.Init();
  188.         Node<TElement>* p = head;
  189.         if ((startIndex < 0) || (endIndex < 0) || (endIndex < startIndex) || (endIndex >= Length)) {
  190.             cout << "Error with index" << endl;
  191.         }
  192.         else {
  193.             for (int i = 0; i < startIndex; i++) {
  194.                 p = p->next;
  195.             }
  196.             for (int j = startIndex; j <= endIndex; j++) {
  197.                 B.Append(p->value);
  198.                 p = p->next;
  199.             }
  200.         }
  201.         return B;
  202.     }
  203.     void Show() {
  204.         Node<TElement>* temp = head;
  205.         while (temp != NULL) {
  206.             cout << temp->value << " ";
  207.             temp = temp->next;
  208.         }
  209.         cout << endl;
  210.     }
  211. };
  212.  
  213. template <typename TElement> class ArraySequence: public Sequence<TElement> {
  214. private:
  215.     TElement* point; // указатель на массив
  216.     int Length; // размер массива
  217. public:
  218.     void Init() {
  219.         Length = 0; // по умолчанию размер массива = 10 элементов
  220.         point = new TElement[Length]; // выделить место в памяти для массива
  221.     }
  222.     void Append(TElement item) {
  223.         int i;
  224.         TElement* tmp;
  225.         tmp = new TElement[Length]; // выделяем память
  226.         for (i = 0; i < Length; i++) {
  227.             tmp[i] = point[i];
  228.         }
  229.         Length = Length + 1; // увеличиваем размер массива на 1
  230.         point = new TElement[Length];
  231.         for (i = 0; i < Length - 1; i++) {
  232.             point[i] = tmp[i];
  233.         }
  234.         point[Length - 1] = item;
  235.         delete tmp;
  236.     }
  237.     void Prepend(TElement item) {
  238.         int i;
  239.         TElement* tmp;
  240.         tmp = new TElement[Length]; // выделяем память
  241.         for (i = 0; i < Length; i++) {
  242.             tmp[i] = point[i];
  243.         }
  244.         Length = Length + 1; // увеличиваем размер массива на 1
  245.         point = new TElement[Length];
  246.         point[0] = item;
  247.         for (i = 1; i < Length; i++) {
  248.             point[i] = tmp[i - 1];
  249.         }
  250.         delete tmp;
  251.     }
  252.     void InsertAt(int index, TElement item) {
  253.         if ((Length + 1 <= index) || (index < 0)) {
  254.             cout << "This index out of length" << endl;
  255.         }
  256.         else {
  257.             int i;
  258.             TElement* tmp;
  259.             tmp = new TElement[Length]; // выделяем память
  260.             for (i = 0; i < Length; i++) {
  261.                 tmp[i] = point[i];
  262.             }
  263.             Length = Length + 1; // увеличиваем размер массива на 1
  264.             point = new TElement[Length];
  265.             for (i = 0; i < index; i++)
  266.                 point[i] = tmp[i];
  267.             point[index] = item;
  268.             for (i = index + 1; i < Length; i++)
  269.                 point[i] = tmp[i - 1];
  270.             delete tmp;
  271.         }
  272.     }
  273.     int getLength() {
  274.         cout << "Lenght of sequence: ";
  275.         cout << Length << endl;
  276.         return Length;
  277.     }
  278.     int getIsEmpty() {
  279.         if (Length == 0) {
  280.             cout << "Sequence is empty" << endl;
  281.             return 1;
  282.         }
  283.         else {
  284.             cout << "Sequence isn't empty" << endl;
  285.             return -1;
  286.         }
  287.     }
  288.     TElement Get(int index) {
  289.         if ((Length <= index) || (index < 0)) {
  290.             cout << "Element with index " << index << " didn't find" << endl;
  291.             return -1;
  292.         }
  293.         else {
  294.             cout << "Element with index " << index << ": ";
  295.             cout << point[index] << endl;
  296.             return point[index];
  297.         }
  298.     }
  299.     TElement GetFirst() {
  300.         if (Length != 0) {
  301.             cout << "First element: " << point[0] << endl;
  302.             return point[0];
  303.         }
  304.         else {
  305.             cout << "Sequance is empty" << endl;
  306.             return -1;
  307.         }
  308.     }
  309.     TElement GetLast() {
  310.         if (Length != 0) {
  311.             cout << "Last element: " << point[Length - 1] << endl;
  312.             return point[Length - 1];
  313.         }
  314.         else {
  315.             cout << "Sequance is empty" << endl;
  316.             return -1;
  317.         }
  318.     }
  319.     void Remove(TElement item) {
  320.         int i = 0, index, key = 0;
  321.         while (i < Length) {
  322.             if (point[i] == item) {
  323.                 index = i;
  324.                 i = Length;
  325.                 key = 1;
  326.             }
  327.             i++;
  328.         }
  329.         if (key) { //удаляем
  330.             TElement* tmp;
  331.             tmp = new TElement[Length];
  332.             for (i = 0; i < Length; i++) {
  333.                 tmp[i] = point[i];
  334.             }
  335.             Length = Length - 1;
  336.             point = new TElement[Length];
  337.             for (i = 0; i < index; i++) {
  338.                 point[i] = tmp[i];
  339.             }
  340.             for (i = index; i < Length; i++) {
  341.                 point[i] = tmp[i + 1];
  342.             }
  343.         }
  344.     }
  345.     ArraySequence GetSubsequence(int startIndex, int endIndex) {
  346.         ArraySequence B;
  347.         B.Init();
  348.         if ((startIndex < 0) || (endIndex < 0) || (endIndex < startIndex) || (endIndex >= Length)) {
  349.             cout << "Error with index" << endl;
  350.         }
  351.         else {
  352.             for (int i = startIndex; i <= endIndex; i++) {
  353.                 B.Append(point[i]);
  354.             }
  355.         }
  356.         return B;
  357.     }
  358.     void Show() {
  359.         int i;
  360.         for (i = 0; i < Length; i++) {
  361.             cout << point[i] << " ";
  362.         }
  363.         cout << endl;
  364.     }
  365. };
  366.  
  367.  
  368. void test1(ArraySequence<int> A, ArraySequence<int> A2){
  369.     A.Init();
  370.     A.getIsEmpty();
  371.     A.getLength();
  372.     cout << endl;
  373.     A.Append(23);
  374.     A.getLength();
  375.     A.GetFirst();
  376.     A.GetLast();
  377.     A.Get(0);
  378.     A.Get(-1);
  379.     A.Get(1);
  380.     cout << endl;
  381.     A.Append(43);
  382.     A.Show();
  383.     A.getLength();
  384.     A.GetFirst();
  385.     A.GetLast();
  386.     A.Get(0);
  387.     A.Get(1);
  388.     A.Get(-1);
  389.     A.Get(2);
  390.     cout << endl;
  391.     A.Prepend(53);
  392.     A.getLength();
  393.     A.GetFirst();
  394.     A.GetLast();
  395.     A.Get(0);
  396.     A.Get(1);
  397.     A.Get(-1);
  398.     A.Get(3);
  399.     cout << endl;
  400.     A2 = A.GetSubsequence(1, 1);
  401.     A2.getLength();
  402.     A2.GetFirst();
  403.     A2.GetLast();
  404.     cout << endl;
  405. }
  406.  
  407.  
  408. void test2(ListSequence<int> A, ListSequence<int> A2) {
  409.     A.Init();
  410.     A.getIsEmpty();
  411.     A.getLength();
  412.     cout << endl;
  413.     A.Append(23);
  414.     A.getLength();
  415.     A.GetFirst();
  416.     A.GetLast();
  417.     A.Get(0);
  418.     A.Get(-1);
  419.     A.Get(1);
  420.     cout << endl;
  421.     A.Append(43);
  422.     A.Show();
  423.     A.getLength();
  424.     A.GetFirst();
  425.     A.GetLast();
  426.     A.Get(0);
  427.     A.Get(1);
  428.     A.Get(-1);
  429.     A.Get(2);
  430.     cout << endl;
  431.     A.Prepend(53);
  432.     A.getLength();
  433.     A.GetFirst();
  434.     A.GetLast();
  435.     A.Get(0);
  436.     A.Get(1);
  437.     A.Get(-1);
  438.     A.Get(3);
  439.     cout << endl;
  440.     A2 = A.GetSubsequence(1, 1);
  441.     A2.getLength();
  442.     A2.GetFirst();
  443.     A2.GetLast();
  444.     cout << endl;
  445. }
  446.  
  447. int main() {
  448.     int i = 1;
  449.     while (i) {
  450.         cout << "Which Sequence do you want?" << endl;
  451.         cout << "1 - Array Sequance" << endl;
  452.         cout << "2 - List Sequance" << endl;
  453.         cout << "0 - EXIT" << endl;
  454.         int n;
  455.         cin >> n;
  456.         cout << endl;
  457.         ArraySequence<int> A1;
  458.         ArraySequence<int> A2;
  459.         ListSequence<int> B1;
  460.         ListSequence<int> B2;
  461.         switch (n) {
  462.         case 1:
  463.             test1(A1, A2);
  464.             break;
  465.         case 2:
  466.             test2(B1, B2);
  467.             break;
  468.         case 0:
  469.             i = 0;
  470.             break;
  471.         default:
  472.             break;
  473.         }
  474.     }
  475.     return 0;
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement