Reversing a Double Linked List

 

Double linked list is a linked list which has connectors in both directions.

Quick way to reverse a double linked list is to reverse the connectors in the linked list.

This is the code in C# which reverses the data content in a double linked list.


using System;
using System.Text;

namespace DoubleLinkedList
{
    public class DoubleLink
    {
        public string Title { get; set; }
        public DoubleLink PreviousLink { get; set; }
        public DoubleLink NextLink { get; set; }

        public DoubleLink(string title)
        {
            Title = title;
        }

        public override string ToString()
        {
            return Title;
        }
    }

    public class DoubleLinkedList
    {
        private DoubleLink _first;
        public DoubleLinkedList()
        {
            _first = null;
        }
        public DoubleLink Insert(string title)
        {
    // Creates a link, sets its link to the first item and then makes this the first item in the list.
            DoubleLink link = new DoubleLink(title);
            link.NextLink = _first;

            if (_first != null)
                _first.PreviousLink = link;

            _first = link;

            return link;
        }

        public DoubleLink Delete()
        {
    // Gets the first item, and sets it to be the one it is linked to
            DoubleLink temp = _first;

            if (_first != null)
            {
                _first = _first.NextLink;

                if (_first != null)
                    _first.PreviousLink = null;
            }

            return temp;
        }

        public string PrintList()
        {
            DoubleLink currentLink = _first;

            StringBuilder builder = new StringBuilder();

            while (currentLink != null)
            {
                builder.Append(currentLink);
                currentLink = currentLink.NextLink;
            }

            return builder.ToString();
        }

        public void InsertAfter(DoubleLink link, string title)
        {
            if (link == null || string.IsNullOrEmpty(title))
                return;

            DoubleLink newLink = new DoubleLink(title);
            newLink.PreviousLink = link;
            if (link.NextLink != null)
                link.NextLink.PreviousLink = newLink;
                newLink.NextLink = link.NextLink;
                link.NextLink = newLink;
        }
        public void Reverse()
        {
            while (_first != null)
            {
               //Swap the links
                DoubleLink dl = _first.PreviousLink;
                _first.PreviousLink = _first.NextLink;
                _first.NextLink = dl;                if (_first.PreviousLink != null)
                    _first = _first.PreviousLink;  
                    // Move to the next node in original list
                else
                    break;
            }
        }
    }
}

 

6 thoughts on “Reversing a Double Linked List

  1. all the time i used to read smaller articles that also clear their motive, and that is also happening with this piece
    of writing which I am reading now.

  2. Hello! Do you use Twitter? I’d like to follow you if that would be okay. I’m definitely enjoying your blog
    and look forward to new posts.

  3. you are truly a good webmaster. The site loading speed is amazing.
    It sort of feels that you’re doing any unique trick. Moreover, The contents are masterwork. you’ve done a great activity on
    this topic!

  4. I do not even know the way I finished up right here, however I
    thought this post was good. I don’t understand who you are however certainly you are going to a famous blogger if you happen to are not already. Cheers!

  5. If you are going for most excellent contents like myself, just pay
    a visit this web site every day because it gives quality contents, thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s