Advertisement
ZavarelliJace

Programs (4/7/2020) Jace Zavarelli

Apr 7th, 2020
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.02 KB | None | 0 0
  1. //  Jace Zavarelli
  2. //  4/7/2020
  3.  
  4. //                  This is the Linked List Program that is used throughout many of the other assignments: such as Stacks and Queue's
  5.  
  6. class LinkedList
  7. {
  8.     //reference to the head node.
  9.     private Node head;
  10.     private int listCount;
  11.    
  12.     //LinkedList constructor
  13.     public LinkedList()
  14.     {
  15.         //this is an empty list, so the reference to the head node is set to a new node with no data
  16.         head = new Node(null);
  17.         listCount = 0;
  18.     }
  19.    
  20.     public void add(Object data)
  21.     {
  22.         //appends the specified element to the end of this list.
  23.         Node temp = new Node(data);
  24.         Node current  = head;
  25.         //starting at the head node, crawl to the end of the list,
  26.        
  27.         while(current.getNext() != null)
  28.         {
  29.             current = current.getNext();
  30.         }
  31.        
  32.         //The last node's "next" reference set to our new node
  33.         current.setNext(temp);
  34.         //increment the number of elements variable
  35.         listCount++;
  36.     }
  37.    
  38.     public void add(Object data, int index)
  39.     {
  40.         //inserts the specified element at the specified position in this list.
  41.         Node temp = new Node(data);
  42.         Node current = head;
  43.        
  44.         //crawl to the requested index or the last element in the list, whichever comes first
  45.         for(int i = 1; i < index && current.getNext() != null; ++i)
  46.         {
  47.             current = current.getNext();
  48.         }
  49.         //Set the new node's next-node reference to this node's next-node reference
  50.         temp.setNext(current.getNext());
  51.         //Now set this node's next-node reference to the new node
  52.         current.setNext(temp);
  53.         //increment the number of elements variable
  54.         listCount++;
  55.     }
  56.    
  57.     public Object get(int index)
  58.     {
  59.         //returns the element at the specified position in this list.
  60.        
  61.         //index must be 1 or higher
  62.         if(index <= 0)
  63.             return null;
  64.        
  65.         Node current = head.getNext();
  66.         for(int i = 1; i < index; i++)
  67.         {
  68.             if(current.getNext() == null)
  69.                 return null;
  70.            
  71.             current = current.getNext();
  72.         }
  73.        
  74.         return current.getData();
  75.     }
  76.    
  77.     public boolean remove(int index)
  78.     {
  79.         //removes element at specified position in list.
  80.        
  81.         //if index out of range, exit
  82.         if(index < 1 || index > size())
  83.             return false;
  84.        
  85.         Node current = head;
  86.         for(int i = 1; i < index; ++i)
  87.         {
  88.             if(current.getNext() == null)
  89.                 return false;
  90.            
  91.             current = current.getNext();
  92.         }
  93.        
  94.         current.setNext(current.getNext().getNext());
  95.         //decrement the number of elements variable
  96.         listCount--;
  97.         return true;
  98.     }
  99.    
  100.     public int size()
  101.     {
  102.         return listCount;
  103.     }
  104.    
  105.     public String toString()
  106.     {
  107.         Node current = head.getNext();
  108.         String output = "";
  109.        
  110.         while(current != null)
  111.         {
  112.             output += "[" + current.getData().toString() + "]";
  113.             current = current.getNext();
  114.         }
  115.        
  116.         return output;
  117.     }
  118.    
  119.     private class Node
  120.     {
  121.         //reference to the next Node in the chain, or null if there isn't one.
  122.         Node next;
  123.         //Data carried by this node could be of any type you need.
  124.         Object data;
  125.        
  126.         //Node Constructor
  127.         public Node(Object _data)
  128.         {
  129.             next = null;
  130.             data = _data;
  131.         }
  132.        
  133.         @SuppressWarnings("unused")
  134.         public Node(Object _data, Node _next)
  135.         {
  136.             next = _next;
  137.             data = _data;
  138.         }
  139.        
  140.         public Object getData()
  141.         {
  142.             return data;
  143.         }
  144.        
  145.         @SuppressWarnings("unused")
  146.         public void setData(Object _data)
  147.         {
  148.             data = _data;
  149.         }
  150.        
  151.         public Node getNext()
  152.         {
  153.             return next;
  154.         }
  155.        
  156.         public void setNext(Node _next)
  157.         {
  158.             next = _next;
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164.  
  165. //                                                                              This is the QUEUE class and assignment for Queue's@h@
  166. public class Queue
  167. {
  168.     private LinkedList list;
  169.    
  170.     //Queue Constructor
  171.     public Queue()
  172.     {
  173.         //Create a new LinkedList;
  174.         list = new LinkedList();
  175.     }
  176.    
  177.     public boolean isEmpty()
  178.     {
  179.         //Post: Returns true if the queue is empty. Otherwise it is false.
  180.         return(list.size() == 0);
  181.     }
  182.    
  183.     public void enqueue(Object item)
  184.     {
  185.         //Post: An item is added to the back of the queue.
  186.        
  187.         //Append the item to the end of our linked list.
  188.         list.add(item);
  189.     }
  190.    
  191.     public Object dequeue()
  192.     {
  193.         //Preview: this.isEmpty() == false
  194.         //Post: The item at the front of the queue is returned and deleted from the queue.
  195.         //  Returns null if precondition not met.
  196.        
  197.         //Store a reference to the item at the front of the queue so that it does not get garbage
  198.         //  collected when we remove it from the list.
  199.         //Note: list.get(...) returns null if item not found at specified index. See postcondition.
  200.         Object item = list.get(1);
  201.        
  202.         //Remove the item from the list. My implementation of the linked list is based on the J2SE API
  203.         // reference. In both, elements start at 1, unlike arrays which start at 0.
  204.         list.remove(1);
  205.        
  206.         //return item
  207.         return item;
  208.     }
  209.    
  210.     public Object peek()
  211.     {
  212.         //Preview: this.isEmpty() == false
  213.         //Post: The item at the front of the queue is returned and deleted from the queue.
  214.         //  Returns null if precondition not met.
  215.        
  216.         //This method is very similar to dequeue(). See Queue.dequeue() for comments.
  217.         return list.get(1);
  218.     }
  219. }
  220.  
  221.  
  222. //                                              This goes along with the QUEUE class and is used to accompany the Queue's assignment@h@
  223. public class Person
  224. {
  225.     String name;
  226.    
  227.     //Person Constructor
  228.     public Person(String _name)
  229.     {
  230.         //Post: An instance of Person is initialized.
  231.        
  232.         //Set the name of this person object to the name passed into the constructor.
  233.         name = _name;
  234.     }
  235.    
  236.     public String getName()
  237.     {
  238.         //Post: The name of this person is returned.
  239.        
  240.         return name;
  241.     }
  242.    
  243.     //End of Person Class
  244. }
  245.  
  246.  
  247.  
  248. //                                                                  This is the code for STACKS or for the Stack Assignment@h@
  249. import java.util.LinkedList;
  250.  
  251. public class Stack
  252. {
  253.     private LinkedList<Object> list;
  254.    
  255.     //Stack Constructor
  256.     public Stack()
  257.     {
  258.         //Create a new LinkedList
  259.         list = new LinkedList<Object>();
  260.     }
  261.    
  262.     public boolean isEmpty()
  263.     {
  264.         //Post: Returns true if the stack is empty. Otherwise it is false;
  265.         return(list.size() == 0);
  266.     }
  267.    
  268.     public void push(Object item)
  269.     {
  270.         //Post: An item is added to the back of the stack
  271.        
  272.         //Append the item to the end of our linked list.
  273.         list.add(item);
  274.     }
  275.    
  276.     public Object pop()
  277.     {
  278.         //Pre: this.isEmpthy() == false
  279.         //Post:The item at the front of the stack is returned and deleted from the stack.
  280.         //      Returns null if precondition not met.
  281.        
  282.         //Store a reference to the item at the front of the stack so that it does not get garbage
  283.         //  collected when we remove it from the list.
  284.         //Note: list.get(...) returns null if item not found at specified index. See Postconditions.
  285.         Object item = list.get(list.size());
  286.        
  287.         //Remove the item from the list.
  288.         //My implementation of the linked list is based on the J2SE API reference. In both, elements
  289.         //  start at 1, unlike arrays which start at 0.
  290.         list.remove(list.size());
  291.        
  292.         //return item
  293.         return item;
  294.     }
  295.    
  296.     public Object peek()
  297.     {
  298.         //Pre: this.isEmpthy() == false
  299.         //Post:The item at the front of the stack is returned and deleted from the stack.
  300.         //      Returns null if precondition not met.
  301.        
  302.         //This  method is very similar to pop(). See Stack.pop() for comments.
  303.         return list.get(list.size());
  304.                
  305.     }
  306. }
  307.  
  308.  
  309.  
  310. //                                                      This is Indexed List, an Interface, which is used in SLIndexed List@h@
  311.  
  312. public interface IndexedList
  313. {
  314.     public void add(int index, Object obj);
  315.    
  316.     public Object get(int index);
  317.    
  318.     public boolean isEmpty();
  319.    
  320.     public boolean isFull();
  321.    
  322.     public Object remove(int index);
  323.    
  324.     public Object set(int index, Object obj);
  325.    
  326.     public int size();
  327.    
  328.     public String toString();
  329. }
  330.  
  331.  
  332.  
  333. //                                                                                  This is SLIndexed List@h@
  334.  
  335. import java.io.*; //for Serialization
  336. import javax.swing.*;
  337. import java.util.*; //for ListIterator
  338.  
  339. @SuppressWarnings("unused")
  340. public class SLIndexedListCode implements IndexedList, Serializable
  341. {
  342.     private static final long serialVersionUID = 1L;
  343.  
  344.     private Node head;          //pointer to first node
  345.     private int listSize;       //list size
  346.     private Node nodeAtIndex;   //The node at index position or null if past the end of the list
  347.     private Node nodeBefore;    //The node before index position or null if before the beginning of the list
  348.    
  349.     @SuppressWarnings("unused")
  350.     private static Node pointer;
  351.    
  352.     public SLIndexedListCode()
  353.     {
  354.         head = new Node("!", null);
  355.         head = new Node("ball", head);
  356.         head = new Node("the", head);
  357.         head = new Node("Throw", head);
  358.         listSize = 4;
  359.     }
  360.    
  361.     public void add(int index, Object obj)
  362.     {
  363.         if(index < 0 || index >= listSize)
  364.         {
  365.             throw new RuntimeException("Index =" + index + " is out of the list bounds");
  366.         }
  367.        
  368.         if(index == 0)
  369.         {
  370.             head = new Node(obj, head);
  371.         }
  372.         else
  373.         {
  374.             locateNode(index);
  375.             nodeBefore.next = new Node(obj, nodeAtIndex);
  376.         }
  377.        
  378.         listSize++;
  379.     }
  380.    
  381.     public boolean isEmpty()
  382.     {
  383.         return listSize == 0;
  384.     }
  385.    
  386.     public boolean isFull()
  387.     {
  388.         return false;
  389.     }
  390.    
  391.     public Object get(int index)
  392.     {
  393.         if(index < 0 || index >= listSize)
  394.         {
  395.             throw new RuntimeException("Index = " + index + " is out of the list bounds");
  396.         }
  397.        
  398.         locateNode(index);
  399.         return nodeAtIndex.value;
  400.     }
  401.    
  402.     public Object remove(int index)
  403.     {
  404.         if(index < 0 || index >= listSize)
  405.         {
  406.             throw new RuntimeException("Index = " + index + " is out of the list bounds");
  407.         }
  408.        
  409.         Object removedObj = null;
  410.        
  411.         if(index == 0)
  412.         {
  413.             removedObj = head.value;
  414.             head = head.next;
  415.         }
  416.         else
  417.         {
  418.             locateNode(index);
  419.             nodeBefore.next = nodeAtIndex.next;
  420.             removedObj = nodeAtIndex.value;
  421.         }
  422.        
  423.         listSize--;
  424.         return removedObj;
  425.     }
  426.    
  427.     public Object set(int index, Object obj)
  428.     {
  429.         Object replacedObj = null;
  430.        
  431.         try
  432.         {
  433.             if(index == 0)
  434.             {
  435.                 replacedObj = head.value;
  436.                 head.value = obj;
  437.             }
  438.             else
  439.             {
  440.                 locateNode(index);
  441.                 replacedObj = nodeAtIndex.value;
  442.                 nodeAtIndex.value = obj;
  443.             }
  444.         }
  445.         catch(RuntimeException e)
  446.         {
  447.             JOptionPane.showMessageDialog(null, "Index = " + index + " is out of the list bounds",
  448.                     "Out of Bounds Error", JOptionPane.ERROR_MESSAGE);
  449.         }
  450.        
  451.         return replacedObj;
  452.     }
  453.    
  454.     public int size()
  455.     {
  456.         return listSize;
  457.     }
  458.    
  459.     public String toString()
  460.     {
  461.         String message = "";
  462.        
  463.         for(Node pointer = head; pointer != null; pointer = pointer.next)
  464.         {
  465.             message += pointer.value + " ";
  466.         }
  467.        
  468.         return message;
  469.     }
  470.  
  471.     private void locateNode(int index)
  472.     {
  473.         nodeBefore = null;
  474.         nodeAtIndex = head;
  475.        
  476.         for(int i = 1; i < listSize && i <= index; i++)
  477.         {
  478.             nodeBefore = nodeAtIndex;
  479.             nodeAtIndex = nodeAtIndex.next;
  480.         }
  481.        
  482.         if(index == listSize)
  483.         {
  484.             nodeBefore = nodeAtIndex;
  485.             nodeAtIndex = null;
  486.         }
  487.     }
  488.    
  489.     private class Node
  490.     {
  491.         private Object value;
  492.         private Node next;
  493.        
  494.         private Node()
  495.         {
  496.             value = null;
  497.             next = null;
  498.         }
  499.        
  500.         private Node(Object value, Node next)
  501.         {
  502.             this.value = value;
  503.             this.next = next;
  504.         }
  505.     }
  506.    
  507.     public static void main(String[] args)
  508.     {
  509.         SLIndexedListCode list = new SLIndexedListCode();   // 1) Singly Linked List
  510.        
  511.         pointer = list.head;    // 2) Sets point to the head of list
  512.        
  513.         list.add(2, "yellow");  // 4.1) Sets the 3rd node( 2nd Index ) to the string yellow
  514.         list.set(2, "blue");    // 5) Changes 3rd node to blue
  515.        
  516.        
  517.         list.remove(3); // 6) removed 4th node ( 3rd Index ) of the list
  518.            
  519.        
  520.         list.set(4, "Football"); // 8.2) Should return as error when active. The If statement will show list size in the program.
  521.         if(list.size() < 5)
  522.         {
  523.             System.out.println("The List Size is " + list.size());
  524.             System.out.println();
  525.         }
  526.    
  527.        
  528.         for(int i = 0; i < 4; i++)  // 3.1) For Loop that shows the list
  529.         {
  530.             list.toString();    // 4.2) Uses to string to add the yellow string to the phrase
  531.            
  532.             System.out.println(list.get(i));    // 3.2) String prints out each part of looped list
  533.         }
  534.        
  535.        
  536.         if(list.isEmpty()) // 8.1) If the list is empty it will show in this if statement
  537.         {
  538.             System.out.println();
  539.             System.out.println("The List is Empty");
  540.         }
  541.         else
  542.         {
  543.             System.out.println();
  544.             System.out.println("The List is Not Empty");
  545.         }
  546.     }
  547. }
  548.  
  549.  
  550.  
  551. //                                                              This is the program DLPositionalListCode@h@
  552.  
  553. import java.util.*; //for ListIterator
  554. import java.io.*; //for Serialization
  555. import javax.swing.*; //for JOptionPane
  556.  
  557. public class DLPositionalListCode implements ListIterator<Object>, Serializable
  558. {
  559.     private static final long serialVersionUID = 1L;
  560.    
  561.     private Node head;
  562.     private Node curPos;
  563.     private Node lastItemPos;
  564.     private int listSize;
  565.     private String removeItem;
  566.    
  567.     public DLPositionalListCode()
  568.     {
  569.         head = new Node(null, null, null);
  570.         head.next = head;
  571.         head.previous = head;
  572.         curPos = head;
  573.         lastItemPos = null;
  574.         listSize = 0;
  575.     }
  576.    
  577.     //LIST ITERATOR METHODS
  578.    
  579.     //Inserts the specified element into the list (Optional Operation).
  580.     public void add(Object obj)
  581.     {
  582.         //create new node for object
  583.         Node newNode = new Node(obj, curPos.previous, curPos);
  584.        
  585.         //sets newNode.previous to last item in list and newNode.next to head that points at first item
  586.         curPos.previous.next = newNode; //sets last item's next to newNode
  587.         curPos.previous = newNode;      //sets head's previous to new node
  588.        
  589.         listSize++;
  590.         lastItemPos = null;
  591.     }
  592.    
  593.     //curPos points at head each time
  594.     public void addAfter(Object obj)
  595.     {
  596.         //adds at end of linked list
  597.        
  598.         //create new node for object
  599.        
  600.         Node newNode = new Node(obj, curPos.previous, curPos);
  601.         //sets newNode.previous to last item in list and newNode.next to head that points at first item
  602.         curPos.previous.next = newNode; //sets last item's next to newNode
  603.         curPos.previous = newNode;      //sets head's previous to new node
  604.        
  605.         listSize++;
  606.         lastItemPos = null;
  607.     }
  608.    
  609.     //moves curPos to newest added node
  610.     public void addAfter2(Object obj)
  611.     {
  612.         //adds at end of linked list
  613.        
  614.         //create new node for object
  615.        
  616.         Node newNode = new Node(obj, curPos, curPos.next);
  617.         //sets newNode.previous to head and newNode.next to new 2nd item in list
  618.         curPos.next = newNode;              //sets next of node curPos is pointing to new Node
  619.         newNode.next.previous = newNode; //sets head's previous to new node
  620.        
  621.         curPos = curPos.next;
  622.         listSize++;
  623.         lastItemPos = null;
  624.     }
  625.    
  626.     //curPos points at head each time
  627.     public void addBefore(Object obj)
  628.     {
  629.         //add at beginning of the list
  630.        
  631.         //create new node for object
  632.        
  633.         Node newNode = new Node(obj, curPos, curPos.next);
  634.         //sets newNode.previous to head and newNode.next to new 2nd item in list
  635.         curPos.next.previous = newNode; //sets 2nd item previous to newNode
  636.         curPos.next = newNode;          //points head.next to newNode
  637.        
  638.         listSize++;
  639.         lastItemPos = null;
  640.     }
  641.    
  642.     //curPos points to newest added node
  643.     public void addBefore2(Object obj)
  644.     {
  645.         //add at beginning of the list
  646.        
  647.         //create new node for object
  648.        
  649.         Node newNode = new Node(obj, curPos.previous, curPos);
  650.         //sets newNode.previous to left of curPos and newNode.next to curPos
  651.         curPos.previous = newNode; //sets 2nd item in previous to newNode
  652.         newNode.next.previous = newNode; //points the head.next to newNode
  653.        
  654.         curPos = curPos.previous;
  655.         listSize++;
  656.         lastItemPos = null;
  657.     }
  658.    
  659.     //Returns true if this list iterator has more elements when traversing the list in the forward direction
  660.     public boolean hasNext()
  661.     {
  662.         if(curPos.next == head)
  663.         {
  664.             return false;
  665.         }
  666.         else
  667.         {
  668.             return true;
  669.         }
  670.     }
  671.    
  672.     //Returns true if this list iterator has more elements when traversing the list in the reverse direction
  673.     public boolean hasPrevious()
  674.     {
  675.         if(curPos.previous == head)
  676.         {
  677.             return false;
  678.         }
  679.         else
  680.         {
  681.             return true;
  682.         }
  683.     }
  684.    
  685.     //Returns the next element in the list
  686.     public Object next()
  687.     {
  688.         if(!hasNext())
  689.         {
  690.             System.out.println("You are at the end of the list. The first item will be returned:");
  691.         }
  692.        
  693.         curPos = curPos.next;
  694.        
  695.         return curPos.previous.value;
  696.     }
  697.    
  698.     //Returns the index of the element that would be returned by a subsequent call to next
  699.     public int nextIndex()
  700.     {
  701.         //(+1) to account for next
  702.         return currentIndex() + 1;
  703.     }
  704.    
  705.     //Returns the previous element in the list
  706.     public Object previous()
  707.     {
  708.         if(!hasPrevious())
  709.         {
  710.             System.out.println("You are at the beginning of the list. The last item will be returned:");
  711.         }
  712.        
  713.         //moves current position
  714.         curPos = curPos.previous;
  715.        
  716.         return curPos.value;
  717.     }
  718.    
  719.     //Returns the index of the element that would be returned by a subsequent call to previous
  720.     public int previousIndex()
  721.     {
  722.         //(-1) to account for previous
  723.         return currentIndex() - 1;
  724.     }
  725.    
  726.     //Removes what curPos is point to and moves curPos to previous
  727.     public void remove()
  728.     {
  729.         lastItemPos = curPos;
  730.         curPos = curPos.previous;
  731.         curPos.next = lastItemPos.next;
  732.    
  733.         lastItemPos.next.previous = curPos;
  734.         listSize--;
  735.     }
  736.    
  737.     //prompts user for the value to be removed
  738.     public void remove2()
  739.     {
  740.         removeItem = JOptionPane.showInputDialog("Input the object you would like to remove.");
  741.         curPos = head.next;
  742.        
  743.         while(curPos != head && !curPos.value.toString().equals(removeItem))
  744.         {
  745.             if(curPos.next == head)
  746.             {
  747.                 System.out.println("The item couldn't be found.\n");
  748.                 curPos = head;
  749.             }
  750.             else
  751.             {
  752.                 next();
  753.             }
  754.         }
  755.        
  756.         if(curPos != head)
  757.         {
  758.             remove();
  759.         }
  760.     }
  761.    
  762.     //Replaces what curPos is pointing to
  763.     public void set(Object obj)
  764.     {
  765.         if(curPos == null)
  766.         {
  767.             throw new RuntimeException("There is no established item to set.");
  768.         }
  769.        
  770.         curPos.value = obj;
  771.     }
  772.    
  773.    
  774.     //LIST ITERATOR METHOD END
  775.    
  776.    
  777.     //Returns the current index of the element
  778.     public int currentIndex()
  779.     {
  780.         int counter = 0;
  781.         Node tempCurPos = new Node();
  782.        
  783.         if(!hasNext())
  784.         {
  785.             return -1;
  786.         }
  787.         else
  788.         {
  789.             tempCurPos.next = curPos;
  790.             curPos = head.next;
  791.            
  792.             while(curPos != tempCurPos.next)
  793.             {
  794.                 next();
  795.                 counter++;
  796.             }
  797.            
  798.             curPos = tempCurPos.next;
  799.             return counter;
  800.         }
  801.     }
  802.    
  803.     //returns number of elements in list
  804.     public int size()
  805.     {
  806.         return listSize;
  807.     }
  808.    
  809.     //creates a string from the contents of the list
  810.     public String toString()
  811.     {
  812.         String str = " ";
  813.        
  814.         for(Node node = head.next; node != head; node = node.next)
  815.         {
  816.             str += node.value + " ";
  817.         }
  818.        
  819.         return str;
  820.     }
  821.    
  822.     //creates a string from something that was at the beginning of the list
  823.     public String toBackwardString()
  824.     {
  825.         String str = "";
  826.        
  827.         for(Node node = head.previous; node != head; node = node.previous)
  828.         {
  829.             str += node.value + " ";
  830.         }
  831.        
  832.         return str;
  833.     }
  834.    
  835.     //private inner class for Node
  836.     private class Node implements Serializable
  837.     {
  838.         private static final long serialVersionUID = 1L;
  839.        
  840.         private Object value;
  841.         private Node next;
  842.         private Node previous;
  843.        
  844.         private Node()
  845.         {
  846.             value = null;
  847.             previous = null;
  848.             next = null;
  849.         }
  850.        
  851.         private Node(Object value)
  852.         {
  853.             this.value = value;
  854.             previous = null;
  855.             next = null;
  856.         }
  857.        
  858.         private Node(Object value, Node previous, Node next)
  859.         {
  860.             this.value = value;
  861.             this.previous = previous;
  862.             this.next = next;
  863.         }
  864.     }
  865.    
  866.     public static void main(String[] args)
  867.     {
  868.         // 9) Doubly Linked List (Works)
  869.         DLPositionalListCode list = new DLPositionalListCode();
  870.        
  871.         // 10) Printing previous, next, and size (Works)
  872. //      System.out.println(list.listSize);
  873. //      System.out.println(list.hasNext());
  874. //      System.out.println(list.hasPrevious());
  875. //     
  876. //      System.out.println();
  877. //     
  878.        
  879.         // 11) Sets the List to the letters of the alphabet
  880.         char letter = 'a';
  881.  
  882.         for(int i = 0; i < 26; ++i)
  883.         {
  884.             String newStr = Character.toString(letter + i);
  885.             list.add(newStr);
  886.         }
  887.         System.out.print(list.toString());
  888.        
  889.        
  890.         System.out.println();
  891.         System.out.println();
  892.        
  893.         // 12) Repeat step ten with filled list (Works)
  894. //      System.out.println(list.listSize);
  895. //      System.out.println(list.hasNext());
  896. //      System.out.println(list.hasPrevious());
  897.        
  898.  
  899. //      System.out.println();
  900. //     
  901.         // 13) Moving curPos to the previous node and return its value (Works)
  902. //      list.previous();
  903. //      System.out.println(list.previous());
  904. //     
  905. //      System.out.println();
  906. // 
  907.         // 14) Changing position to $ and printing (Works)
  908. //      list.set("$");
  909. //      System.out.println(list.toString());
  910. //     
  911. //
  912. //      System.out.println();
  913. //     
  914.         // 15) Print current value, current index, previous index, next index five elements back from position z\
  915.         list.next();
  916.         list.next();
  917.         //Current Position = Z
  918.         System.out.println(list.previous());
  919.        
  920.         list.previous();
  921.         list.previous();
  922.         list.previous();
  923.         list.previous();
  924.         list.previous();
  925.        
  926.         System.out.println();  
  927.         list.next();
  928.         System.out.println(list.previous());
  929.         System.out.println(list.currentIndex());
  930.         System.out.println(list.previousIndex());
  931.         System.out.println(list.nextIndex());
  932.        
  933.         //16) Removes Node at current position, the prints the list, list from end to start, size, and current value
  934.         System.out.println();
  935.         list.next();
  936.         System.out.println(list.previous());
  937.         list.remove();
  938.         System.out.println(list.previous());
  939.         System.out.println(list.toString());
  940.         System.out.println();
  941.        
  942.         while(list.currentIndex() != 0)
  943.         {
  944.             list.previous();
  945.         }
  946.        
  947.         //I wanted to check is there a reason for not just having a method that just calls the current position,
  948.         //Or why do we not have something to call the list at a specific index.
  949.        
  950.         for(int i = 0; i < 26; ++i)
  951.         {
  952.             System.out.print(list.previous() + "|");
  953.         }
  954.        
  955.         //17) Repeats 16 but ask what is wanted to be removed (Works)
  956. //      System.out.println();
  957. //     
  958. //      System.out.println();
  959. //      list.next();
  960. //      System.out.println(list.previous());
  961. //      list.remove2();
  962. //      System.out.println(list.previous());
  963. //      System.out.println(list.toString());
  964. //      System.out.println();
  965. //     
  966. //      while(list.currentIndex() != 0)
  967. //      {
  968. //          list.next();
  969. //      }
  970. //     
  971. //      for(int i = 0; i < 26; ++i)
  972. //      {
  973. //          System.out.print(list.previous());
  974. //          System.out.println();
  975. //      }
  976. //     
  977.        
  978.         System.out.println();
  979.        
  980.         //18) Start at the beginning of the list and set all values to "*" then print
  981.         System.out.println();
  982.        
  983.         for(int i = 0; i < 26; ++i)
  984.         {
  985.             list.set("*");
  986.             list.next();
  987.         }
  988.        
  989.         System.out.print(list.toString());
  990.         System.out.println();
  991.         System.out.println();
  992.        
  993.         //19) Spells Happy Birthday to you in the list
  994.        
  995.         list.addAfter2("Happy ");
  996.         list.addAfter2("Birthday ");
  997.         list.addAfter2("to ");
  998.         list.addAfter2("you ");
  999.        
  1000.         System.out.println(list.toString());
  1001.         System.out.println(list.toBackwardString());
  1002.        
  1003.         System.out.println();
  1004.         System.out.println();
  1005.        
  1006.         //20) Repeat 19 with addAfter 2 , addBefore2, and addBefore
  1007.        
  1008.         list.addAfter2("Happy ");
  1009.         list.addAfter2("Birthday ");
  1010.         list.addAfter2("to ");
  1011.         list.addAfter2("you ");
  1012.        
  1013.         System.out.println(list.toString());
  1014.         System.out.println(list.toBackwardString());
  1015.         System.out.println();
  1016.         System.out.println();
  1017.        
  1018.         list.addBefore("Happy ");
  1019.         list.addBefore("Birthday ");
  1020.         list.addBefore("to ");
  1021.         list.addBefore("you ");
  1022.        
  1023.         System.out.println(list.toString());
  1024.         System.out.println(list.toBackwardString());
  1025.         System.out.println();
  1026.         System.out.println();
  1027.        
  1028.         list.addBefore2("Happy ");
  1029.         list.addBefore2("Birthday ");
  1030.         list.addBefore2("to ");
  1031.         list.addBefore2("you ");
  1032.        
  1033.         System.out.println(list.toString());
  1034.         System.out.println(list.toBackwardString());
  1035.        
  1036.     }
  1037. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement