i am not getting the desired result in link list in C#
when i am putting the record bt its nt showing in the records when i view this code
so plsss help me to find the desired result

using System;
using System.Collections.Generic;
using System.Text;

namespace Single_Linked_List
{
    class Node
    {
        public int rollNumber;
        public string name;
        public Node next;
    }
    class List
    {
        Node START;
        public List()
        {
            START = null;
        }
        public void addNode()
        {
            int rollNo;
            string nm;
            Console.Write("\nEnter The Roll Number Of The Student : ");
            rollNo = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter The Name Of The Student");
            nm = Console.ReadLine();
            Node newnode = new Node();
            newnode.rollNumber = rollNo;
            newnode.name = nm;

            if (START == null || rollNo <= START.rollNumber)
            {
                if ((START != null) && (rollNo == START.rollNumber))
                {
                    Console.WriteLine("\nDuplicate Roll Numbers Are Not Allowed\n");
                    return;
                }
                newnode.next = START;
                
                return;
            }
            Node previous, current;
            previous = START;
            current = START;

            while ((current != null) && (rollNo >= current.rollNumber))
            {
                if (rollNo == current.rollNumber)
                {
                    Console.WriteLine("\nDuplicate Roll Numbers Not Allowed\n");
                    return;
                }
                previous = current;
                current = current.next;
            }
            newnode.next = current;
            previous.next = newnode;
        }
        public bool delNode (int rollNo)
        {
            Node previous, current;
            previous = current = null;
            if (Search(rollNo, ref previous, ref current) == false)
                return false;
            previous.next = current.next;
            if (current == START)
                START = START.next;
            return true;
        }
        public bool Search (int rollNo, ref Node previous, ref Node current)
        {
            previous = START;
            current = START;

            while ((current != null) && (rollNo != current.rollNumber))
            {
                previous = current;
                current = current.next;
            }
            if (current == null)
                return (false);
            else
                return (true);
        }
        public void traverse()
        {
            if (listEmpty())
                Console.WriteLine("\nList is Empty\n");
            else
            {
                Console.WriteLine("\nThe Records in the list are : ");
                Node currentNode;
                for (currentNode = START; currentNode != null; currentNode = currentNode.next)
                    Console.Write(currentNode.rollNumber + " " + currentNode.name + "\n");
                Console.WriteLine();
            }
        }
        public bool listEmpty()
        {
            if (START == null)
                return true;
            else
                return false;
        }
    
        static void Main(string[] args)
        {
            List obj = new List();
            while (true)
	        {
                try
                {
                    Console.WriteLine("\nMenu");
                    Console.WriteLine("1. Add A Record to the list");
                    Console.WriteLine("2. Delete A Record from the list");
                    Console.WriteLine("3. View All The Records in the list");
                    Console.WriteLine("4. Search for A Record in the list");
                    Console.WriteLine("5. Exit");
                    Console.WriteLine("\nEnter Your Choice (1-5) : ");
                    char ch = Convert.ToChar(Console.ReadLine());

                    switch (ch)
                    {
                        case '1':
                            {
                                obj.addNode();
                            }
                            break;
                            
                        case '2' :
                            {
                                if (obj.listEmpty())
                                {
                                    Console.WriteLine("\nList Is Empty");
                                    break;
                                }
                                Console.Write("\nEnter The Roll Number of the student whose record is to be deleted : ");
                                int rollNo = Convert.ToInt32(Console.ReadLine());
                                Console.WriteLine();
                                if (obj.delNode(rollNo) == false)
                                    Console.WriteLine("\nRecord not found.");
                                else
                                    Console.WriteLine("Record with roll number " + rollNo + "deleted");
                            }
                            break;
                        case '3' :
                            {
                                obj.traverse();
                            }
                            break;
                        case'4' :
                            {
                                if (obj.listEmpty() == true)
                                {
                                    Console.WriteLine("\nList Is empty");
                                    break;
                                }
                                Node previous, current;
                                previous = current = null;
                                Console.Write("\nEnter the roll number of the student whose student is to be searched : ");
                                int num = Convert.ToInt32(Console.ReadLine());
                                if (obj.Search(num, ref previous, ref current) == false)
                                    Console.WriteLine("\nRecords not found.");
                                else
                                {
                                    Console.WriteLine("\nRecord found");
                                    Console.WriteLine("\nRoll Number : " + current.rollNumber);
                                    Console.WriteLine("\n Name : " + current.name);
                                }
                            }
                            break;
                        case '5' : 
                            return;
                        default :
                            {
                                Console.WriteLine("\nInvalid option");
                                break;
                            }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("\nCheck for the value entered.");
                }
	        }   
        }
    }
}

Besides the fact that there exists a generic LinkedList class which can do these things for you, you might consider to put your code in CODE TAGS. Nobody will even try to feel the need to start reading your code if you don't.

Besides the fact that there exists a generic LinkedList class which can do these things for you, you might consider to put your code in CODE TAGS. Nobody will even try to feel the need to start reading your code if you don't.

I agree. I read the thread and didn't reply to it because I thought you were going out of your way to use retired technologies. If you use code tags and generics I would be willing to help.

You have an issue in adding a node itself, i have corrected the code below. (START->next = newnode) will not work since you have not initialized the START node.

using System;
using System.Collections.Generic;
using System.Text;

namespace Single_Linked_List
{
    class Node
    {
        public int rollNumber;
        public string name;
        public Node next;
    }

