Advertisement
irapilguy

Untitled

Nov 29th, 2020
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdarg.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8.     int x;
  9.     Node* next;
  10.     Node* prev;
  11.     Node(int x)
  12.     {
  13.         this->x = x;
  14.         prev = next = NULL;
  15.     }
  16. };
  17.  
  18. class DualList
  19. {
  20. private:
  21.     Node* begin, * end;
  22.     int count;
  23. public:
  24.     DualList()
  25.     {
  26.         begin = end = NULL;
  27.         count = 0;
  28.     }
  29.     DualList(const DualList& a)
  30.     {
  31.  
  32.     }
  33.     DualList(DualList& list)
  34.     {
  35.         begin = end = NULL;
  36.         count = 0;
  37.  
  38.         Node* temp = list.begin;
  39.         while (temp)
  40.         {
  41.             Insert(count, temp->x);
  42.             count++;
  43.             temp = temp->next;
  44.         }
  45.     }
  46.     void Insert(int pos, int x);
  47.     int Size();
  48.     void Print();
  49.     void reversePrint();
  50.     void Del(int x);
  51.  
  52.     DualList operator+(DualList& a)
  53.     {
  54.  
  55.  
  56.         return *this;
  57.     }
  58.  
  59.     DualList& operator =(DualList& a)
  60.     {
  61.         Node* newTemp = a.begin;
  62.         int cnt = a.count;
  63.         for (int i = 0; i < count; i++)
  64.         {
  65.             this->Insert(cnt, newTemp->x);
  66.             newTemp = newTemp->next;
  67.             cnt++;
  68.         }
  69.         return *this;
  70.     }
  71.  
  72.     bool operator !=(DualList& a)
  73.     {
  74.         Node* newTmp = a.begin;
  75.         Node* newTmp1 = begin;
  76.         if (count != a.count)
  77.         {
  78.             return true;
  79.         }
  80.         for (int i = 0; i < count; i++)
  81.         {
  82.             if (newTmp1->x != newTmp->x)
  83.             {
  84.                 return true;
  85.             }
  86.             newTmp = newTmp->next;
  87.             newTmp1 = newTmp1->next;
  88.         }
  89.         return false;
  90.     }
  91.  
  92.     bool operator ==(DualList& a)
  93.     {
  94.         Node* newTmp = a.begin;
  95.         Node* newTmp1 = begin;
  96.         /*if (count == a.count)
  97.         {
  98.             return true;
  99.         }*/
  100.         for (int i = 0; i < count; i++)
  101.         {
  102.             if (newTmp1->x != newTmp->x)
  103.             {
  104.                 return false;
  105.             }
  106.             newTmp = newTmp->next;
  107.             newTmp1 = newTmp1->next;
  108.         }
  109.         return true;
  110.     }
  111.  
  112.     bool operator >(DualList& a)
  113.     {
  114.         Node* newTmp = a.begin;
  115.         Node* newTmp1 = begin;
  116.         if (*this == a) return false;
  117.  
  118.         if (count < a.count)
  119.         {
  120.             return false;
  121.         }
  122.         for (int i = 0; i < count; i++)
  123.         {
  124.             if (newTmp1->x < newTmp->x)
  125.             {
  126.                 return false;
  127.             }
  128.             newTmp = newTmp->next;
  129.             newTmp1 = newTmp1->next;
  130.         }
  131.         return true;
  132.     }
  133.  
  134.     bool operator <(DualList& a)
  135.     {
  136.         Node* newTmp = a.begin;
  137.         Node* newTmp1 = begin;
  138.  
  139.         if (*this == a) return false;
  140.  
  141.         if (count > a.count)
  142.         {
  143.             return false;
  144.         }
  145.         for (int i = 0; i < count; i++)
  146.         {
  147.             if (newTmp1->x > newTmp->x)
  148.             {
  149.                 return false;
  150.             }
  151.             newTmp = newTmp->next;
  152.             newTmp1 = newTmp1->next;
  153.         }
  154.         return true;
  155.     }
  156.  
  157.     bool operator >=(DualList& a)
  158.     {
  159.         Node* newTmp = a.begin;
  160.         Node* newTmp1 = begin;
  161.  
  162.         if (count < a.count)
  163.         {
  164.             return false;
  165.         }
  166.         for (int i = 0; i < count; i++)
  167.         {
  168.             if (newTmp1->x < newTmp->x)
  169.             {
  170.                 return false;
  171.             }
  172.             newTmp = newTmp->next;
  173.             newTmp1 = newTmp1->next;
  174.         }
  175.         return true;
  176.     }
  177.  
  178.     bool operator <=(DualList& a)
  179.     {
  180.         Node* newTmp = a.begin;
  181.         Node* newTmp1 = begin;
  182.  
  183.         if (count > a.count)
  184.         {
  185.             return false;
  186.         }
  187.  
  188.         for (int i = 0; i < count; i++)
  189.         {
  190.             if (newTmp1->x > newTmp->x)
  191.             {
  192.                 return false;
  193.             }
  194.             newTmp = newTmp->next;
  195.             newTmp1 = newTmp1->next;
  196.         }
  197.         return true;
  198.     }
  199. };
  200.  
  201. void DualList::Insert(int pos, int x)
  202. {
  203.     if (pos < 0 || pos > count)
  204.     {
  205.         cout << "Incorrect position" << endl;
  206.         return;
  207.     }
  208.     Node* newNode = new Node(x);
  209.     if (pos == 0)
  210.     {
  211.         if (begin == NULL)
  212.         {
  213.             begin = end = newNode;
  214.         }
  215.         else
  216.         {
  217.             newNode->next = begin;
  218.             begin->prev = newNode;
  219.             begin = newNode;
  220.         }
  221.     }
  222.     else if (pos == count)
  223.     {
  224.         end->next = newNode;
  225.         newNode->prev = end;
  226.         end = newNode;
  227.     }
  228.     else
  229.     {
  230.         Node* prevInsert = begin;
  231.         Node* postInsert;
  232.         Node* temp = begin;
  233.         for (int i = 0; i < pos; i++)
  234.         {
  235.             prevInsert = prevInsert->next;
  236.         }
  237.         postInsert = prevInsert->next;
  238.         prevInsert->next = newNode;
  239.         prevInsert->prev = newNode;
  240.     }
  241.     count++;
  242. }
  243.  
  244. int DualList::Size()
  245. {
  246.     return count;
  247. }
  248.  
  249. void DualList::Print()
  250. {
  251.     Node* temp = begin;
  252.     for (int i = 0; i < count; i++)
  253.     {
  254.         cout << temp->x << " ";
  255.         temp = temp->next;
  256.     }
  257. }
  258.  
  259. void DualList::reversePrint()
  260. {
  261.     Node* temp = end;
  262.     for (int i = 0; i < count; i++)
  263.     {
  264.         cout << temp->x << " ";
  265.         temp = temp->prev;
  266.     }
  267. }
  268.  
  269. void DualList::Del(int x)
  270. {
  271.     Node* newNode = new Node(x);
  272.     if (x == 0)
  273.     {
  274.         newNode = newNode->next;
  275.         newNode->prev = NULL;
  276.         delete newNode;
  277.     }
  278.  
  279. }
  280.  
  281. int main()
  282. {
  283.     DualList a;
  284.     DualList b;
  285.     a.Insert(0, 4);
  286.     a.Insert(1, 5);
  287.     a.Insert(2, 6);
  288.     a.Insert(3, 9);
  289.     cout << "a: ";
  290.     a.Print();
  291.     cout << endl;
  292.     b.Insert(0, 4);
  293.     b.Insert(1, 5);
  294.     b.Insert(2, 6);
  295.     b.Insert(3, 7);
  296.     cout << "b: ";
  297.     b.Print();
  298.     //a = b;
  299.     cout << endl;
  300.     cout << (a < b) << endl;
  301.     cout << (a > b) << endl;
  302.     cout << (a <= b) << endl;
  303.     cout << (a >= b) << endl;
  304.     cout << (a == b) << endl;
  305.     cout << (a != b) << endl;
  306.     return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement