예제 #1
0
        public void Prepend(T data)
        {
            DoubleNode <T> dnode = new DoubleNode <T>(data);

            dnode.next = head;
            dnode.prev = null;
            if (head != null)
            {
                head.prev = dnode;
            }
            head = dnode;
        }
예제 #2
0
        public void PrintList()
        {
            if (head == null)
            {
                Console.WriteLine("List Is Empty");
                return;
            }
            DoubleNode <T> node = head;

            while (node != null)
            {
                Console.Write(node.data + " ");
                node = node.next;
            }
            Console.WriteLine();
        }
예제 #3
0
        public void NodeCount()
        {
            DoubleNode <T> temp = head;

            if (head == null)
            {
                Console.Write("\nEmpty List");
                return;
            }
            int count = 0;

            while (temp.next != null)
            {
                temp   = temp.next;
                count += 1;
            }
            Console.WriteLine($"List Length is {count+1}");
        }
예제 #4
0
        public void Delete(int position)
        {
            int k = 1;

            if (head == null)
            {
                Console.WriteLine("List Is Empty");
                return;
            }
            //First Node
            if (position == 1)
            {
                head = head.next;
                if (head != null)
                {
                    head.prev = null;
                    return;
                }
            }

            DoubleNode <T> temp  = head;
            DoubleNode <T> temp2 = null;

            while (temp.next != null)
            {
                k++;
                if (position == k)
                {
                    temp2 = temp.next.prev;
                    if (temp.next.next != null)
                    {
                        temp.next      = temp.next.next;
                        temp.next.prev = temp2.prev;
                    }
                    else
                    {
                        temp.next = null;
                    }
                    return;
                }
                temp = temp.next;
            }
        }
예제 #5
0
        public void Append(T data)
        {
            DoubleNode <T> temp = new DoubleNode <T>(data);

            temp.next = null;
            DoubleNode <T> current = head;

            //if list is empty
            if (current == null)
            {
                temp.prev = null;
                head      = temp;
                return;
            }
            while (current.next != null)
            {
                current = current.next;
            }
            current.next = temp;
            temp.prev    = current;
        }
예제 #6
0
        public void InsertAtMiddle(T data, int position)
        {
            int            k       = 1;
            DoubleNode <T> newNode = new DoubleNode <T>(data);

            if (position == 1)
            {
                newNode.next = head;//insert at begining First Time
                newNode.prev = null;
                if (head != null)
                {
                    head.prev = newNode;
                }
                head = newNode;
                return;
            }

            DoubleNode <T> temp = head;

            while ((k < position - 1) && (temp.next != null))
            {
                temp = temp.next;
                k++;
            }

            if (k < position - 1)
            {
                Console.WriteLine($"Poisition not found at position {position}");
                return;
            }
            newNode.next = temp.next;
            newNode.prev = temp;
            if (temp.next != null)
            {
                temp.next.prev = newNode;
            }
            temp.next = newNode;
            return;
        }
예제 #7
0
        public void NodeSearch(T data)
        {
            DoubleNode <T> temp = head;

            if (head == null)
            {
                Console.Write("\nEmpty List");
                return;
            }
            int count = 1;

            while (temp != null)
            {
                if (temp.data.Equals(data))
                {
                    Console.WriteLine($"{data} found at index {count}");
                    return;
                }
                temp   = temp.next;
                count += 1;
            }
            Console.WriteLine($"{data} Not  found in List");
        }
예제 #8
0
 public DoublyLinkedList()
 {
     head = null;
 }
예제 #9
0
 public DoubleNode(T data)
 {
     this.data = data;
     next      = null;
     prev      = null;
 }