Exemple #1
0
        private static DoublyLinkedListNode <int> SortedInsert <T>(DoublyLinkedListNode <int> head, int data)
        {
            if (head == null)
            {
                return(new DoublyLinkedListNode <int>(data));
            }

            var current = head;

            //var found = false;
            while (current != null)
            {
                if (current.Data > data)
                {
                    //found = true;
                    var newNode = new DoublyLinkedListNode <int>(data)
                    {
                        Previous = current.Previous, Next = current
                    };

                    if (current.Previous != null)
                    {
                        current.Previous.Next = newNode;
                    }
                    else
                    {
                        return(newNode);
                    }

                    break;
                }
                current = current.Next;
            }

            if (current == null)
            {
                var newNode = new DoublyLinkedListNode <int>(data);
                newNode.Next = null;
                head.Next    = newNode;
                //return newNode;
            }

            return(head);
        }
Exemple #2
0
        public static void MainNode(string[] args)
        {
            int[] nodeData = { 2, 4, 6, 8, 9 };// {5, 3, 2, 4, 6, 9, 1};//, 10, 11, 12, 13, 0, -1, -2, -3, -4, -5, 14, 15, 16, 17, 20, 28, 29};
            //string[] nodeData = { "abebe", "beso", "bela", "chala", "chube", "be", "beso", "chebete" };

            SinglyLinkedListNode <int> singlyLinkedListNode = null;

            DoublyLinkedListNode <int> head = null, tail = null;

            foreach (var llist in nodeData)
            {
                singlyLinkedListNode = InsertNode(singlyLinkedListNode, llist);

                if (tail == null)
                {
                    head = tail = new DoublyLinkedListNode <int>(llist);
                }
                else
                {
                    tail = new DoublyLinkedListNode <int>(llist, null, tail);
                    tail.Previous.Next = tail;
                }
            }

            //var h = SortedInsert<int>(head, 7);

            var rev = Reverse <int>(head);

            //var hh = InsertNodeAtPosition<int>(singlyLinkedListNode, 7, 6);

            //var nodeLength = GetNodeLength(singlyLinkedListNode);

            //var nod = RemoveDuplicates(node);

            int[] nodeData2 = { 1, 4, 6, 8, 9 };
            SinglyLinkedListNode <int> singlyLinkedListNode2 = null;

            foreach (var llist in nodeData2)
            {
                singlyLinkedListNode2 = InsertNode(singlyLinkedListNode2, llist);
            }
            var merge = FindMergeNode(singlyLinkedListNode, singlyLinkedListNode2);
        }
Exemple #3
0
        private static DoublyLinkedListNode <T> Reverse <T>(DoublyLinkedListNode <T> head)
        {
            if (head == null)
            {
                return(null);
            }

            var tail = new DoublyLinkedListNode <T>(head.Data);

            var curr = head.Next;

            while (curr != null)
            {
                tail = new DoublyLinkedListNode <T>(curr.Data, tail, null);
                tail.Next.Previous = tail;

                curr = curr.Next;
            }

            return(tail);
        }
Exemple #4
0
        public static void Execute()
        {
            int t = Convert.ToInt32(Console.ReadLine());

            for (int tItr = 0; tItr < t; tItr++)
            {
                DoublyLinkedList llist = new DoublyLinkedList();

                int llistCount = Convert.ToInt32(Console.ReadLine());

                for (int i = 0; i < llistCount; i++)
                {
                    int llistItem = Convert.ToInt32(Console.ReadLine());
                    llist.InsertNode(llistItem);
                }

                int data = Convert.ToInt32(Console.ReadLine());

                DoublyLinkedListNode llist1 = sortedInsert(llist.head, data);

                PrintDoublyLinkedList(llist1, " ");
                Console.WriteLine();
            }
        }
Exemple #5
0
 public DoublyLinkedList()
 {
     this.head = null;
     this.tail = null;
 }
Exemple #6
0
 public DoublyLinkedListNode(int nodeData)
 {
     this.data = nodeData;
     this.next = null;
     this.prev = null;
 }
Exemple #7
0
 public DoublyLinkedListNode(T d, DoublyLinkedListNode <T> next, DoublyLinkedListNode <T> previous)
 {
     Data     = d;
     Next     = next;
     Previous = previous;
 }
Exemple #8
0
 public DoublyLinkedListNode(T d)
 {
     Data = d;
     Next = Previous = null;
 }