Friday, 5 July 2013

C SHARP PROGRAM OF LINKED LIST


Write a program to implement Linked list.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace linkedlist

{

    class Program

    {

        static void Main(string[] args)

        {

            int num,sval,type;

            LinkedList<int> list = new LinkedList<int>();

            LinkedListNode<int> curr, newnode;

            for (int x = 0; x < 5; x++)

            {

                Console.Write("Enter Number ");

                num = int.Parse(Console.ReadLine());

                newnode = new LinkedListNode<int>(num);

            start:

                Console.WriteLine("1.First");

                Console.WriteLine("2.Last");

                Console.WriteLine("3.Before");

                Console.WriteLine("4.After");

                Console.Write("Enter Position to Insert : ");

                type = int.Parse(Console.ReadLine());

                switch (type)

                {

                    case 1:

                        {

                            list.AddFirst(newnode);

                            break;

                        }

                    case 2:

                        {

                            list.AddLast(newnode);

                            break;

                        }

                    case 3:

                        {

                            Console.Write("Enter Before which no ");

                            sval = int.Parse(Console.ReadLine());

                            for (curr = list.First; curr != null; curr = curr.Next)

                            {

                                if (curr.Value == sval)

                                {

                                    list.AddBefore(curr, newnode);

                                    break;

                                }

                            }

                            break;

                        }

                    case 4:

                        {

                            Console.Write("Enter After which no ");

                            sval = int.Parse(Console.ReadLine());

                            for (curr = list.First; curr != null; curr = curr.Next)

                            {

                                if (curr.Value == sval)

                                {

                                    list.AddAfter(curr, newnode);

                                    break;

                                }

                            }

                            break;

                        }

                    default:

                        {

                            Console.WriteLine("Invalid Choice");

                            goto start;

                        }

                }

            }

            for (curr = list.First; curr != null; curr = curr.Next)

            {

                Console.WriteLine(curr.Value);

            }

            list.RemoveLast();

            list.RemoveFirst();

            list.Remove(3);

           

            for (curr = list.First; curr != null; curr = curr.Next)

            {

                Console.WriteLine(curr.Value);

            }

        }

    }

}

No comments:

Post a Comment