Пример #1
0
 /// <summary>
 /// Removes duplicated from sorted list
 /// ASsumption: the list is already sorted either ascending or descending.
 /// </summary>
 public void RemoveDuplicateFromSortedList()
 {
     for (MyLinkedListNode i = this.first; i != null && i != this.last; i = i.getNext())
     {
         var leftNode  = i;
         var rightNode = i.getNext();
         if (leftNode.getData() == rightNode.getData())
         {
             MyLinkedListNode duplicateNode = rightNode.getNext();
             for (; duplicateNode != null && duplicateNode.getData() == rightNode.getData(); duplicateNode = duplicateNode.getNext())
             {
             }
             if (duplicateNode != null) // if nonduplicate found
             {
                 duplicateNode.setPrevious(leftNode);
                 leftNode.setNext(duplicateNode);
             }
             else //duplicate till the end of the linked list.
             {
                 leftNode.setNext(duplicateNode);
             }
             //recliam the duplicate chain  to get garbage collected.
         }
     }
 }
Пример #2
0
 public void bubblesortDescending()
 {
     //apply bubble sort priciples to a linked list
     for (MyLinkedListNode i = this.first; i != this.last; i = i.getNext())
     {
         for (MyLinkedListNode j = i.getNext(); j != null; j = j.getNext())
         {
             if (i.getData() < j.getData())
             {
                 var temp = i.getData();
                 i.setData(j.getData());
                 j.setData(temp);
             }
         }
     }
 }
Пример #3
0
 public int removeFirst()
 {
     if (count == 0)
     {
         throw new Exception("No such element exists!");
     }
     else if (count == 1)
     {
         //make first and last null
         //save the data you want to return before changing
         //the references
         int temp = first.getData();
         first = last = null;
         count--;
         return(temp);
     }
     else
     {
         //save the data
         int temp = first.getData();
         //save a reference to the second node
         MyLinkedListNode tempref = first.getNext();
         //break the next link from first to the second node
         first.setNext(null);
         //break the link from second node back to first node
         tempref.setPrevious(null);
         //reset first to the second node
         first = tempref;
         count--;
         return(temp);//return the data of the remove node
     }
 }
Пример #4
0
        public void DisplayAll()
        {
            //return its data
            MyLinkedListNode temp = first;

            while (temp != null)
            {
                Console.Write(temp.getData() + " ");
                temp = temp.getNext();
            }
            Console.WriteLine();
        }
Пример #5
0
        public int getData(int index)
        {
            //make sure that the index is valid
            if (index < 0 || index >= count)
            {
                throw new Exception("index out of bound");
            }
            //sequence through the linkedlist until you reach the node
            //at the given index
            //return its data
            MyLinkedListNode temp = first;
            int i = 0;

            while (i < index)
            {
                temp = temp.getNext();
                i++;
            }
            //temp should be referencing the node at the given index
            return(temp.getData());
        }
Пример #6
0
        //    public int removeLast(){
        //
        //    }
        public int[] toArray()
        {
            //copy the entire list to an array an return it
            //create the array
            int[] array = new int[count];
            //sequence through linkedlist from first node to last
            //use a temp to start at the first node
            MyLinkedListNode temp = first;
            int index             = 0;

            while (temp != null)
            {
                //get data from node and copy it to array
                array[index] = temp.getData();

                //increment index
                index++;
                //move to next node
                temp = temp.getNext();
            }
            return(array);
        }
Пример #7
0
        MyLinkedList ReverseLinkedList(MyLinkedList myLinkedList)
        {
            MyLinkedListNode left     = myLinkedList.getFirst();
            MyLinkedListNode right    = myLinkedList.getLast();
            MyLinkedListNode tempNode = null;

            //Shuffle the  right neighbours of left node to point to right.
            left.getNext().setPrevious(right);

            //Shuffle the left of neighbours of right node to point to left.
            right.getPrevious().setNext(left);

            // make sure that the old left neighbor of right node is  now the recent  left neighbour of left node
            // and the old left neighbour of  left node is now  the recent left neighbor of right node
            tempNode = left.getPrevious();
            left.setPrevious(right.getPrevious());
            right.setPrevious(tempNode);

            // make sure that the old right neighbor of right node is  now the recent  right neighbour of left node
            // and the old right neighbour of left node is now  the recent right neighbor of right node
            tempNode = left.getNext();
            left.setNext(right.getNext());
            right.setNext(tempNode);

            //Arrange the first and last node as they are at the edge
            myLinkedList.setFirstNode(right);
            myLinkedList.setLastNode(left);

            //Get the first and last after reversing the left and right edge nodes.
            left  = myLinkedList.getFirst();
            right = myLinkedList.getLast();

            //This is for the remaining inner nodes.
            for (int i = 1; i < myLinkedList.getCount() / 2; i++)
            {
                left = left.getNext();
                if (left == right) //left and right have joined  for even length  of linked list.
                {
                    break;
                }

                right = right.getPrevious();

                if (left == right)//left and right have joined  for odd length  of linked list.
                {
                    break;
                }
                //Shuffle the refences of neighbours of left to point to right.
                if (left.getNext() == right) // left and right about to join
                {
                    left.getPrevious().setNext(right);
                    right.getNext().setPrevious(left);

                    left.setNext(right.getNext());
                    right.setPrevious(left.getPrevious());

                    left.setPrevious(right);
                    right.setNext(left);
                    break;
                }

                left.getNext().setPrevious(right);
                left.getPrevious().setNext(right);
                //Shuffle the refences of neighbours of right to point to left.
                right.getNext().setPrevious(left);
                right.getPrevious().setNext(left);

                // make sure that the old left neighbor of right node is  now the recent  left neighbour of left node
                // and the old left neighbour of  left node is now  the recent left neighbor of right node
                tempNode = left.getPrevious();
                left.setPrevious(right.getPrevious());
                right.setPrevious(tempNode);

                // make sure that the old right neighbor of right node is  now the recent  right neighbour of left node
                // and the old right neighbour of left node is now  the recent right neighbor of right node
                tempNode = left.getNext();
                left.setNext(right.getNext());
                right.setNext(tempNode);

                tempNode = left;
                left     = right;
                right    = tempNode;
            }
            return(myLinkedList);
        }