Advertisement
AmirVagapov

List + Set (broken)

Jun 3rd, 2023
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 24.14 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. class HelloWorld {
  5.     static void Main() {
  6.         Console.WriteLine("test list");
  7.         test_list();
  8.         Console.WriteLine("test set");
  9.         test_set();
  10.     }
  11.     static void test_list() { /********** TEST LIST **********/
  12.         //1
  13.         Console.WriteLine("1 test");
  14.         Console.WriteLine("Создайте пустой список S1. Добавьте элемент 1 в голову, добавьте элемент 10 в хвост. Выведите S1 на экран (используя функцию Print)");
  15.         List<int> s1 = new List<int>();
  16.         s1.push_front(1);
  17.         s1.push(10);
  18.         Console.Write("s1 = ");
  19.         s1.Print();
  20.         //2
  21.         Console.WriteLine("2 test");
  22.         Console.WriteLine("Создайте список S2 из 6 случайных чисел. Выведите список S2 на экран, используя потоковый вывод. Найдите в S2 max и min элемент. Отсортируйте S2.");
  23.         List<int> s2 = new List<int>();
  24.         Random rnd = new Random();
  25.         for(int i = 0; i < 6; i++) {
  26.             s2.push(rnd.Next() % 15);
  27.         }
  28.         Console.Write("s2 = ");
  29.         s2.Print();
  30.         // a[1] = 3;
  31.         Console.WriteLine("min: "+ s2.min().getData()+ " max: "+ s2.max().getData());
  32.         s2 = s2.mergesort();
  33.         Console.Write("s2 = ");
  34.         s2.Print();
  35.         //3
  36.         Console.WriteLine("3 test");
  37.         Console.WriteLine("Найдите в S2 2-й элемент и выведите его на экран. Удалите 2-й элемент списка S2.");
  38.         Console.WriteLine(s2[1].getData());
  39.         s2.delete(1);
  40.         Console.Write("s2 = ");
  41.         s2.Print();
  42.         //4
  43.         Console.WriteLine("4 test");
  44.         Console.WriteLine("Найдите в S2 6-й элемент (т.е. убедитесь, что в списке осталось меньше 6 элементов). Удалите элемент из хвоста S2.");
  45.         Console.Write("s2[5] = ");
  46.         Console.WriteLine(s2[5]);
  47.         s2.pop();
  48.         Console.Write("s2 = ");
  49.         s2.Print();
  50.         //5
  51.         Console.WriteLine("5 test");
  52.         Console.WriteLine("Создайте пустой список S3. Инициализируйте его значением S1. Проверьте равенство списков S1 и S3. Проверьте, есть ли в S3 элемент 15.");
  53.         List<int> s3 = s1.clone();
  54.         Console.Write("s3 = ");
  55.         s3.Print();
  56.         Console.Write("s3 == s1 ");
  57.         Console.WriteLine(s3 == s1);
  58.         Console.WriteLine("s3.find(15) == null ");
  59.         Console.WriteLine(s3.find(15) == null);
  60.         //6
  61.         Console.WriteLine("6 test");
  62.         Console.WriteLine("Удалите элемент из головы списка S3. Удалите из списка S3 элемент 10. Выведите S3 на экран. Проверьте S3 на пустоту.");
  63.         s3.pop_front();
  64.         s3.delete_value(10);
  65.         Console.Write("s3 = ");
  66.         s3.Print();
  67.         Console.Write("s3.IsNull() ");
  68.         Console.WriteLine(s3.IsNull());
  69.         // 7
  70.         Console.WriteLine("7 test");
  71.         Console.WriteLine("Создайте последовательность из 6 случайных чисел, меньших 20. Превратите ее в список S4 и выведите его на экран. Проверьте, есть ли в S4 элемент 25. Добавьте элемент 25 четвертым в список.");
  72.         List<int> s4 = new List<int>();
  73.         for(int i = 0; i < 6; i++) {
  74.             s4.push(rnd.Next() % 20);
  75.         }
  76.         Console.Write("s4 = ");
  77.         s4.Print();
  78.         Console.Write("s4.find(25) == null ");
  79.         Console.WriteLine(s4.find(25) == null);
  80.         s4.insert(25, 3);
  81.         Console.Write("s4 = ");
  82.         s4.Print();
  83.         //8
  84.         Console.WriteLine("8 test");
  85.         Console.WriteLine("Создайте список S5, проинициализировав его при создании списком S2. Выведите S5 на экран. Проверьте, есть ли в S5 элемент 4 и если есть – удалите его, если нет – добавьте элемент 4 в хвост.");
  86.         List<int> s5 = s2.clone();
  87.         Console.Write("s5 = ");
  88.         s5.Print();
  89.         Console.Write("s5.find(4) == null ");
  90.         Console.WriteLine(s5.find(4) == null);
  91.         if (s5.find(4) != null) {
  92.             s5.delete_value(4);
  93.         }
  94.         else {
  95.             s5.push(4);
  96.         }
  97.         Console.Write("s5 = ");
  98.         s5.Print();
  99.         //9
  100.         Console.WriteLine("9 test");
  101.         Console.WriteLine("Измените S5, записав в него 4 числа: 11, 12, 13, 14 (числа вводятся с клавиатуры). Сравните S5 и S4.");
  102.         s5.read_input();
  103.         Console.Write("s5 = ");
  104.         s5.Print();
  105.         Console.Write("s5 == s4 ");
  106.         Console.WriteLine(s5 == s4);
  107.         //10
  108.         Console.WriteLine("10 test");
  109.         Console.WriteLine("Добавьте в хвост S5 список S4. Добавьте в голову S5 список S1.");
  110.         Console.Write("s5 = ");
  111.         s5.Print();
  112.         s5 = s5 + s4;
  113.         Console.Write("(s5 + s4) s5 = ");
  114.         s5.Print();
  115.         s5 = s1 + s5;
  116.         Console.Write("(s1 + s5) s5 = ");
  117.         s5.Print();
  118.     }
  119.     static void test_set() { /********** TEST SET **********/
  120.         //1
  121.         Console.WriteLine("1 test");
  122.         Console.WriteLine("Создайте множество S1, из 10 случайных чисел. Выведите S1 на экран");
  123.         Set<int> s1 = new Set<int>();
  124.         Random rnd = new Random();
  125.         int j = 0;
  126.         while(j < 10) {
  127.             int val = rnd.Next(20);
  128.             if (s1.find(val) == null) {
  129.                 s1.push(val);
  130.                 j += 1;
  131.             }
  132.         }
  133.         Console.Write("s1 = ");
  134.         s1.Print();
  135.         //2
  136.         Console.WriteLine("2 test");
  137.         Console.WriteLine("Создайте множество S2 и инициализируйте его (при создании) значением S1.  Выведите S2 на экран (используйте потоковый вывод). Проверьте равенство множеств S1  и  S2.");
  138.         Set<int> s2 = new Set<int>(s1.clone());
  139.         Console.Write("s2 = ");
  140.         s2.Print();
  141.         Console.Write("s1 == s2 ");
  142.         Console.WriteLine(s1 == s2);
  143.         //3
  144.         Console.WriteLine("3 test");
  145.         Console.WriteLine("Проверьте, есть ли в S1 элемент 5. Создайте множество S3, которое получается  удалением/добавлением из S1 элемента 5. Проверьте, что S1 и S3 – не равны.");
  146.         Console.Write("s1.find(5) != null ");
  147.         Console.WriteLine(s1.find(5) != null);
  148.         Set<int> s3 = new Set<int>(s1.clone());
  149.         if (s3.find(5) != null) {
  150.             s3.delete_value(5);
  151.         }
  152.         else {
  153.             s3.push(5);
  154.         }
  155.         Console.Write("s3 = ");
  156.         s3.Print();
  157.         Console.Write("s1 == s3 ");
  158.         Console.WriteLine(s1 == s3);
  159.         //4
  160.         Console.WriteLine("4 test");
  161.         Console.WriteLine("Создайте пустое множество S4. Проверьте его на пустоту.  Добавьте в S4 последовательно числа 5, 10, 15, 5.  Выведите S4 на экран.");
  162.         Set<int> s4 = new Set<int>();
  163.         Console.Write("s4.IsNull() ");
  164.         Console.WriteLine(s4.IsNull());
  165.         s4.push(5);
  166.         s4.push(10);
  167.         s4.push(15);
  168.         s4.push(5);
  169.         Console.Write("s4 = ");
  170.         s4.Print();
  171.         //5
  172.         Console.WriteLine("5 test");
  173.         Console.WriteLine("Создайте пустое множество S5.  Инициализируйте его множеством S4.  Проверьте, что во множестве S5 есть элемент 15 и удалите его. Выведите получившееся множество на экран.");
  174.         Set<int> s5 = new Set<int>(s4);
  175.         Console.Write("s5 = ");
  176.         s4.Print();
  177.         Console.Write("s5.find(15) != null ");
  178.         Console.WriteLine(s5.find(15) != null);
  179.         s5.delete_value(15);
  180.         Console.WriteLine("15 deleted");
  181.         Console.Write("s5 = ");
  182.         s5.Print();
  183.         //6
  184.         Console.WriteLine("6 test");
  185.         Console.WriteLine("Создайте список T, из 20 случайных чисел. Выведите T на экран. Создайте из T множество S6.  Выведите S6 на экран. Определите количество элементов в S6.");
  186.         List<int> t = new List<int>();
  187.         for(int i = 0; i < 20; i++) {
  188.             t.push(rnd.Next() % 30);
  189.         }
  190.         Console.Write("t = ");
  191.         t.Print();
  192.         Set<int> s6 = new Set<int>(t);
  193.         Console.Write("s6 = ");
  194.         s6.Print();
  195.         Console.Write("s6.len() = ");
  196.         Console.WriteLine(s6.len());
  197.         //7
  198.         Console.WriteLine("7 test");
  199.         Console.WriteLine("Найдите S7 – дополнение S6 до универсального. Найдите множество S8=S7∩S6.");
  200.         Console.WriteLine("Будем считать, что множество состоит из 30 эелементов");
  201.         Set<int> s7 = new Set<int>();
  202.         for(int i = 0; i < 30; i++) {
  203.             if (s6.find(i) == null) s7.push(i);
  204.         }
  205.         Console.Write("s7 = ");
  206.         s7.Print();
  207.         Set<int> s8 = Set<int>.intersection(s7, s6);
  208.         Console.Write("s8 = ");
  209.         s8.Print();
  210.         //8
  211.         Console.WriteLine("8 test");
  212.         Console.WriteLine("Создайте множество \nS9={1,3,5,7,9,11,13,15,17,19,21,23,25,27,29}\n(используйте потоковый ввод).  Найдите V1 =S7 ∩ S9,  V2 = S7 ∪ S9,  V3 = S7 \\ S9.");
  213.         Set<int> s9 = new Set<int>();
  214.         s9.read_input();
  215.         Console.Write("s9 = ");
  216.         s9.Print();
  217.         Set<int> v1 = Set<int>.intersection(s7, s9);
  218.         Console.Write("v1 = ");
  219.         v1.Print();
  220.         Set<int> v2 = Set<int>.union(s7, s9);
  221.         Console.Write("v2 = ");
  222.         v2.Print();
  223.         Set<int> v3 = Set<int>.difference(s7, s9);
  224.         Console.Write("v3 = ");
  225.         v3.Print();
  226.         //9
  227.         Console.WriteLine("9 test");
  228.         Console.WriteLine("Измените V1, объединив его с V3. Сравните V1  с S7.");
  229.         v1 = Set<int>.union(v1, v3);
  230.         Console.Write("v1 = ");
  231.         v1.Print();
  232.         Console.Write("v1 == v7 ");
  233.         Console.WriteLine(v1 == s7);
  234.         //10
  235.         Console.WriteLine("10 test");
  236.         Console.WriteLine("Измените множество V2, заменив его разностью V2 и V3. Сравните V2  с  S9");
  237.         v2 = Set<int>.difference(v2, v3);
  238.         Console.Write("v2 = ");
  239.         v2.Print();
  240.         Console.Write("v2 == s9 ");
  241.         Console.WriteLine(v2 == s9);
  242.     }
  243. }
  244. class Node {
  245.     protected int data;
  246.     private Node next;
  247.     public void nextSet(Node a) {
  248.         next = a;
  249.     }
  250.     public Node nextGet() {
  251.         return next;
  252.     }
  253.     public Node(int d) {
  254.         data = d;
  255.         nextSet(null);
  256.     }
  257.     public Node clone()
  258.     {
  259.         return new Node(data);
  260.     }
  261.     public Node setData(int newdata) {
  262.         data = newdata;
  263.     }
  264.     public Node getData() {
  265.         return data;
  266.     }
  267.    
  268. }
  269.  
  270. class List {
  271.     protected Node head;
  272.  
  273.     // Конструкторы
  274.     public List(int d) {
  275.         head = new Node(d);
  276.     }
  277.     public List(Node d) {
  278.         head = d;
  279.     }
  280.     public List() {
  281.         head = null;
  282.     }
  283.  
  284.     public List clone() {
  285.         if (head == null) return new List();
  286.         List cl = new List(new Node(head.getData()));
  287.         Node curr = head;
  288.         Node curr2 = cl.head;
  289.         while(curr.nextGet() != null) {
  290.             curr = curr.nextGet();
  291.             curr2.nextSet(new Node(curr.getData()));
  292.             curr2 = curr2.nextGet();
  293.         }
  294.         return cl;
  295.     }
  296.     public void push(int d) {
  297.         Node temp = new Node(d);
  298.         if (IsNull()) {
  299.             head = temp;
  300.             return;
  301.         }
  302.         Node tail = head;
  303.         while(tail.nextGet()!= null) {
  304.             tail = tail.nextGet();
  305.         }
  306.         tail.nextSet(temp);
  307.     }
  308.     public void push_front(int d) {
  309.         Node temp = new Node(d);
  310.         if (IsNull()) {
  311.             head = temp;
  312.             return;
  313.         }
  314.         temp.nextSet(head);
  315.         head = temp;
  316.     }
  317.     public Node pop() {
  318.         if (head == null) {return null;}
  319.         Node tail = head.nextGet();
  320.         if (head.nextGet()== null) {
  321.             head = null;
  322.             return tail;
  323.         }
  324.         Node prev = head;
  325.         while(tail.nextGet()!= null) {
  326.             tail = tail.nextGet();
  327.             prev = prev.nextGet();
  328.         }
  329.         Node res = tail;
  330.         tail = prev;
  331.         tail.nextSet(null);
  332.         return res;
  333.     }
  334.     public Node pop_front() {
  335.         if (IsNull()) {return null;}
  336.         Node res = head;
  337.         if (head.nextGet()== null) {
  338.             Node temp = new Node(head.getData());
  339.             head = null;
  340.             return temp;
  341.         }
  342.         else {
  343.             head = head.nextGet();
  344.         }
  345.         return res;
  346.     }
  347.     public Node insert(int a, int n) {
  348.         Node ins = new Node(a);
  349.         Node cur = head;
  350.         if (cur == null) {
  351.             if (n == 0) {
  352.                 head = ins;
  353.                 return ins;
  354.             }
  355.             else return null;
  356.         }
  357.         for(int i = 1; i < n; i++) {
  358.             if (cur.nextGet()== null) return null;
  359.             cur = cur.nextGet();
  360.         }
  361.         ins.nextSet(cur.nextGet());
  362.         cur.nextSet(ins);
  363.         return ins;
  364.     }
  365.     public Node insert_after_key(int a, int key) {
  366.         Node ins = new Node(a);
  367.         Node cur = find(key);
  368.         if (cur == null) return null;
  369.         ins.nextSet(cur.nextGet());
  370.         cur.nextSet(ins);
  371.         return ins;
  372.     }
  373.     public Node this[int n] {
  374.         get {
  375.             Node temp = head;
  376.             for(int i = 0; i < n; i++) {
  377.                 if(temp == null) {return null;}
  378.                 temp = temp.nextGet();
  379.             }
  380.             return temp;
  381.         }
  382.         set {
  383.             if (head == null && n == 0) {head = value; return;}
  384.             Node temp = head;
  385.             for(int i = 0; i < n; i++) {
  386.                 if(temp == null) {throw new NullReferenceException("List size is smaller than n");}
  387.                 temp = temp.nextGet();
  388.             }
  389.             temp.getData() = value.getData();
  390.         }
  391.     }
  392.     public Node delete(int n) {
  393.         if (head == null) return null;
  394.         Node temp = head;
  395.         Node prev = head;
  396.         for(int i = 0; i < n; i++) {
  397.             if (temp.nextGet() == null) return null;
  398.             prev = temp;
  399.             temp = temp.nextGet();
  400.         }
  401.         prev.nextSet(temp.nextGet());
  402.         return temp;
  403.     }
  404.     public Node delete_value(int value) {
  405.         if (head == null) return null;
  406.         Node temp = head;
  407.         Node prev = null;
  408.         do {
  409.             if (temp.getData().CompareTo(value) == 0) {
  410.                 if (prev == null) head = temp.nextGet();
  411.                 else prev.nextSet(temp.nextGet());
  412.                 return temp;
  413.             }
  414.             prev = temp;
  415.             temp = temp.nextGet();
  416.         } while(temp != null);
  417.         return null;
  418.     }
  419.     public void delete_values(int value) {
  420.         if (head == null) return;
  421.         Node temp = head;
  422.         Node prev = null;
  423.         while (temp != null && temp.getData().CompareTo(value) == 0) {
  424.             head = temp.nextGet();
  425.             temp = head;
  426.         }
  427.         while (temp != null) {
  428.             while (temp != null && temp.getData().CompareTo(value) != 0) {
  429.                 prev = temp;
  430.                 temp = temp.nextGet();
  431.             }
  432.             if (temp == null)
  433.                 return;
  434.             prev.nextSet(temp.nextGet());
  435.             temp = prev.nextGet();
  436.         }
  437.     }
  438.  
  439.     public Node find(int value) {
  440.         if (IsNull()) {return null;}
  441.         Node temp = head;
  442.         while (temp != null) {
  443.             if (temp.getData().CompareTo(value) == 0) {return temp;} //temp.data == value
  444.             temp = temp.nextGet();
  445.         }
  446.         return temp;
  447.     }
  448.     public Node min() {
  449.         if (IsNull()) {return null;}
  450.         Node temp = head;
  451.         Node min = head;
  452.         while(temp != null) {
  453.             if (min.detData().CompareTo(temp.getData()) > 0) {min = temp;}
  454.             temp = temp.nextGet();
  455.         }
  456.         return min;
  457.     }
  458.     public Node max() {
  459.         if (IsNull()) {return null;}
  460.         Node temp = head;
  461.         Node max = head;
  462.         while(temp != null) {
  463.             if (max.getData().CompareTo(temp.getData()) < 0) {max = temp;}
  464.             temp = temp.nextGet();
  465.         }
  466.         return max;
  467.     }
  468.     public bool IsNull() {
  469.         return head == null;
  470.     }
  471.     public void clear() {
  472.         head = null;
  473.     }
  474.     public void read_input() {
  475.         head = null;
  476.         Console.Write("Enter number of nodes : ");
  477.         int n = (int)Convert.ChangeType(Console.ReadLine(), typeof(int));
  478.         Console.WriteLine(n);
  479.         for (int i = 0; i < n; i++) {
  480.             Node temp = (int)Convert.ChangeType(Console.ReadLine(), typeof(int));
  481.             push(temp);
  482.             Console.WriteLine("pushed " + temp);
  483.         }
  484.     }
  485.     Node getmiddle() {
  486.         if (head == null) return head;
  487.         Node slow = head, fast = head;
  488.         while(fast.nextGet()!= null && fast.nextGet().nextGet()!= null) {
  489.             slow = slow.nextGet();
  490.             fast = fast.nextGet().nextGet();
  491.         }
  492.         return slow;
  493.     }
  494.     static List merge(List left, List right) {
  495.         if (left.IsNull()) return right;
  496.         if (right.IsNull()) return left;
  497.         List result = new List();
  498.         if (left.head.getData().CompareTo(right.head.getData()) <= 0) {
  499.             result = left;
  500.             result.head.nextSet(merge(new List(left.head.nextGet()), right).head);
  501.         }
  502.         else {
  503.             result = right;
  504.             result.head.nextSet(merge(left, new List(right.head.nextGet())).head);
  505.         }
  506.         return result;
  507.     }
  508.     public List mergesort() {
  509.         if (head == null || head.nextGet()== null) return (new List(head));
  510.         Node middle = getmiddle();
  511.         List middle_next = new List(middle.nextGet());
  512.         middle.nextSet(null);
  513.         List left = mergesort();
  514.         List right = middle_next.mergesort();
  515.         // Console.WriteLine("debug");
  516.         // left.Print();
  517.         // right.Print();
  518.         List result = merge(left, right);
  519.         // result.Print();
  520.         head = result.head;
  521.         return result;
  522.     }
  523.     public void Print() {
  524.         if (IsNull()) {
  525.             Console.Write("null\n");
  526.             return;
  527.         }
  528.         Node temp = head;
  529.         Console.Write("[" + temp.getData());
  530.         temp = temp.nextGet();
  531.         while(temp != null) {
  532.             Console.Write(", " + temp.getData());
  533.             temp = temp.nextGet();
  534.         }
  535.         Console.Write("]\n");
  536.     }
  537.  
  538.     public static List operator + (List a, List b) {
  539.         if (a.head == null) return b;
  540.         if (b.head == null) return a;
  541.         List result = a;
  542.         Node temp = result.head;
  543.         while(temp.nextGet()!= null) {
  544.             temp = temp.nextGet();
  545.         }
  546.         temp.nextSet(b.head);
  547.         return result;
  548.     }
  549.     public static bool operator == (List a, List b) {
  550.         Node f = a.head;
  551.         Node s = b.head;
  552.         if (f == null ^ s == null) return false;
  553.         if (f == null && s == null) return true;
  554.         while(f.nextGet() != null && s.nextGet() != null) {
  555.             if (f.getData().CompareTo(s.getData()) != 0) return false;
  556.             f = f.nextGet();
  557.             s = s.nextGet();
  558.         }
  559.         if (f.nextGet() == null ^ s.nextGet() == null) return false;
  560.         return true;
  561.     }
  562.  
  563.     public static bool operator != (List a, List b) {
  564.         return !(a==b);
  565.     }
  566. }
  567.  
  568. class Set : List {
  569.     public Set() {
  570.     }
  571.     public Set(Node a) {
  572.         head = a.clone();
  573.     }
  574.     public Set(int a) {
  575.         head = new Node(a);
  576.     }
  577.     public Set(List a) {
  578.         head = a.clone()[0];
  579.         delete_repetitions();
  580.     }
  581.    
  582.     void delete_repetitions() {
  583.         Node ptr1 = head;
  584.         Node ptr2 = null;
  585.         while (ptr1 != null && ptr1.nextGet() != null) {
  586.             ptr2 = ptr1;
  587.             while (ptr2.nextGet() != null) {
  588.                 if (ptr1.getData().CompareTo(ptr2.nextGet().getData()) == 0) ptr2.nextSet(ptr2.nextGet().nextGet());
  589.                 else ptr2 = ptr2.nextGet();
  590.             }
  591.             ptr1 = ptr1.nextGet();
  592.         }
  593.     }
  594.     public new void push(int d) {
  595.         if (base.find(d) == null)
  596.             base.push(d);
  597.     }
  598.     public new void push_front(int d) {
  599.         if (base.find(d) == null)
  600.             base.push_front(d);
  601.     }
  602.     public new Node insert(int a, int n) {
  603.         if (base.find(a) == null)
  604.             return base.insert(a, n);
  605.         return null;
  606.     }
  607.     public new Node insert_after_key(int a, int key) {
  608.         if (base.find(a) == null)
  609.             return base.insert_after_key(a, key);
  610.         return null;
  611.     }
  612.     public new void read_input() {
  613.         base.clear();
  614.         Console.Write("Enter number of nodes : ");
  615.         int n = (int)Convert.ChangeType(Console.ReadLine(), typeof(int));
  616.         Console.WriteLine(n);
  617.         for (int i = 0; i < n; i++) {
  618.             Node temp = (int)Convert.ChangeType(Console.ReadLine(), typeof(int));
  619.             push(temp);
  620.             Console.WriteLine("pushed " + temp);
  621.         }
  622.     }
  623.     public static Set union(Set a, Set b) {
  624.         if (a.IsNull()) return (Set)b.clone();
  625.         if (b.IsNull()) return (Set)a.clone();
  626.         Set res = new Set(a.clone());
  627.         foreach(Node item in b) {
  628.             res.push(item.clone().getData());
  629.         }
  630.         return res;
  631.     }
  632.     public static Set intersection(Set a, Set b) {
  633.         if (a.IsNull() && b.IsNull()) return null;
  634.         Set res = new Set();
  635.         foreach(Node item in b) {
  636.             if (a.find(item.getData()) != null) res.push(item.getData());
  637.         }
  638.         foreach(Node item in a) {
  639.             if (b.find(item.getData()) != null) res.push(item.getData());
  640.         }
  641.         return res;
  642.     }
  643.     public static Set difference(Set a, Set b) {
  644.         if (a.IsNull()) return null;
  645.         if (b.IsNull()) return new Set(a.clone());
  646.         Set res = new Set(a.clone());
  647.         foreach(Node item in b) {
  648.             res.delete_value(item.getData());
  649.         }
  650.         return res;
  651.     }
  652.     public new Node this[int n] {
  653.         get {return base[n];}
  654.         set {
  655.             if (find(value.getData()) != null)
  656.                 base[n] = value;
  657.         }
  658.     }
  659.     public int len() {
  660.         int i = 0;
  661.         foreach(Node item in this) i++;
  662.         return i;
  663.     }
  664.     // public static bool operator == (Set<T> a, Set<T> b) {
  665.     //     if (a.head == null ^ b.head == null) return false;
  666.     //     if (a.head == null && b.head == null) return true;
  667.     //     Set<T> temp = new Set<T>(b);
  668.     //     foreach(Node<T> item in a) {
  669.     //         if (temp.delete_value(item.data) == null) return false;
  670.     //     }
  671.     //     return temp.IsNull();
  672.     // }
  673.     public new void Print() {
  674.         this.mergesort();
  675.         base.Print();
  676.     }
  677.     public static bool operator == (Set a, Set b) {
  678.         a.mergesort();
  679.         b.mergesort();
  680.         return ((List)a == (List)b);
  681.     }
  682.     public static bool operator != (Set a, Set b) {
  683.         return !(a == b);
  684.     }
  685. }
  686.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement