Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.31 KB | None | 0 0
  1.  
  2.  
  3. public class OrderedLinkedList extends LinkedListClass
  4. {
  5.         //default constructor
  6.     public OrderedLinkedList()
  7.     {
  8.         super();
  9.     }
  10.  
  11.         //copy constructor
  12.     public OrderedLinkedList(OrderedLinkedList otherList)
  13.     {
  14.         super(otherList);
  15.     }
  16.  
  17.     public void mergeSort()
  18.     {
  19.         first = recMergeSort(first);
  20.     }//end mergeSort
  21.  
  22.  
  23.     private LinkedListNode divideList(LinkedListNode head)
  24.     {
  25.         LinkedListNode middle;
  26.         LinkedListNode current;
  27.  
  28.         LinkedListNode secondListHead;
  29.  
  30.         if(head == null)   //list is empty
  31.             secondListHead = null;
  32.         else
  33.             if(head.link == null)   //list has only one node
  34.                 secondListHead = null;
  35.             else
  36.             {
  37.                 middle = head;
  38.                 current = head.link;
  39.  
  40.                 if(current != null)     //list has more than two nodes
  41.                     current = current.link;
  42.  
  43.                 while(current != null)
  44.                 {
  45.                     middle = middle.link;
  46.                     current = current.link;
  47.  
  48.                     if(current != null)
  49.                         current = current.link;
  50.                 } //end while
  51.  
  52.                 secondListHead = middle.link;   //secondListHead points
  53.                                         //to the first tnode of the
  54.                                         //second sublist
  55.                 middle.link = null;     //set the link of the last node
  56.                                         //of the first sublist to null
  57.             } //end else
  58.  
  59.             return secondListHead;
  60.     } //end divideList
  61.  
  62.  
  63.     private LinkedListNode mergeList(LinkedListNode first1,
  64.                                      LinkedListNode first2)
  65.     {
  66.         LinkedListNode lastSmall;   //reference variable to the
  67.                                     //last node of the merged list
  68.         LinkedListNode newHead;     //reference variable to the
  69.                                     //merged list
  70.  
  71.         if(first1 == null)      //the first sublist is empty
  72.             return first2;
  73.         else
  74.             if(first2 == null)   //the second sublist is empty
  75.                 return first1;
  76.             else
  77.             {
  78.                 if(first1.info.compareTo(first2.info) < 0) //compare
  79.                                                            //the first nodes
  80.                 {
  81.                     newHead = first1;
  82.                     first1 = first1.link;
  83.                     lastSmall = newHead;
  84.                 }
  85.                 else
  86.                 {
  87.                     newHead = first2;
  88.                     first2 = first2.link;
  89.                     lastSmall = newHead;
  90.                 }
  91.  
  92.                 while(first1 != null && first2 != null)
  93.                 {
  94.                     if(first1.info.compareTo(first2.info) < 0)
  95.                     {
  96.                         lastSmall.link = first1;
  97.                         lastSmall = lastSmall.link;
  98.                         first1 = first1.link;
  99.                     }
  100.                     else
  101.                     {
  102.                         lastSmall.link = first2;
  103.                         lastSmall = lastSmall.link;
  104.                         first2 = first2.link;
  105.                     }
  106.                 } //end while
  107.  
  108.                 if(first1 == null)  //first sublist is exhausted first
  109.                     lastSmall.link = first2;
  110.                 else              //second sublist is exhausted first
  111.                     lastSmall.link = first1;
  112.  
  113.                 return newHead;
  114.             }
  115.     }//end mergeList
  116.  
  117.  
  118.     private LinkedListNode recMergeSort(LinkedListNode head)
  119.     {
  120.         LinkedListNode otherHead;
  121.  
  122.         if(head != null)  //if the list is not empty
  123.            if(head.link != null)  //if the list has more
  124.                                   //than one node
  125.            {
  126.               otherHead = divideList(head);
  127.               head = recMergeSort(head);
  128.               otherHead = recMergeSort(otherHead);
  129.               head = mergeList(head, otherHead);
  130.            }
  131.  
  132.         return head;
  133.     } //end recMergeSort
  134.  
  135.  
  136.         //Method to determine whether searchItem is in
  137.         //the list.
  138.         //Postcondition: Returns true if searchItem is found
  139.         //               in the list; otherwise, it returns
  140.         //               false.
  141.     public boolean search(DataElement searchItem)
  142.     {
  143.         LinkedListNode current; //variable to traverse the list
  144.         boolean found;
  145.  
  146.         current = first;  //set current to point to the first
  147.                           //node in the list
  148.  
  149.         found = false;    //set found to false
  150.  
  151.         while(current != null && !found ) //search the list
  152.             if(current.info.compareTo(searchItem) >= 0)
  153.                 found = true;
  154.             else
  155.                 current = current.link; //make current point to
  156.                                         //the next node
  157.  
  158.         if(found)
  159.            found = current.info.equals(searchItem);
  160.  
  161.         return found;
  162.     }
  163.  
  164.         //Method to insert insertItem in the list
  165.         //Postcondition: first points to the new list,
  166.         //       newItem is inserted at the proper place
  167.         //       in the list, and count is incremented by 1.
  168.     public void insertNode(DataElement insertItem)
  169.     {
  170.         LinkedListNode current;     //variable to traverse the list
  171.         LinkedListNode trailCurrent;    //variable just before current
  172.         LinkedListNode newNode;     //variable to create a node
  173.  
  174.         boolean  found;
  175.  
  176.         newNode = new LinkedListNode(); //create the node
  177.         newNode.info = insertItem.getCopy(); //store newItem
  178.                                           //in the node
  179.         newNode.link = null;    //set the link field of the node
  180.                                 //to null
  181.  
  182.         if(first == null)  //Case 1
  183.         {
  184.            first = newNode;
  185.            count++;
  186.         }
  187.         else
  188.         {
  189.            trailCurrent = first;
  190.            current = first;
  191.            found = false;
  192.  
  193.            while(current != null && !found) //search the list
  194.                 if(current.info.compareTo(insertItem) >= 0)
  195.                    found = true;
  196.                 else
  197.                 {
  198.                    trailCurrent = current;
  199.                    current = current.link;
  200.                 }
  201.  
  202.            if(current == first)   //Case 2
  203.            {
  204.               newNode.link = first;
  205.               first = newNode;
  206.               count++;
  207.            }
  208.            else          //Case 3
  209.            {
  210.               trailCurrent.link = newNode;
  211.               newNode.link = current;
  212.               count++;
  213.            }
  214.         }//end else
  215.     }//end insertNode
  216.  
  217.  
  218.         //Method to delete deleteItem from the list.
  219.         //Postcondition: If found, the node containing
  220.         //               deleteItem is deleted from the
  221.         //               list. Also, first points to the first
  222.         //               node, last points to the last
  223.         //               node of the updated list, and count
  224.         //               is decremented by 1.
  225.     public void deleteNode(DataElement deleteItem)
  226.     {
  227.        LinkedListNode current;      //variable to traverse the list
  228.        LinkedListNode trailCurrent; //variable just before current
  229.        boolean found;
  230.  
  231.        if(first == null)    //Case 1; list is empty.
  232.           System.err.println("Can not delete from an "
  233.                            + "empty list.");
  234.        else
  235.        {
  236.            if(first.info.equals(deleteItem)) //Case 2
  237.            {
  238.               first = first.link;
  239.               count--;
  240.            }
  241.            else  //search the list for the node with the given info
  242.            {
  243.               found = false;
  244.               trailCurrent = first;   //set trailCurrent to point to
  245.                                     //the first node
  246.               current = first.link;   //set current to point to the
  247.                                       //second node
  248.  
  249.               while(current != null && !found)
  250.               {
  251.                   if(current.info.compareTo(deleteItem) >= 0)
  252.                      found = true;
  253.                   else
  254.                   {
  255.                      trailCurrent = current;
  256.                      current = current. link;
  257.                   }
  258.               } //end while
  259.  
  260.               if(current == null)   //Case 4
  261.                  System.out.println("Item to be deleted is "
  262.                                   + "not in the list.");
  263.               else
  264.                  if(current.info.equals(deleteItem)) //item to be
  265.                                          //deleted is in the list
  266.                  {
  267.                     if(first == current)    //Case 2
  268.                     {
  269.                        first = first.link;
  270.                        count--;
  271.                     }
  272.                     else                    //Case 3
  273.                     {
  274.                        trailCurrent.link = current.link;
  275.                        count--;
  276.                     }
  277.                  }
  278.                  else                   //Case 4
  279.                     System.out.println("Item to be deleted is "
  280.                                      + " not in the list.");
  281.            }
  282.         } //end else
  283.     } //end deleteNode
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement