Saturday, October 22, 2011

DOUBLY LINKEDLIST


DOUBLY LINKEDLIST
Code in Csharp

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

namespace doubly_link
{
    class dnode
    {
        public int id;
        public string name;
        public dnode next;
        public dnode prev;
        public dnode()
        {
            id = 0;
            name = "";
            next = null;
            prev = null;
        }
        public dnode(dnode n)
        {
            this.id = n.id;
            this.name = n.name;
            next = null;
            prev = null;
        }
        public dnode(string nam, int id1)
        {
            name = nam;
            id = id1;
            next = null;
            prev = null;
        }
    }
    class dlink_list
    {
        dnode head, temp,curr;

        public dlink_list()
        {
            curr=head = temp = null;
        }
        public void insert(dnode n)
        {
            dnode a = head;
            if (head == null)
            {
                 curr=head =temp = new dnode(n);
            
            }
            else 
            {

                temp = curr;
                curr = new dnode(n);
                temp.next = curr;
                curr.prev = temp;         
               
            }
        }
        public void showforward()
        {
            dnode a = head;
            while (a!= null)
            {
                Console.WriteLine("name is:{0} \t id is:{1}", a.name, a.id);
                a = a.next;
            }
        }
        public void showbackward()
        {
            dnode a = curr;
            while (a != null)
            {
                Console.WriteLine("Name is:{0} \tId is:{1}", a.name, a.id);
                a = a.prev;
            }
        }
        public void deletef()
        {
            //dnode b;
            head = head.next;
            head.prev = null;
           
           
        }
        public void insertfirst(dnode n)
        {
            dnode t = new dnode(n);
            t.next = head;
            head.prev = t;
            head = t;
           // t.prev = head;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            dlink_list a = new dlink_list();
            a.insert(new dnode("thor",23));
            a.insert(new dnode("true", 75));
            a.insert(new dnode("Hamer", 7));
            a.insert(new dnode("kahn", 23));
            a.insertfirst(new dnode("cham", 34));
           // a.deletef();
            a.showforward();
            Console.WriteLine("-------------------------");
            Console.WriteLine("After deleted first");
            Console.WriteLine("-------------------------");
            a.deletef();
            a.showbackward();
            Console.WriteLine("-------------------------");
           
        }
    }
}

No comments:

Post a Comment

used in operatonal research LP(linear programming) The Simplex Algorithm Simplex method Resolve using the Simple...