insertion of node at the begining and end of linklist

No.of Views794
Bookmarked0 times
Downloads 
Votes0
By  poly666   On  16 Feb 2010 00:02:46
Tag : CSharp , Miscellaneous
insertion of node at the begining and end of linklist
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

C# Code

{codecitation class="brush: csharp; gutter: true;" width="650px"}

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

namespace Linklist

{

/* 1st we will creat a node class so that we can access its the two variables*/

class node

{

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 insertionend()

{

node newnode = new node();

Console.WriteLine("Enter the data");

newnode.data = int.Parse(Console.ReadLine());

if (START == null)

{

START = newnode;

LASt = newnode;

newnode.link = null;

}

else

{

LASt.link = newnode;

newnode.link = null;

LASt = 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: { obj.insertionend(); break; }

case 2: { obj.insertionbegin(); break; }

case 3: { obj.displaynode();break; }

case 4: { break; }

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

}

} while (true);

Console.ReadLine();
}

}

}


{/codecitation}


Thank you

Bhandari

 
Sign Up to vote for this article
 
About Author
 
poly666
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-15 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top