Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //Single Linked List
            SinglyLinkedList singleList = new SinglyLinkedList();

            singleList.AddToLinkedList("A");
            singleList.AddToLinkedList("B");
            singleList.AddToLinkedList("C");
            singleList.AddToLinkedList("D");
            singleList.AddToLinkedList("E");

            singleList.DisplayAllItems();
            Console.ReadLine();

            //Double Linked List
            DoubleLinkedList list = new DoubleLinkedList();

            list.Insert("A");
            list.Insert("B");
            list.Insert("C");

            DoubleLink link4 = list.Insert("D");

            list.Insert("E");
            Console.WriteLine("List: " + list);

            list.InsertAfter(link4, "[4a]");
            Console.WriteLine("List: " + list);
            Console.Read();
        }
Ejemplo n.º 2
0
        public override string ToString()
        {
            DoubleLink    currentLink = _first;
            StringBuilder builder     = new StringBuilder();

            while (currentLink != null)
            {
                builder.Append(currentLink);
                currentLink = currentLink.NextLink;
            }
            return(builder.ToString());
        }
Ejemplo n.º 3
0
        public DoubleLink Delete()
        {
            DoubleLink f = _first;

            if (_first != null)
            {
                _first = _first.NextLink;
                if (_first != null)
                {
                    _first.PreviousLink = null;
                }
            }
            return(f);
        }
Ejemplo n.º 4
0
        public DoubleLink Insert(string title)
        {
            // Creates a link
            DoubleLink link = new DoubleLink(title);

            //makes it the first item in the list
            link.NextLink = _first;

            if (_first != null)
            {
                _first.PreviousLink = link;
            }
            _first = link;
            return(link);
        }
Ejemplo n.º 5
0
        public void InsertAfter(DoubleLink link, string title)
        {
            if (link == null || string.IsNullOrEmpty(title))
            {
                return;
            }
            DoubleLink newLink = new DoubleLink(title);

            newLink.PreviousLink = link;
            // Update the 'after' link's next reference, so its previous points to the new one
            if (link.NextLink != null)
            {
                link.NextLink.PreviousLink = newLink;
            }
            // Steal the next link of the node, and set the after so it links to our new one
            newLink.NextLink = link.NextLink;
            link.NextLink    = newLink;
        }
Ejemplo n.º 6
0
 public DoubleLinkedList()
 {
     _first = null;
 }