    class List
    {
        Node START;
        public List()
        {
            START = null;
        }

        public void addNode()
        {
            int rollNo;
            string nm;
            Console.Write("\nEnter The Roll Number Of The Student : ");
            rollNo = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter The Name Of The Student");
            nm = Console.ReadLine();
            Node newnode = new Node();
            newnode.rollNumber = rollNo;
            newnode.name = nm;

            if (START == null || rollNo <= START.rollNumber)
            {
                if ((START != null) && (rollNo == START.rollNumber))
                {
                    Console.WriteLine("\nDuplicate Roll Numbers Are Not Allowed\n");
                    return;
                }
                //newnode.next = START;
                START = newnode;                
                return;
            }

            Node previous, current;
            previous = START;
            current = START;

            while ((current != null) && (rollNo >= current.rollNumber))
            {
                if (rollNo == current.rollNumber)
                {
                    Console.WriteLine("\nDuplicate Roll Numbers Not Allowed\n");
                    return;
                }
                previous = current;
                current = current.next;
            }
            newnode.next = current;
            previous.next = newnode;
        }
        public bool delNode (int rollNo)
        {
            Node previous, current;
            previous = current = null;
            if (Search(rollNo, ref previous, ref current) == false)
                return false;
            previous.next = current.next;
            if (current == START)
                START = START.next;
            return true;
        }
        
        public bool Search (int rollNo, ref Node previous, ref Node current)
        {
            previous = START;
            current = START;

            while ((current != null) && (rollNo != current.rollNumber))
            {
                previous = current;
                current = current.next;
            }
            if (current == null)
                return (false);
            else
                return (true);
        }

        public void traverse()
        {
            if (listEmpty())
                Console.WriteLine("\nList is Empty\n");
            else
            {
                Console.WriteLine("\nThe Records in the List are : ");
                Node currentNode;
                for (currentNode = START; currentNode != null; currentNode = currentNode.next)
                    Console.Write(currentNode.rollNumber + " " + currentNode.name + "\n");
                Console.WriteLine();
            }
        }
        
        public bool listEmpty()
        {
            if (START == null)
                return true;
            else
                return false;
        }
    
        static void Main(string[] args)
        {
            List obj = new List();
            while (true)
	        {
                try
                {
                    Console.WriteLine("\nMenu");
                    Console.WriteLine("1. Add A Record to the List");
                    Console.WriteLine("2. Delete A Record from the List");
                    Console.WriteLine("3. View All The Records in the List");
                    Console.WriteLine("4. Search for A Record in the List");
                    Console.WriteLine("5. Exit");
                    Console.WriteLine("\nEnter Your Choice (1-5) : ");
                    char ch = Convert.ToChar(Console.ReadLine());

                    switch (ch)
                    {
                        case '1':
                            {
                                obj.addNode();
                            }
                            break;
                            
                        case '2' :
                            {
                                if (obj.listEmpty())
                                {
                                    Console.WriteLine("\nList Is Empty");
                                    break;
                                }
                                Console.Write("\nEnter The Roll Number of the student whose record is to be deleted : ");
                                int rollNo = Convert.ToInt32(Console.ReadLine());
                                Console.WriteLine();
                                if (obj.delNode(rollNo) == false)
                                    Console.WriteLine("\nRecord not found.");
                                else
                                    Console.WriteLine("Record with roll number " + rollNo + "deleted");
                            }
                            break;
                        case '3' :
                            {
                                obj.traverse();
                            }
                            break;
                        case'4' :
                            {
                                if (obj.listEmpty() == true)
                                {
                                    Console.WriteLine("\nList Is empty");
                                    break;
                                }
                                Node previous, current;
                                previous = current = null;
                                Console.Write("\nEnter the roll number of the student whose student is to be searched : ");
                                int num = Convert.ToInt32(Console.ReadLine());
                                if (obj.Search(num, ref previous, ref current) == false)
                                    Console.WriteLine("\nRecords not found.");
                                else
                                {
                                    Console.WriteLine("\nRecord found");
                                    Console.WriteLine("\nRoll Number : " + current.rollNumber);
                                    Console.WriteLine("\n Name : " + current.name);
                                }
                            }
                            break;
                        case '5' : 
                            return;
                        default :
                            {
                                Console.WriteLine("\nInvalid option");
                                break;
                            }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("\nCheck for the value entered.");
                }
	        }   
        }
    }
}
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.