5 Replies - 650 Views - Last Post: 16 January 2013 - 11:02 AM Rate Topic: -----

#1 DamianMRLY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-December 12

Doubly linked list C#

Posted 15 January 2013 - 10:51 AM

Hi,

I want to sort 2 listboxes with DLL i have came to this part but i have no idea how to make the second node, and how to implement it into the buttonsort. I have searched for hours and hours for a solution but i can't find it.

Maybe you guys can help me on my way.

Many thanxs,
Damion


namespace DLL
{
    class cS
    {
        private static cS v, m;

        private  cS L;
        private int w;
        private cS R;

        private static cS[] car;

        //constructor
        private cS(cS pL,int v, cS pR)
        {
            L = pL;
            w = v;
            R = pR;
        }

        public static int[] mDLL(int[] iar)
        {
            //make first node
            car[0] = new cS(null, iar[0], null);
            v = car[0]; m = v;
            car[1] = new cS(null, iar[0], null);
            if (iar[1] <= iar[0])
            {
                //before
                car[0].L = car[1];
                car[1].R = car[0];
                v = car[1];
            }
            else 
            {
                //back
                car[0].R = car[1];
                car[1].L = car[0];
            }

            for (int i = 2; i < n; i++)
            {
                car[i] = new cS(null, iar[i], null);
                m=v;
                while (car[i].w > m.w)
                {
                    if (m.R != null)
                    {
                        m = m.R;
                    }
                    else
                    {
 
                    }
                }

            }
            return iar;
        }      

    }
}

///FORM

        private void buttonsort_Click(object sender, EventArgs e)
        {
            int n=10;
            int[] iar=new int[n];
            Random r = new Random();
            for (int i =0 ; i<n; i++)
            {
                iar[i] = r.Next(0, 20);
                lbU.Items.Add(iar[i]);
            }
            iar = cS.mDLL(iar);
            for (int i = 0; i < n; i++)
            {
                lbS.Items.Add(iar[i]);
            }




Is This A Good Question/Topic? 0
  • +

Replies To: Doubly linked list C#

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Doubly linked list C#

Posted 15 January 2013 - 04:57 PM

You should use meaningful names for you variables. You will look at the code next month and you will not understand it.
Was This Post Helpful? 0
  • +
  • -

#3 DamianMRLY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-December 12

Re: Doubly linked list C#

Posted 16 January 2013 - 06:22 AM

I have updated my code and it wont sort the last 2 numbers and it sometimes random crashes the application.


//Button sort 

        private void button1_Click(object sender, EventArgs e)
        {
            lbS.Items.Clear();
            lbU.Items.Clear();
            // n = lengte van array
            int n = 10;
            int[] iar = new int[n];
            Random r = new Random();
            for (int i = 0; i < n; i++)
            {
                iar[i] = r.Next(0, 20);
                lbU.Items.Add(iar[i]);
            }
            iar = cDLL.mDLL(iar);
            for (int i = 0; i < n; i++)
            {
                lbS.Items.Add(iar[i]);
            }

//class cDLL

namespace Kraan_Luuk_DLL
{
    class cDLL
    {
        // v = voor (before)
        // o = opslag (temp)
        //nAR = node array
        // L = linker (left)
        // M = midden (middle)
        // R = rechter (right)

        private static cDLL v, o;
        private static cDLL[] nAR;
        private cDLL L;
        private int M;
        private cDLL R;

        //constructor
        private cDLL(cDLL pL, int v, cDLL pR)

        // pL pointer left
        // M = meeloper (v) (walker)
        // pR pointer right
        {
            
            L = pL;
            M = v;
            R = pR;
        }
        public static int[] mDLL(int[] iar)
        {
            //iar = int array
            //n = length of int array iar          
            int n = iar.Length;

            nAR = new cDLL[n];
            //node array [0] = new cdll null <- [0] ->null
            nAR[0] = new cDLL(null, iar[0], null);

            v = nAR[0]; o = v;
            nAR[1] = new cDLL(null, iar[0], null);
            if (iar[1] >= iar[0])
            {
                //infront
                nAR[0].R = nAR[1];
                nAR[1].L = nAR[0];
            }
            else
            {
                //behind
                nAR[0].L = nAR[1];
                nAR[1].R = nAR[0];
                v = nAR[1];
            }
            for (int i = 2; i < n; i++)
            {
                nAR[i] = new cDLL(null, iar[i], null);
                o = v;
                while (nAR[i].M > o.M)
                {
                    if (o.R != null)
                    {
                        o = o.R;
                    }
                    else
                    {
                        nAR[i].L = o;
                        o.R = nAR[i];
                        break;
                    }
                }

                if (v == o)
                {
                    o.L = nAR[i];
                    nAR[i].R = o;
                    v = nAR[i];
                }
                else
                {
                    o.L.R = nAR[i];
                    nAR[i].L = o.L;
                    o.L = nAR[i];
                    nAR[i].R = o;
                }

            }

            for (int i = 0; i < iar.Length; i++)
            {
                iar[i] = v.M;
                v = v.R;
            }
            return iar;
        }
    }
}




Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Doubly linked list C#

Posted 16 January 2013 - 10:46 AM

If you are creating a linked list why are you placing the nodes into an array? Your code is confusing and I debug other peoples code for a living. I think you need to separate out your logic into different methods, you are trying to do too much in one method.

Is this some form of a class assignment? Is there a requirement to use a linked list? Tell us what, exactly, is the goal of the code. If it is to sort the items in a ListBox, just set the Sorted property to 'true'.


Note: You've been programming too long when you want to end a sentence with ;

This post has been edited by Momerath: 16 January 2013 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 DamianMRLY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-December 12

Re: Doubly linked list C#

Posted 16 January 2013 - 10:53 AM

Hi Momerath,

Thanxs for answering.

My basic goal is to sort 10 random numbers in an listbox trough DLL.

So you suggest i should better break it up in to mutiple methods?
Was This Post Helpful? 0
  • +
  • -

#6 DamianMRLY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-December 12

Re: Doubly linked list C#

Posted 16 January 2013 - 11:02 AM

For my project i need to make an zipper (huffman algorithm) i tought this was a nice practise before i start but i have been struggeling for hours with this one.

So set Sorted = true wont help me;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1