ARTICLE

Insertion at the begining of a Linklist in C#

Posted by Abhishek Bhandari Articles | C# Language July 11, 2009
This code snippet shows how to insert data to a Linklist at the beginning.
Reader Level:

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

namespace ConsoleApplication38
{
    class node
    {
        public int delete;
        public int data;/*it will store the data*/
        public node link;/*it will hold the address of the next memory location where the data is stored*/
    }
    class Program
    {
        /*these are the variables */
        public static node START;
        public static node LASt;
        public static node CURRENT;
        public Program()
        {
            START = null;
            LASt = null;
        }
        public void insertionbegin()
        {
            node newnode = new node();
            Console.WriteLine("Enter the data");
            newnode.data = int.Parse(Console.ReadLine());
            newnode.link = START;
            START = newnode;
            Console.WriteLine("DAta has been inserted");

        }

        public void displaynode()
        {
            Console.WriteLine("Data in the list are");
            CURRENT = START;
            while (CURRENT != null)
            {
                Console.WriteLine(CURRENT.data);
                CURRENT = CURRENT.link;
            }
            Console.ReadLine();

        }

        static void Main(string[] args)

        {
            Program obj = new Program();
            do
            {
                Console.Clear();
                Console.WriteLine("********************************************");
                Console.WriteLine("**        MAIN MENU     *****************");
                Console.WriteLine("**************************************");
                Console.WriteLine("*1.Inserting at the end of the list******");
                Console.WriteLine("*2.Insertion at the begining of the list****");
                Console.WriteLine("*3.Display the list       *******************");
                Console.WriteLine("*4.Exit                      ****************");
                Console.WriteLine("*********************************************");
                Console.WriteLine("\nEnter ur choice");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1: { break; }
                    case 2: { obj.insertionbegin(); break; }
                    case 3: { obj.displaynode(); break; }
                    case 4: { break; }
                    default: { Console.WriteLine("Invalid option"); break; }
                }
            } while (true);
            Console.ReadLine();

        }
    }
}

Login to add your contents and source code to this article
post comment
     

ok as u say sir ill try my level best well im new to these thing ..sir i wana be what u are ok so to achieve my dream im working ok ok but after understanding and practising these logics i froget them after 3 to 4 days i dont know why or these weak points are telling me that software is not my line ..hummm im confused but still hope to understand programming in 5 yrs cauze after 5 yrs  ill be 24 yr old so its time to earn not to learn .
Good Nite
your well wisher
Gniitian Abhishek Bhandari

Posted by Abhishek Bhandari Jul 14, 2009

Hi, Congrats for your first article. 


But , I am sorry to say that this article is not up to par. Only posting code is not going to help any way. If possible, please provide  us some explanation also such that we could get more frit out of your article. 

Thanks 

Posted by Dhananjay Kumar Jul 13, 2009
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter