0

My situation

  • create a list of objects using just classes

This how code work

  • with the class ListRatings i can create a list of Rating (without using list method that c# offer with the library System.Collections.Generic)

(you can see what attribute and method have class ListRatings and class Rating in code bellow)

Issue

when i try to print all rating i added to my list, my program print just first and last!

My code

class ListRating:

public class ListRatings
{
    private Rating first;
    private Rating last;

    public ListRatings()
    {
        first = null;
        last = null;
    }
    public void InsertNewRat(Rating rating)
    {
        if (first == null)
        {
            first = rating;
            last = rating;
        }
        else
        {
            first.setNext(rating);
            last.setNext(rating);
        }
    }
    public void PrintRats()
    {
        Rating e = first;

        Console.WriteLine(e.getMatter());
        Console.WriteLine(e.getDate());
        Console.WriteLine(e.getRate());

        e = e.getNext();

        Console.WriteLine(e.getMatter());
        Console.WriteLine(e.getDate());
        Console.WriteLine(e.getRate());

        e = e.getNext();

        Console.WriteLine(e.getMatter());
        Console.WriteLine(e.getDate());
        Console.WriteLine(e.getRate());

    }
}

class Rating:

public class Rating
{
    private int rate;
    private string matter;
    private DateTime date;
    private Rating next;

    public Rating(int rate, string matter, DateTime date)
    {
        this.rate = rate;
        this.matter = matter;
        this.date = date;
        next = null;
    }

    public int getRate()
    {
        return rate;
    }

    public string getMatter()
    {
        return matter;
    }

    public DateTime getDate()
    {
        return date;
    }

    public Rating getNext()
    {
        return next;
    }

    public void setNext(Rating valutazione)
    {
        next = valutazione;
    }
}

Main:

    static void Main(string[] args)
    {
        ListRatings lr = new ListRatings();
        Rating r1 = new Rating(9, "Math", new DateTime(2021, 10, 5));
        Rating r2 = new Rating(10, "sport", new DateTime(2021, 11, 3));
        Rating r3 = new Rating(6, "English", new DateTime(2021, 11, 7));

        lr.InsertNewRat(r1);
        lr.InsertNewRat(r2);
        lr.InsertNewRat(r3);

        lr.PrintRats();
        Console.ReadKey();

    }

OUTPUT

Math
05/10/2021 00:00:00
9
English
07/11/2021 00:00:00
6

and the program stop and say error: System.NullReferenceException [is in the second e.getNext(); i used in the class ListRating]

with this output you can see that is printing the first one and jump to the last one without printing the second one.

The output i need is

Math
05/10/2021 00:00:00
9
Sport
10
03/11/07 00:00:00
English
07/11/2021 00:00:00
6

sorry for my bad english

4
  • Have you checked this implementation for a linked list? geeksforgeeks.org/linked-list-implementation-in-c-sharp Commented Jan 13, 2022 at 22:19
  • 1
    what's the benefits of a linked list over a standard list ? Commented Jan 13, 2022 at 22:28
  • 1
    our teacher give us to do it, i mean it's not bad at all. we often do something in hard way when we can just do it in an easy way, but is like training. and also for that: Linked lists are an ordered collection of objects. So what makes them different from normal lists? Linked lists differ from lists in the way that they store elements in memory. While lists use a contiguous memory block to store references to their data, linked lists store references as part of their own elements. Commented Jan 13, 2022 at 22:37
  • 1
    An advantage would be, it's faster to insert or remove elements from the middle of a modestly large linked list since you update pointers instead of copying a potentially large block of memory. Commented Jan 13, 2022 at 22:49

3 Answers 3

1

You aren't correctly updating your linked list. This code:

else
{
    first.setNext(rating);
    last.setNext(rating);
}

always sets both the first and last node's next node to the one you're trying to insert.

Instead, you want

else
{
    last.setNext(rating);
    last = rating;
}

This sets the node that's currently last to the new node, then updates your pointer to the last node to rating, which should now be last in the linked list.

As for the NullReferenceException, @TheVillageIdiot is correct

Sign up to request clarification or add additional context in comments.

1 Comment

I ran it and get the output you expect...
0

You can adjust this solution:

class Program
{
    static void Main(string[] args)
    {
        var list = new List<int>();
        list.Append(1);
        list.Append(2);
        list.Append(3);
        list.Append(4);
        list.Append(5);

        list.Print();

        var list2 = new List<int>(11, 22, 33);
        list.Concat(list2);
        list.Print();
    }
}

public class List<T>
{
    Node<T> first;
    Node<T> last;

    public List()
    {

    }

    public List(params T[] data)
    {
        Append(data);
    }

    public void Append(params T [] data)
    {
        foreach (T d in data)
        {
            var n = new Node<T> { data = d };
            if (last == null)
            {
                first = n;
                last = n;
            }
            else
            {
                last.next = n;
                last = n;
            }
        }
    }

    public void Concat(List<T> list)
    {
        if (last == null)
        {
            first = list.first;
            last = list.last;
        }
        else
        {
            last.next = list.first;
            last = list.last;
        }
    }

    public void Print() 
    {
        var n = first;
        while (n != null)
        {
            Debug.Write(n.data);
            if (n.next != null)
                Debug.Write(" -> ");

            n = n.next;
        }
        
        Debug.WriteLine("");
    }
}

public class Node<T>
{
    public T data { get; set; }
    public Node<T> next { get; set; }
}

5 Comments

the solution i need is without using list method. (is possibile)
Just change "List<T>" to "MyList<T>", and "List" to "MyList"...
i didn't get it. i dont mean name but the list method. in others word my code don't have to contains something like that: list<T>...
My List class is my own class, not the .NET List class. You can rename it whatever you like, that's the point. Delete the next() from your Rating class, than create your list like so : var ratings = new MyList<Rating>(). Etc...
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

You are trying to create a linked list implementation. Not sure if you need last in your RatingsList class. Personally, I would rename first to rating as well. The issue is how you are iterating over the ratings in PrintRats function. Change it to this and then try:

public void PrintRats()
{
    Rating e = first;
    
    while(e!=null)
    {
        Console.WriteLine(e.getMatter());
        Console.WriteLine(e.getDate());
        Console.WriteLine(e.getRate());

        e = e.getNext();
    }
}

Also, you can move all those Console.WriteLine in the above method into Rating class. Lastly, naming methods PrintRats instead of PrintRatings does not make it concise.

PS: If I'm sounding cranky, this is still before morning tea.

2 Comments

i use many console writeline instead of cycle to help with design my bad english. so the first 3 lines were for first 3 line on output etc...
with your code i didn't get the error but the program still print just the first and last one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.