hey guys can someone help me with this i am stuck here where do i define a loop to add more nodes in the linked list or any other way to define nodes itslef in the code

 class Program
    {
        static void Main(string[] args)
        {



                List obj = new List();


                    obj.addnode();
                obj.traverse();





        }
        class LinkedListNode
        {

            public int rollnumber;
            public string name;
            public int score1;
            public int score2;
            public int score3;
            public int average;
            public LinkedListNode next;
        }
        class List
        {
            LinkedListNode start;


            public List()
            {
                start = null;

            }


            public void addnode()
            {


                string nm;
                int rollno;
                int s1;
                int s2;
                int s3;
                int avg;
                LinkedListNode previous, current;
                previous = start;
                current = start;



                    Console.WriteLine("\nenter the name of the student");
                    nm = Console.ReadLine();
                    Console.WriteLine("\nenter the roll number of the student");
                    rollno = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("\nenter score1:");
                    s1 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("\nenter score2:");
                    s2 = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("\nenter score3:");
                    s3 = Convert.ToInt32(Console.ReadLine());

                    avg = (s1 + s2 + s3) / 3;



                    LinkedListNode newnode = new LinkedListNode();
                    newnode.rollnumber = rollno;
                    newnode.name = nm;
                    newnode.score1 = s1;
                    newnode.score2 = s2;
                    newnode.score3 = s3;
                    newnode.average = avg;
                    newnode.next = start;
                    start = newnode;
                    return;




                    if (start == null || rollno >= start.rollnumber)
                    {
                        if ((start != null) && (rollno == start.rollnumber))
                        {
                            Console.WriteLine("\nduplicate roll numbers not allowed");
                            return;
                        }
                        newnode.next = start;
                        start = newnode;
                        return;
                    }


                    while ((current != null) && (rollno >= current.rollnumber))
                    {


                        previous = current;
                        current = current.next;
                    }


                    newnode.next = current;
                    previous.next = newnode;



                }


        public void traverse()
        {
            if (listEmpty())
            {
                Console.WriteLine("\nlist is empty");
            }

            else
            {
                Console.WriteLine("\nRoll No." + " " + "Name" + "\t" + "score-1" + "\t" + "score-2" + "\t" + "score-3" + "\t " + "Average");
                Console.ReadLine();
                LinkedListNode currentnode;
                for (currentnode = start; currentnode != null; currentnode = currentnode.next)
                {


                    Console.WriteLine("\n" + currentnode.rollnumber + "  \t   " + currentnode.name + "  \t  " + currentnode.score1 + " \t   " + currentnode.score2 + " \t   " + currentnode.score3 + "   \t " + currentnode.average);
                    Console.ReadLine();

                }
            }
        }

            public bool listEmpty()
            {

                if (start == null)

                    return true;

                else

                    return false;
            }
            }
}
}

Recommended Answers

All 3 Replies

You probably have some Data file to read from. Read it line by line in a loop.
If you put in the data manually,this might help.

Your code is somewhat confusing. It looks like you're trying to add a LinkedListNode to a List. You would probably have more luck using the LinkedList type rather than a List type.

In looking at your code it seemed somewhat familiar. So looked back and found the code I did for you before. Just to demonstrate adding items to a linked list I took that other code and modified it for user input instead of file input and it uses a linked list. Hope this helps. I also included a simple menu loop that you may be able to use.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Media;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        public class Student
        {
            public int Roll_No=new int();
            public string Name="";
            public int[] Score=new int[3];
            public double Avg=new double();
        }


        //I want to get data like this;
        public static LinkedList<Student> Students = new LinkedList<Student>();

        static void Main()
        {
            char Response = new char();
            while (!(Response.ToString().ToUpper() == "Q"))
            {
                Console.Clear();
                Console.WriteLine("A - Add more students\nL - List the students\nQ - Quit");
                Response = Console.ReadKey().KeyChar;
                switch (Response.ToString().ToUpper())
                {
                    case "A":
                    {
                        AddNodes();
                        break;
                    }
                    case "L":
                    {
                        Traverse();
                        break;
                    }
                }
            }
        }
        public static void AddNodes()
        {
            bool quit = false;
            while (!quit)
            {
                Console.Clear();
                Student TempStudent = new Student();

                Console.WriteLine("\nenter the name of the student");
                TempStudent.Name = Console.ReadLine();
                Console.WriteLine("\nenter the roll number of the student");
                TempStudent.Roll_No = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("\nenter score1:");
                TempStudent.Score[0] = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("\nenter score2:");
                TempStudent.Score[1] = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("\nenter score3:");
                TempStudent.Score[2] = Convert.ToInt32(Console.ReadLine());
                TempStudent.Avg = (TempStudent.Score[0] + TempStudent.Score[1] + TempStudent.Score[2]) / 3;
                Students.AddLast(TempStudent);
                Console.Write("\nAnother?(Y/N)");
                if (!(Console.ReadKey().ToString().ToUpper() == "Y"))
                {
                    quit = true;
                }

            } 
        }
        public static void Traverse()
        {
            Console.Clear();
            Console.WriteLine("Roll Nos. " + "Names\t\t" + "score 1\t" + "score 2\t" + "score 3\t" + "Average\n");
            foreach (var Person in Students)
            {
                Console.WriteLine(Person.Roll_No + "\t" + Person.Name + "\t  " + Person.Score[0] + "\t" + Person.Score[1] + "\t" + Person.Score[2] + "\tAvg. - " + Person.Avg);
            }
            Console.WriteLine("\nPress Any Key To Continue");
            Console.ReadLine(); 
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.