daily pastebin goal
16%
SHARE
TWEET

lab11/17/2017

DennisBoone2020 Nov 17th, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ******************************************************************************************************************************
  2. package linkedlist;
  3.  
  4. /**
  5.  * A Simple Linked List implementation of int values Adapted from textbook
  6.  * @author Julie Anderson, M Raunak
  7.  * @version 1.0 2/1/2014
  8.  */
  9.  
  10. public class IntegerLinkedList {
  11.    
  12.     private IntegerNode _head;
  13.     private int _numOfItems;
  14.  
  15.     /**
  16.      * default (no-arg) constructor. Constructs an empty list
  17.      */
  18.     public IntegerLinkedList() {
  19.         _head = null;
  20.         _numOfItems = 0;
  21.     }
  22.  
  23.     /**
  24.      * Accessor for numberOfItems
  25.      *
  26.      * @return numberOfItmes
  27.      */
  28.     public int getNumberOfItems() {
  29.         return _numOfItems;
  30.     }
  31.  
  32.     /**
  33.      * insert method
  34.      *
  35.      * @param value data to insert inserts node at head
  36.      */
  37.     public void insert(int value) {
  38.        
  39.         System.out.println("Inserting " + value);
  40.         // step 1: construct a new node
  41.         IntegerNode nd = new IntegerNode(value);
  42.  
  43.         // step 2: update the links
  44.  
  45.         // whatever _head was referring to
  46.         // will now be referred to by the new node = _head;
  47.        
  48.         nd.setNext(_head); // essentially nd.next
  49.    
  50.         // make _head now point to this newly created  node (IntegerNode type of object)
  51.         _head = nd;
  52.        
  53.         // increment the number of items
  54.         _numOfItems++;
  55.     }
  56.  
  57.        
  58.     /**
  59.      * delete method
  60.      *
  61.      * @param value  the value to delete
  62.      * @return true if value was deleted, false otherwise
  63.      */
  64.     public boolean delete(int value) {
  65.        
  66.         System.out.println("Attempting to delete " + value);
  67.        
  68.         IntegerNode current = _head;
  69.         IntegerNode previous = null;
  70.         // loop through the node
  71.         while (current != null && current.getData() != value) {
  72.             previous = current;
  73.             current = current.getNext();
  74.         }
  75.  
  76.         // if current is null, that means that we have come to the end  
  77.         // of the list i.e., we did not find the node we were looking for.
  78.         if (current == null) // not found
  79.             return false;
  80.         else { // the item to delete has been found
  81.             if (current == _head) // if the item to be deleted is the head             
  82.                 _head = _head.getNext(); // delete head
  83.             else
  84.                 previous.setNext(current.getNext());
  85.  
  86.             _numOfItems--;
  87.             return true;
  88.         }
  89.     }
  90.  
  91.     /**
  92.      * toString
  93.      *
  94.      * @return values in list separated by a space
  95.      */
  96.     public String toString() {
  97.         String listString = "";
  98.         IntegerNode currentNode = _head;
  99.         for (int i = 0; i < _numOfItems; i++) {
  100.             listString += currentNode.getData() + "  ";
  101.             currentNode = currentNode.getNext();
  102.         }
  103.         return listString;
  104.     }
  105.    
  106.     public boolean insertAtEnd (int value) {
  107.         IntegerNode current = _head;
  108.         IntegerNode valueNode= new IntegerNode(value);
  109.          
  110.        
  111.         while (current.getNext() != null) {
  112.             current = current.getNext();
  113.     }  
  114.             System.out.println("Inserting " + value);
  115.                    
  116.             current.setNext(valueNode);
  117.             _numOfItems++;
  118.             return true;
  119.            
  120.         }
  121. }
  122. ******************************************************************************************************************************
  123.  
  124. package linkedlist;
  125.  
  126. /**
  127.  * Represents a linked list Node of an int value Adapted from the text book.
  128.  * @author Julie Anderson, M Raunak
  129.  * @version 1.0 2/1/2014
  130.  */
  131.  
  132. public class IntegerNode {
  133.  
  134.     private int _data;
  135.     private IntegerNode _nextNode;
  136.    
  137.     /**
  138.      * default constructor
  139.      * Sets _data to 0, and _next to null
  140.      */
  141.     public IntegerNode() {
  142.         _data = 0;
  143.         _nextNode = null;
  144.        
  145.     }
  146.  
  147.     /**
  148.      * Overloaded constructor
  149.      * @param data data value
  150.      */
  151.     public IntegerNode(int data) {
  152.         _data = data;
  153.         _nextNode = null;
  154.     }
  155.  
  156.     /**
  157.      * accessor (getter) for data
  158.      * @return the value of the node
  159.      */
  160.     public int getData() {
  161.         return _data;
  162.     }
  163.  
  164.     /**
  165.      * accessor (getter) for _next
  166.      *
  167.      * @return the reference to the next node
  168.      */
  169.     public IntegerNode getNext() {
  170.         return _nextNode;
  171.     }
  172.  
  173.     /**
  174.      * mutator (setter) for _data
  175.      *
  176.      * @param newData the new value for the node
  177.      */
  178.     public void setData(int newData) {
  179.         _data = newData;
  180.     }
  181.  
  182.     /**
  183.      * mutator(setter) for reference variable _next
  184.      *
  185.      * @param nextNode the new value for _next
  186.      */
  187.     public void setNext(IntegerNode nextNode) {
  188.         _nextNode = nextNode;
  189.     }
  190. }
  191. ********************************************************************************************************************************
  192.  
  193. package linkedlist;
  194. import java.io.PrintWriter;
  195. import java.util.Scanner;
  196.  
  197. /**
  198.  * Uses and tests the IntegerLinkedList
  199.  *
  200.  * @author Anderson, Franceschi
  201.  * @author Mohammad Raunak
  202.  */
  203.  
  204. public class IntegerLinkedListTest {
  205.  
  206.     public static void main(String[] args) {
  207.  
  208.         // construct empty IntegerLinkedList
  209.         IntegerLinkedList numbers = new IntegerLinkedList();
  210.         printItems(numbers);
  211.        
  212.         numbers.insert(2);
  213.         numbers.insert(6);
  214.         numbers.insert(3);
  215.         numbers.insertAtEnd(12);
  216.         printItems(numbers);
  217.     /** numbers.insert(7); // insert in empty list
  218.         printItems(numbers);
  219.        
  220.         numbers.insert(2); // insert in list with one item
  221.         printItems(numbers);
  222.        
  223.        
  224.         numbers.insert(5); // insert in list with two items
  225.         printItems(numbers);
  226.        
  227.         if (!numbers.delete(8)) // unsuccessful - not in list
  228.             System.out.println("8 could not be deleted:");
  229.         printItems(numbers);
  230.        
  231.         if (numbers.delete(2)) // successful
  232.             System.out.println("2 was successfully deleted:");
  233.         printItems(numbers);
  234.        
  235.         if (numbers.delete(7)) // successful
  236.             System.out.println("7 was successfully deleted:");
  237.         printItems(numbers);
  238.        
  239.         if (numbers.delete(5)) // successful
  240.             System.out.println("5 was successfully deleted:");
  241.         printItems(numbers);
  242.        
  243.         if (!numbers.delete(8)) // unsuccessful - empty list
  244.             System.out.println("8 could not be deleted:");
  245.         printItems(numbers);
  246.         */
  247.         numbers.insertAtEnd(60);
  248.         printItems(numbers);
  249.        
  250.     }
  251.    
  252.    
  253.     public static void printItems(IntegerLinkedList linkedListOfInts){
  254.        
  255.         System.out.println("Number of items in the list: "
  256.                 + linkedListOfInts.getNumberOfItems() + " ==> " + linkedListOfInts.toString());
  257.  
  258.     }
  259.    
  260.  
  261.     private String getOutputFileName() {
  262.         Scanner scan = new Scanner(System.in);
  263.             System.out.println("Please enter the name of THE FILE TO WRITE OUTPUT into");
  264.             return scan.nextLine();
  265.     }
  266.    
  267. public void writeIntoTextFile() {
  268.        
  269.         String outFileName = getOutputFileName();
  270.        
  271.         try {
  272.             // Create a PrintWriter object using the FileOutputStream and File
  273.             PrintWriter pw = new PrintWriter(new FileOutputStream(new File(outFileName)));
  274.            
  275.             System.out.println("The Data that is getting written in file " + outFileName);
  276.             for (PresidentInfo presInfoObj : presList) {
  277.                 pw.println( presInfoObj ); // the presInfoObj.toString() will get invoked
  278.                 System.out.println( presInfoObj );
  279.             }
  280.             pw.close(); // close the file resources
  281.             System.out.println("PresidentInfo list has been successfully written to:" + outFileName);
  282.         } catch (FileNotFoundException fnfe) {
  283.            
  284.             fnfe.printStackTrace();
  285.            
  286.         }
  287.  
  288. }
  289. ********************************************************************************************************************************************************
RAW Paste Data
Top