Singly Linked List in Data Structure

Singly Linked List is the simplest type of Data Structure where each node points to the next node. And the last node does not point any other node, Hence points to Null.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkedListDemo
{
    /// <summary>
    /// Node Class - Define Structure of Node
    /// </summary>
    class Node
    {
        public int Data;
        public Node Next;
    }

    /// <summary>
    /// List Class - Implement Operations of Linked List
    /// </summary>
    class List
    {
        Node Head;
        public List()
        {
            Head = null;
        }

        /// <summary>
        /// Addition of New Node
        /// </summary>
        public void AddNode()
        {
            Console.Write("Enter value to insert :: ");
            int item = Convert.ToInt32(Console.ReadLine());

            Node NewNode = new Node();
            NewNode.Data = item;

            if (Head == null)
                Head = NewNode;

            else
            {
                Node Current;
                Current = Head;
                while (Current.Next != null)
                {
                    Current = Current.Next;
                }

                Current.Next = NewNode;
            }

            NewNode.Next = null;
        }

        /// <summary>
        /// Traversing through the List
        /// </summary>
        public void Traverse()
        {
            Node Current;
            Current = Head;

            while (Current != null)
            {
                Console.Write(Current.Data + " - ");
                Current = Current.Next;
            }
        }


        /// <summary>
        /// Search Function - used in Addition & Deletion Operations as well.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="previous"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public bool Search(int item, ref Node previous, ref Node current)
        {
            previous = Head;
            current = Head;
            while ((current != null) && (item != current.Data))
            {
                previous = current;
                current = current.Next;
            }

            if (current == null)
                return false;
            else
                return true;
        }

        
        /// <summary>
        /// Simple Search Operation - No need to create this funtion if You are creating above search function
        /// </summary>
        /// <returns></returns>
        public bool Search2()
        {
            Node current = Head;
            Console.Write("Enter value to insert :: ");
            int item = Convert.ToInt32(Console.ReadLine());

            while ((current != null) && (item != current.Data))
            {
                current = current.Next;
            }

            if (current == null)
                return false;
            else
                return true;
        }


        /// <summary>
        /// For Deletion of node
        /// </summary>
        /// <returns></returns>
        public bool DeleteNode()
        {
            Console.Write("Enter value to delete :: ");
            int item = Convert.ToInt32(Console.ReadLine());

            Node previous, current;
            previous = current = null;

            if (Search(item, ref previous, ref current) == false)
                return false;

            previous.Next = current.Next;

            if (current == Head)
                Head = Head.Next;
            return true;
        }

        /// <summary>
        /// To check if the List is empty
        /// </summary>
        /// <returns></returns>
        public bool ListEmpty()
        {
            if (Head == null)
                return true;
            else
                return false;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            List MyList = new List();
            while (true)
            {
                Console.WriteLine("\nMenu");
                Console.WriteLine("1. Add a Record");
                Console.WriteLine("2. Traverse List");
                Console.WriteLine("3. Delete a Record");
                Console.WriteLine("4. Search");
                Console.WriteLine("5. Exit");
                Console.Write("Your Option :: ");
                char ch = Convert.ToChar(Console.ReadLine());

                switch (ch)
                {
                    case '1':
                        MyList.AddNode();
                        break;

                    case '2':
                        MyList.Traverse();
                        break;

                    case '3':
                        if (MyList.ListEmpty() == true)
                            Console.WriteLine("List is Empty!!!");
                        else
                            MyList.DeleteNode();
                        break;
                    
                    case '4':
                        if (MyList.ListEmpty() == true)
                            Console.WriteLine("List is Empty!!!");
                        else
                            if (MyList.Search2() == true)           // you can also call Search() by creating (previous = current = null)
                                Console.WriteLine("Item Found");
                            else
                                Console.WriteLine("Not Found");
                        break;
                    case '5':
                        return;

                    default:
                        Console.WriteLine("Invalid Option");
                        break;
                }

            }
        
        }
    }
}