Ejemplo n.º 1
0
        void Button1Click(object sender, EventArgs e)
        {
            /*
            textBox1.Text = Testing.Generate(10);
            textBox2.Text = Testing.Generate(2);
            textBox3.Text = Testing.Generate(2);
            */

            this.ouput.Clear();
            try {
                List sl1 = null;
                List sl2 = null;
                List sl3 = null;

                //sl1 = listView1.GetList();
                //sl2 = listView2.GetList();
                //sl3 = listView3.GetList();

                //if (sl1.isEmpty && sl2.isEmpty && sl3.isEmpty) {

                    if (textBox1.Text.Length!=0 && textBox2.Text.Length!=0 && textBox3.Text.Length!=0){
                        sl1 = new List();
                        sl1.Create(ConvertRoutine.ArraysStrToInt(textBox1.Text.Trim().Split()));
                        sl2 = new List();
                        sl2.Create(ConvertRoutine.ArraysStrToInt(textBox2.Text.Trim().Split()));
                        sl3 = new List();
                        sl3.Create(ConvertRoutine.ArraysStrToInt(textBox3.Text.Trim().Split()));
                    }

                //}
                /*
                else {

                    sl1 = new SingleLinkedList("1 1 1 1 2 1 1 2 3");
                    sl2 = new SingleLinkedList("1 1 2");
                    sl3 = new SingleLinkedList("0");
                }
                */

            SuspendLayout();
            //ouput.Text += sl1.ToString();

            Node tn = sl1.Head.Next;
            Debug.WriteLine(tn+"");
            List clone = (List)sl3.Clone(true);
            Debug.WriteLine(clone+"");

            sl1 = ListManager.Replace(sl1,sl2,sl3);

            //ouput.Text += "\r\n";
            ouput.Text += sl1.ToString();

            //rl.SetList(sl1);

            ResumeLayout();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message+ ex.StackTrace);
            }
        }
Ejemplo n.º 2
0
        //public bool isHead{
        //}
        public object Clone(List list)
        {
            Node p = this.Next;

            Node first;
            Node last;
            Node prev;
            Node c;

            first = new Node(this.Info,null,null);

            last = first.Next;
            //prelast = first;

            prev = first;
            c=null;

            while (p!=list.Head){
                c.Prev.Next = c = new Node(p.Info,last,null);
                c.Prev = prev;
                prev=c;
                p = p.Next;
            }

            return first;
        }
        /// <summary>
        /// Заменяет ? все вхождения одного списка в другой третьим списком
        /// 
        /// 1, 2, 3 , 4, 5, 1, 2, 3, 6
        /// 1, 2, 3
        /// 10, 20.
        /// 
        /// 10, 20, 4, 5, 10, 20, 6.
        /// </summary>
        public static List Replace(List L1, List L2, List L3)
        {
            Debug.WriteLine(L1);
            Debug.WriteLine(L2);
            Debug.WriteLine(L3);

            Node F1 = L1.Head.Next;
            Node F2 = L2.Head.Next;
            Node F3 = L3.Head.Next;

            List l1 = L1;
            List l2 = L2;
            List l3 = L3;

            if (!L1.isEmpty && !L2.isEmpty && !L3.isEmpty){

                Node p = F1;
                Node pp = null;
                Node q = null;

                Node prep = F1;
                bool matched = false;

                //FIXME: работает, вроде

                if (F1.Info == F2.Info){
                    #region если вхождение в самом начале

                    Debug.WriteLine("предположительно есть совпадение в начале");

                    p = F1;
                    pp = F2;

                    //CyclicList t = p;

                    matched = false;

                    while (pp!=L2.Head && p!=L1.Head){

                        matched = true;

                        if (p.Info != pp.Info) {
                            matched = false;
                            break;
                        }
                        //else{
                        //matched = true;
                        p=p.Next;
                        pp=pp.Next;
                        //}
                        Debug.WriteLine("!!! p ="+p);
                    }

                    // bp
                    if (matched) {

                        // нашли вхождение
                        Debug.WriteLine("и правда, есть вхождение в начале, меняем");

                        //q = (Node)F3.Clone(L3);
                        List newlist = L3.Clone(true);

                        Debug.WriteLine("клонирован список "+newlist);

                        q = newlist.Head.Next;
                        q.Prev = L1.Head.Prev;
                        //F1 = q;
                        L1.Head.Next = q;

                        while (q.Next!=newlist.Head){
                            q=q.Next;
                        }
                        q.Next = p;
                        p.Prev = q;

                        prep = q;
                        //p = q.Link;
                        //Debug.WriteLine("prep ="+prep.Info+" p="+p.Info);
                    }

                    else {
                        Debug.WriteLine("встретилась "+p+"ложная тревога, переходим в "+F1.Next);
                        prep = F1;
                        p = F1.Next;
                    }

                    #endregion
                }

                #region середина и конец

                while (p!=L1.Head){

                    Debug.WriteLine(p);

                    if (p.Info == F2.Info){
                        Debug.WriteLine("преположительно вхождение");
                        Debug.WriteLine(p+"");
                        // запомнили начало
                        Node s = p;

                        pp = F2;

                        matched = false;

                        while (pp!=L2.Head && p!=L1.Head){

                            //if (){

                                matched = true;
                                if (pp.Info!=p.Info) {
                                    Debug.WriteLine(pp.Info + "!="+p.Info);
                                    matched = false;
                                    break;
                                }

                                matched = true;
                                //else {
                                //	matched = true;
                                pp = pp.Next;
                                p = p.Next;

                                if (pp == L2.Head && matched){
                                    matched = true;
                                    break;
                                }

                                if (p == L1.Head) {
                                    matched = false;
                                    break;
                                }

                        }

                        if (matched) {
                            Debug.WriteLine("и правда, меняем");

                            //q = (CyclicList)F3.Clone();
                            List newlist = L3.Clone(true);

                            q = newlist.Head.Next;
                            q.Prev = s.Prev;

                            prep.Next = q;
                            q = newlist.Head.Next;

                            while (q.Next!=newlist.Head){
                                q=q.Next;
                            }

                            q.Next = p;
                            p.Prev = q;
                            //prep.Link = q;
                            prep = q;
                            //p = p.Link;

                            Debug.WriteLine("prep = "+prep + " p = "+p);

                        }

                        else {

                            prep = s;
                            p = s.Next;
                        }
                    }

                    else {
                        prep = p;
                        p = p.Next;

                    }

                }

                #endregion

            }
            Debug.WriteLine("возвращается список "+L1);
            return L1;
        }
Ejemplo n.º 4
0
        public List Clone(bool attemptDeepCopy)
        {
            List listClone = new List();

            if (attemptDeepCopy)
            {

                Node prev = listClone.Head; // new Head
                Node c = Head.Next;
                Node q = null;
                while (c!=Head){
                    q = new Node(c.Info,null,null);
                    q.Prev = prev;
                    prev.Next = q;
                    c = c.Next;
                    prev = prev.Next;
                }

                q.Next = listClone.Head;

            }
            else
                listClone = (List)this.Clone();

            return listClone;
        }
Ejemplo n.º 5
0
        public virtual object Clone()
        {
            List listClone = new List();

            for (Node node = Head.Next; node != Head; node = node.Next)
                listClone.Add(node);

            return listClone;
        }