Advertisement
Knokoutz

Assignment8

Nov 1st, 2011
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.99 KB | None | 0 0
  1. package assignment8;
  2.  
  3.    
  4. public abstract class LinkedListClass
  5. {
  6.     //definition of the node
  7.     protected class LinkedListNode
  8.     {
  9.         DataElement info;
  10.         LinkedListNode link;
  11.     }
  12.    
  13.     protected LinkedListNode first; //variable to point to the first node
  14.     protected LinkedListNode last;  //variable to point to the last node
  15.     protected int count;            //variable to store the number of nodes
  16.                                     //in the list
  17.    
  18.     //default constructor
  19.     //initializes the list to an empty state
  20.     public LinkedListClass()
  21.     {
  22.         first = null;
  23.         last = null;
  24.         count = 0;
  25.     }
  26.    
  27.     //copy constructor
  28.     //method to initialize the list to an empty state
  29.     public LinkedListClass(LinkedListClass otherList)
  30.     {
  31.         copy(otherList);
  32.     }
  33.    
  34.     //method to initialize the list to an empty state
  35.     public void initializeList()
  36.     {
  37.         first = null;
  38.         last = null;
  39.         count = 0;
  40.     }
  41.    
  42.     //method to determine whether the list is empty
  43.     public boolean isEmptyList()
  44.     {
  45.         return (first == null);
  46.     }
  47.    
  48.     //method to output the data contained in each node
  49.     public void print()
  50.     {
  51.         LinkedListNode current; //variable to traverse the list
  52.        
  53.         current = first;        //set current so that it points to
  54.                                 //the first node
  55.         while(current != null)  //while more data to print
  56.         {
  57.             System.out.println(current.info + " ");
  58.             current = current.link;
  59.         }
  60.     }
  61.    
  62.     //method to return the number of nodes in the list
  63.     public int length()
  64.     {
  65.         return count;
  66.     }
  67.    
  68.     //method to return the reference of the object containing the data of the
  69.     //first node in the list
  70.     public DataElement front()
  71.     {
  72.         DataElement temp = first.info.getCopy();
  73.         return temp;
  74.     }
  75.    
  76.     //method to return the reference of the object containing the data of the
  77.     //last node in the list
  78.     public DataElement back()
  79.     {
  80.         DataElement temp = last.info.getCopy();
  81.         return temp;
  82.     }
  83.    
  84.     //method to determine whether searchItem is in the list
  85.     public abstract boolean search(DataElement searchItem);
  86.    
  87.     //method to insert newItem in the list
  88.     public void insertFirst(DataElement newItem)
  89.     {
  90.         LinkedListNode newNode;             //variable to create the new node
  91.        
  92.         newNode = new LinkedListNode();     //create the new node
  93.         newNode.info = newItem.getCopy();   //assign a copy of the newItem
  94.                                             //to the node
  95.         newNode.link = first;               //insert newNode before first
  96.         first = newNode;                    //make first point to the
  97.                                             //actual first node
  98.         if(last == null)                    //if the list was empty, newNode is
  99.                                             //also the last node in the list
  100.             last = newNode;
  101.        
  102.         count++;
  103.     }
  104.    
  105.     //method to insert newItem at the end of the list
  106.     public void insertLast(DataElement newItem)
  107.     {
  108.         LinkedListNode newNode;             //variable to create the new node
  109.        
  110.         newNode = new LinkedListNode();     //create the new node
  111.         newNode.info = newItem.getCopy();   //assign a copy of the newItem
  112.                                             //to the node
  113.         newNode.link = first;               //insert newNode before first
  114.         first = newNode;                    //make first point to the
  115.                                             //actual first node
  116.         if(last == null)                    //if the list was empty, newNode is
  117.                                             //also the last node in the list
  118.         {
  119.             first = newNode;
  120.             last = newNode;
  121.         }
  122.         else            //if the list is not empty, insert newNode after last
  123.         {
  124.             last.link = newNode;    //insert newNode after last
  125.             last = newNode;         //set last to point to the actual last node
  126.         }
  127.        
  128.         count++;
  129.     }
  130.    
  131.     //method to delete deleteItem from the list
  132.     public abstract void deleteNode(DataElement deleteItem);
  133.    
  134.     //method to return the reference of the copy of otherList
  135.     private void copy(LinkedListClass otherList)
  136.     {
  137.         LinkedListNode newNode; //variable to create a node
  138.         LinkedListNode current; //variable to traverse the list
  139.        
  140.         first = null;   //make this list empty
  141.        
  142.         if(otherList.first == null) //otherList is empty
  143.         {
  144.             first = null;
  145.             last = null;
  146.             count = 0;
  147.         }
  148.         else
  149.         {
  150.             count = otherList.count;
  151.             current = otherList.first;  //current points to the list
  152.                                         //to be copied
  153.            
  154.             //copy the first element
  155.             first = new LinkedListNode();           //create the node
  156.             first.info = current.info.getCopy();    //copy the info
  157.             first.link = null;  //set the link field of the node to null
  158.             current = current.link; //make current point to the next node of
  159.                                     //the list being copied
  160.            
  161.             //copy the remaining list
  162.             while(current != null)
  163.             {
  164.                 newNode = new LinkedListNode();
  165.                 newNode.info = current.info.getCopy();
  166.                 newNode.link = null;
  167.                 last.link = newNode;
  168.                 last = newNode;
  169.                 current = current.link;
  170.             }
  171.         }
  172.     }
  173.    
  174.     //method to return the reference of the copy of otherList
  175.     public void copyList(LinkedListClass otherList)
  176.     {
  177.         if(this != otherList)       //avoid self-copy
  178.             copy(otherList);
  179.     }
  180.    
  181.     public void splitMid(LinkedListClass otherList)
  182.     {
  183.         otherList = new UnorderedLinkedList();
  184.         LinkedListNode current;
  185.         LinkedListNode current2;
  186.         if (count == 0)
  187.             System.out.println("The list is null.");
  188.         else
  189.         {
  190.             int newLength = count / 2;
  191.             current2 = first;
  192.             if (count % 2 == 0)
  193.                 for(int i = 0; i<newLength; i++)
  194.                 {
  195.                     current2 = current2.link;
  196.                 }
  197.             else
  198.                 for(int i = 0; i<newLength+1; i++)
  199.                 {
  200.                     current2 = current2.link;
  201.                 }
  202.            
  203.             current = first;
  204.             if (count % 2 == 0)
  205.                 while(current != current2)
  206.            
  207.                 {
  208.                     this.deleteNode(first.info);
  209.                     this.deleteNode(first.info);
  210.                     this.insertLast(current.info);
  211.                     current = current.link;
  212.                 }
  213.             else
  214.             {
  215.                 while(current != current2)
  216.                 {
  217.                     this.deleteNode(first.info);
  218.                     this.insertLast(current.info);
  219.                     current = current.link;
  220.                    
  221.                 }
  222.                 int nC = count / 2 - 1;
  223.                
  224.                 for(int i = 0; i <= nC; i++)
  225.                 {
  226.                     this.deleteNode(first.info);
  227.                 }
  228.                
  229.             }
  230.            
  231.             while(current2 != null)
  232.             {
  233.                 otherList.insertLast(current2.info);
  234.                 current2 = current2.link;
  235.             }
  236.         }
  237.     }  
  238.  
  239. }
  240.  
  241.  
  242. ------------------------------------------------------------------------------
  243.  
  244. package assignment8;
  245.  
  246.  
  247. public class UnorderedLinkedList extends LinkedListClass
  248. {
  249.     protected LinkedListNode first; //variable to point to the first node
  250.     protected LinkedListNode last;  //variable to point to the last node
  251.     protected int count;            //variable to store the number of nodes
  252.                                     //in the list
  253.    
  254.     //default constructor
  255.     public UnorderedLinkedList()
  256.     {
  257.         super();
  258.     }
  259.    
  260.     //copy constructor
  261.     public UnorderedLinkedList(UnorderedLinkedList otherList)
  262.     {
  263.         super(otherList);
  264.     }
  265.    
  266.     //method to determine whether searchItem is in the list
  267.     public boolean search(DataElement searchItem)
  268.     {
  269.         LinkedListNode current; //variable to traverse the list
  270.         boolean found;
  271.        
  272.         current = first;    //set current to point to the first node in the list
  273.        
  274.         found = false;      //set found to false
  275.        
  276.         while(current != null && !found)    //search the list
  277.             if(current.info.equals(searchItem)) //item is found
  278.                 found = true;
  279.             else
  280.                 current = current.link;     //make the current point to
  281.                                             //the next node
  282.         return found;
  283.     }
  284.    
  285.     //method to delete deleteItem from the list
  286.     public void deleteNode(DataElement deleteItem)
  287.     {
  288.         LinkedListNode current;         //variable to traverse the list
  289.         LinkedListNode trailCurrent;    //variable just before current
  290.         boolean found;
  291.        
  292.         if(first == null)       //Case 1; the list is empty
  293.             System.err.println("Cannot delete from an empty list.");
  294.         else
  295.         {
  296.             if(first.info.equals(deleteItem))   //Case 2
  297.             {
  298.                 first = first.link;
  299.                
  300.                 if(first == null)       //the list had only one node
  301.                     last = null;
  302.                 count--;
  303.             }
  304.             else    //search the list for the node with the given info
  305.             {
  306.                 found = false;
  307.                 trailCurrent = first;   //set trailCurrent to point to
  308.                                         //the first node
  309.                 current = first.link;   //set current to point to the
  310.                                         //second node
  311.                
  312.                 while(current != null && !found)
  313.                 {
  314.                     if(current.info.equals(deleteItem))
  315.                         found = true;
  316.                     else
  317.                     {
  318.                         trailCurrent = current;
  319.                         current = current.link;
  320.                     }
  321.                 }
  322.                
  323.                 if(found)   //Case 3; if found, delete the node
  324.                 {
  325.                     count--;
  326.                     trailCurrent.link = current.link;
  327.                    
  328.                     if(last == current)         //node to be delete was the
  329.                                                 //last node
  330.                         last = trailCurrent;    //update the value of last
  331.                 }
  332.                 else
  333.                     System.out.println("Item to be delete is not in the list.");
  334.             }
  335.         }
  336.     }
  337.  
  338.    
  339.    
  340. }
  341.  
  342.  
  343. ------------------------------------------------------------------------------
  344.  
  345. package assignment8;
  346.  
  347.  
  348. import java.io.*;
  349. import java.util.*;
  350.  
  351. public class Test
  352. {
  353. static BufferedReader keyboard = new
  354.     BufferedReader (new InputStreamReader(System.in));
  355.    
  356.     public static void main(String[] args) throws IOException
  357.     {
  358.         UnorderedLinkedList myList = new UnorderedLinkedList();
  359.         UnorderedLinkedList otherList = new UnorderedLinkedList();
  360.                
  361.         IntElement num = new IntElement();
  362.         StringTokenizer tokenizer;
  363.        
  364.         System.out.print("Enter a list of integers ending with -999: ");
  365.        
  366.         tokenizer = new StringTokenizer(keyboard.readLine());
  367.         num.setNum(Integer.parseInt(tokenizer.nextToken()));
  368.        
  369.         while (num.getNum() != -999)
  370.         {
  371.             myList.insertLast(num);
  372.             num.setNum(Integer.parseInt(tokenizer.nextToken()));
  373.         }
  374.        
  375.         System.out.println("The length of your list is: " + myList.length());
  376.         System.out.println("Your list is: ");
  377.         myList.print();
  378.         myList.splitMid(otherList);
  379.         System.out.println("\n" + "The length of your list is now: " + myList.length());
  380.         System.out.println("Your list after splitting is now: ");
  381.         myList.print();
  382.         System.out.println("\n" + "The length of your new list is: " + otherList.length());
  383.         System.out.println("Your new list after splitting is: ");
  384.         otherList.print();
  385.     }
  386.  
  387. }
  388.  
  389.  
  390.  
  391. ------------------------------------------------------------------------------
  392.  
  393. package assignment8;
  394.  
  395.  
  396. public class IntElement extends DataElement
  397. {
  398.     protected int num;
  399.    
  400.     //default constructor
  401.     public IntElement()
  402.     {
  403.         num = 0;
  404.     }
  405.    
  406.     //constructor with parameters
  407.     public IntElement(int x)
  408.     {
  409.         num = x;
  410.     }
  411.    
  412.     //copy constructor
  413.     public IntElement(IntElement otherElement)
  414.     {
  415.         num = otherElement.num;
  416.     }
  417.    
  418.     //method to set the value of the instance variable num
  419.     //postcondition: num = x;
  420.     public void setNum(int x)
  421.     {
  422.         num = x;
  423.     }
  424.    
  425.     //method to return the value of the instance variable num
  426.     //postcondition: the value of num is returned
  427.     public int getNum()
  428.     {
  429.         return num;
  430.     }
  431.    
  432.     @Override
  433.     public boolean equals(DataElement otherElement)
  434.     {
  435.         IntElement temp = (IntElement) otherElement;
  436.         return(num == temp.num);
  437.     }
  438.    
  439.     @Override
  440.     public int compareTo(DataElement otheElement)
  441.     {
  442.         IntElement temp = (IntElement) otheElement;
  443.         return(num = temp.num);
  444.     }
  445.    
  446.     @Override
  447.     public void makeCopy(DataElement otherElement)
  448.     {
  449.         IntElement temp = (IntElement) otherElement;
  450.         num = temp.num;
  451.     }
  452.    
  453.     @Override
  454.     public DataElement getCopy()
  455.     {
  456.         IntElement temp = new IntElement(num);
  457.         return temp;
  458.     }
  459.    
  460.     @Override
  461.     public String toString()
  462.     {
  463.         return String.valueOf(num);
  464.     }
  465. }
  466.  
  467. ------------------------------------------------------------------------------
  468.  
  469.  
  470. package assignment8;
  471.  
  472.  
  473. public abstract class DataElement
  474. {    
  475.     public abstract boolean equals(DataElement otherElement);
  476.    
  477.     public abstract int compareTo(DataElement otherElement);
  478.    
  479.     public abstract void makeCopy(DataElement otherElement);
  480.    
  481.     public abstract DataElement getCopy();
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement