Exemple #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.
         }
     }
 }
Exemple #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);
             }
         }
     }
 }
Exemple #3
0
        public void DisplayAll()
        {
            //return its data
            MyLinkedListNode temp = first;

            while (temp != null)
            {
                Console.Write(temp.getData() + " ");
                temp = temp.getNext();
            }
            Console.WriteLine();
        }
Exemple #4
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());
        }
Exemple #5
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);
        }
Exemple #6
0
 public int getLastData()
 {
     //get last element
     return(last.getData());
 }
Exemple #7
0
 public int getFirstData()
 {
     //get first element
     return(first.getData());
 }