Как добавить номер в отсортированный список?C#

Место общения программистов C#
Anonymous
Как добавить номер в отсортированный список?

Сообщение Anonymous »


So, I have an assignment from my school to add a number into a sorted list, but not just a list- IntNode list, which is if I get it right a linked list (maybe I'm wrong I dont know what this thing is called in English). Anyways, I'm very bad at it. So if someone could please look at my code and show me where my mistake is. I would appreciate that. right now, it returns -1, and I'm going insane! so here is the code:
public class IntNode { private int value; private IntNode next; // constructors public IntNode(int value) { this.value = value; this.next = null; } public IntNode(int value, IntNode next) { this.value = value; this.next = next; } //getters public int GetValue() { return this.value; } public IntNode GetNext() { return this.next; } // setters public void SetValue(int value) { this.value = value; } public void SetNext(IntNode next) { this.next = next; } public bool HasNext() { return this.next != null; } public override string ToString() { return this.value + "->"; } } public static IntNode InsertIntoSortedList(IntNode list, int x) { IntNode p = list; int max = p.GetValue(); Console.WriteLine("hila"); if (p == null) { p.SetValue(x); return p; } while(p!=null&& x!=0) { Console.WriteLine("hola"); if (x

Источник: https://stackoverflow.com/questions/780 ... orted-list

Вернуться в «C#»