C# Sortowanie listy jednokierunkowej

0

Witam , mam do napisania program sortujący listę jednokierunkową pozbierałem materiały z wykładów i udało mi sie stworzyć coś takiego może mi ktoś pomóc to dokończyć ?

using System;

namespace lista
{
    public class ElementListy

    {
   private ElementListy NowyElement;
    
    public double Wartość;
     public ElementListy Następny;
      public ElementListy(double x)
      {
       Wartość = x;
        Następny = null;
        }
         }

          private ElementListy Korzeń;
           int i;
            for(i = 0; i < Tablica.Lenght; i++)
             {
              if(i == 0)
               Korzeń = new ElementListy(Tablica[0]);
               }
                    public ElementListy Nowy;
                     if(x < Korzeń.Wartość)
                      {
                        Nowy = new ElementListy(x);
                         Następny = Korzeń;
                          Korzeń = Nowy;
                            Nowy = null;
                              }
                                  public class ElementListy
                                   {
                                     public double Wartość;
                                       public ElementListy Następny;
                                         public ElementListy(double x)
                                           {
                                           Wartość = x;
                                              Następny = null;
                                               }
                           public void DopiszDoListy(double x)
                          if(this.Wartość < x)
                         {
                        if(this.Następny == null)Następny = new ElementListy(x);
                       else
                      {
                     if(Następny.Wartość < x) Następny.DopiszDoListy(x);
                    else
                   {
                  NowyElement = new ElementListy(x);
                NowyElement.Następny = this.Następny;
               Następny = NowyElement;
             NowyElement = null;
   } //end if Następny.Wartość < x
  }//end if this.Następny == null)
 }//end if this.Wartość < x
     if(this.Wartość == x)
    {
   NowyElement = new ElementListy(x);
  NowyElement.Następny = this.Następny;
 this.Następny = NowyElement;
NowyElement = null;
}
0

user image

Sformatuj Twój kod po ludzku.

0

Hmmm, nie lepiej użyc gotowej Listy zaimplementowanej w bibliotece. Tak byś miał prostrzą prace :P

// Definiowaniew listy
List<string> list = new List<string>();
// Dodawanie elementow do listy
list.Add("Element listy");

// sortowanie listy
list.Sort();

// drukowanie listy :P
foreach (string value in list){
        Console.WriteLine(value);
}
0

musi być tak jak napisałem pomoże ktoś ?

0

Twój kod wygląda jakbyś go posklejał nie rozumiejąc co robisz, napisz wersję która się kompiluje lub sformatuj po ludzku (wcięcia).

private ElementListy Korzeń;
           int i;
            for(i = 0; i < Tablica.Lenght; i++)
0

nie za bardzo ogarniam ten język dlatego proszę o pomoc

0

Ziomek, Ty masz TYLKO sformatować kod, o to Cię proszą ludzie.

0

Troszkę poprawiłem formatowanie, bo było nie czytelne :P

using System;
 
namespace lista {
    
public class ElementListy {

    private ElementListy NowyElement;

    public double Wartość;
    public ElementListy Następny;

    public ElementListy(double x) {
        Wartość = x;
        Następny = null;
    }
}

private ElementListy Korzeń;

int i;
for(i = 0; i < Tablica.Lenght; i++) {
if(i == 0)
    Korzeń = new ElementListy(Tablica[0]);
}

public ElementListy Nowy;

if(x < Korzeń.Wartość) {
    Nowy = new ElementListy(x);
    Następny = Korzeń;
    Korzeń = Nowy;
    Nowy = null;


}

public class ElementListy {

    public double Wartość;
    public ElementListy Następny;

    public ElementListy(double x) {
        Wartość = x;
        Następny = null;
    }

    public void DopiszDoListy(double x) 
        if(this.Wartość < x) {
        if (this.Następny == null) {
            Następny = new ElementListy(x);
        } else {
            if (Następny.Wartość < x) {
                Następny.DopiszDoListy(x);
            } else {
                NowyElement = new ElementListy(x);
                NowyElement.Następny = this.Następny;
                Następny = NowyElement;
                NowyElement = null;
            } //end if Następny.Wartość < x
        }//end if this.Następny == null)
    }//end if this.Wartość < x

    if(this.Wartość == x ) {
        NowyElement = new ElementListy(x);
        NowyElement.Następny = this.Następny;
        this.Następny = NowyElement;
        NowyElement = null;
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1