Advertisement
ydornberg

assn1 mylist

Oct 10th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.24 KB | None | 0 0
  1. package assign1;
  2.  
  3. public class MyList {
  4.     // Node class
  5.     private class Node {
  6.     private char value;
  7.     private Node next;
  8.     public Node(char value) { this(value, null); }//add constructor
  9.     public Node(char value, Node next) {
  10.         this.next = next;
  11.         this.value = value;
  12.     }
  13.     // Accessor methods
  14.     public char getElement() { return value; }
  15.     public Node getNext() { return next; }
  16.     // Modifier methods
  17.     public void setElement(char v) { value = v; }
  18.     public void setNext(Node n) { next = n; }
  19.     }
  20.    
  21.     // MyList class
  22.     private Node head;
  23.     // Implement required methods here
  24.     public MyList(MyList rhs){
  25.         head = null;
  26.     }
  27.     public MyList (char[] charArray, int n){//-----------------------------------MyList char[] int
  28.         for(int i=0; i<n; i++){
  29.             this.pushBack(charArray[i]);
  30.         }
  31.     }
  32.     public boolean remove (int index){//-------------------------------------boolean remove index
  33.         if(this.size()==0){
  34.             System.out.println("No entries to remove.");
  35.             return false;
  36.         }
  37.         else{
  38.             if(index<0 || index>this.size()){
  39.                 System.out.println("Not a valid index number.");
  40.                 return false;
  41.             }
  42.             else{
  43.                 if(index == 0){     //remove object 0
  44.                     Node temp = head;
  45.                     head = head.next;
  46.                     temp.next = null;
  47.                 }
  48.                 else if(index == this.size()){  //remove last object
  49.                     Node temp = getNode(index-1);
  50.                     temp.next = null;
  51.                 }
  52.                 else{       //remove object inside list
  53.                     Node temp1 = getNode(index-1);
  54.                     Node temp2 = getNode(index);
  55.                     Node temp3 = getNode(index+1);
  56.                     temp1.next = temp3;
  57.                     temp2.next = null;
  58.                 }
  59.                 return true;
  60.             }
  61.         }
  62.     }
  63.     public boolean remove (char value){//-------------------------------------boolean remove char
  64.         int index = this.find(value);
  65.         boolean x = remove(index);
  66.         return x;
  67.     }
  68.     public boolean removeAll (char value){//------------------------------------boolean removeAll char
  69.         boolean j = findAll(value);
  70.         if(j==false)
  71.             return j;
  72.         else{
  73.             while(j==true){
  74.                 remove(value);
  75.                 j = findAll(value);
  76.             }
  77.             return true;
  78.         }
  79.     }
  80.     public Node previous(Node curr){
  81.         int i=0;
  82.         while(getNode(i)!=curr)
  83.             i++;
  84.         return getNode(i-1);
  85.         }
  86.     public Node next(Node curr){return curr.next;}
  87.     public boolean contains(char value){//---------------------------------boolean contains char
  88.         boolean contain = findAll(value);
  89.         return contain;
  90.     }
  91.     public char get(int index){//--------------------------------------------char get index
  92.         Node temp1 = head;
  93.         for(int i=0; i<index; i++){
  94.             if(temp1.next != null)
  95.                 temp1 = temp1.next;
  96.         }
  97.         return temp1.value;
  98.     }
  99.     private Node getNode(int index){//-----------------------------------------Node getNode index
  100.         Node temp1 = head;
  101.         for(int i=0; i<index; i++){
  102.             if(temp1.next != null)
  103.                 temp1 = temp1.next;
  104.         }
  105.         return temp1;
  106.     }
  107.     public void set(int index, char value){//-----------------------------------void set index value
  108.         if(index<0 || index>this.size()){
  109.             System.out.println("Invalid index number.");
  110.         }
  111.         else
  112.             getNode(index).value = value;
  113.     }
  114.     public boolean equals(MyList Ilist){//----------------------------------------boolean equals MyList
  115.         int i=0;
  116.         while(this.getNode(i).value == Ilist.getNode(i).value){
  117.             i++;
  118.             if((i==this.size())&&(Ilist.getNode(i).next == null))
  119.                 return true;
  120.         }
  121.         return false;
  122.     }
  123.     public void pushFront(char value){//---------------------------------------void pushFront char
  124.         this.insertBefore(0,value);
  125.     }
  126.     public void pushBack(char value){//----------------------------------------void pushBack char
  127.         this.insertAfter(this.size(), value);
  128.     }
  129.     public void popFront(){//--------------------------------------------------void popFront
  130.         this.remove(0);
  131.     }
  132.     public void popBack(){//-----------------------------------------------------void popBack
  133.         this.remove(this.size());
  134.     }
  135.     public void swap(int i, int j){//--------------------------------------------void swap int int
  136.         if(this.size()==0)
  137.             System.out.println("No entries to swap.");
  138.         else{
  139.             if((i>=0)&&(i<=this.size())&&(j>=0)&&(j<=this.size())){
  140.                 Node temp1 = getNode(i);
  141.                 Node temp2 = getNode(j);
  142.                 this.remove(j);
  143.                 this.insertAtPos(j, temp1.value);
  144.                 this.remove(i);
  145.                 this.insertAtPos(i, temp2.value);
  146.             }
  147.             else
  148.                 System.out.println("Not a valid index number.");
  149.         }
  150.     }
  151.     public void insertAtPos(int i, char value){//--------------------------------void insertAtPos int char
  152.         Node temp = new Node(value,null);
  153.         if(this.size()==0){
  154.             head = temp;
  155.         }
  156.         else{
  157.             if((i>this.size())||(i<0)){ //index out of list
  158.                 System.out.println("Not a valid index number.");
  159.             }
  160.             else if(i==this.size()){//insert at end
  161.                 getNode(i).next = temp;
  162.             }
  163.             else if(i==0){      //insert at beginning
  164.                 temp.next = getNode(i);
  165.                 head = temp;
  166.             }
  167.             else{               //insert inside list
  168.                 temp.next = getNode(i);
  169.                 getNode(i-1).next = temp;
  170.             }
  171.         }
  172.     }
  173.     public void insertAfter(int i, char value){//-------------------------------void insertAfter int char
  174.         Node temp = new Node(value,null);
  175.         if(this.size()==0){
  176.             head = temp;
  177.         }
  178.         else{
  179.             if((i>this.size())||(i<0)){ //index out of list
  180.                 System.out.println("Not a valid index number.");
  181.             }
  182.             else if(i==this.size()){//insert at end
  183.                 getNode(i).next = temp;
  184.             }
  185.             else{               //insert inside list
  186.                 temp.next = getNode(i+1);
  187.                 getNode(i).next = temp;
  188.             }
  189.         }
  190.     }
  191.     public void insertBefore(int i, char value){//--------------------------------void insertBefore int char
  192.         Node temp = new Node(value,null);
  193.         if(this.size()==0){
  194.             System.out.println("No valid entries to insert before");
  195.         }
  196.         else{
  197.             if((i>this.size())||(i<0)){ //index out of list
  198.                 System.out.println("Not a valid index number.");
  199.             }
  200.             else if(i==0){//insert at beginning
  201.                 temp.next = head;
  202.                 head = temp;
  203.             }
  204.             else{               //insert inside list
  205.                 Node temp3 = getNode(i-1);
  206.                 Node temp1 = getNode(i-2);
  207.                 temp.next = temp3;
  208.                 temp1.next = temp;
  209.             }
  210.         }
  211.     }
  212.     public MyList sublist(int fromIndex, int toIndex){//------------------MyList sublist int int
  213.         MyList sublist = new MyList(null);
  214.         if((fromIndex>=0)&&(fromIndex<=this.size())&&(toIndex>=0)&&(toIndex<=this.size())){
  215.             for(int i=fromIndex; i<=toIndex; i++){
  216.                 sublist.pushBack(get(i));
  217.             }
  218.         }
  219.         else
  220.             System.out.println("Invalid index numbers provided.");
  221.         return sublist;
  222.     }
  223.     public int find(char value){ //--------------------------------------------int find char
  224.         Node temp = head;
  225.         int i =0;
  226.         while((temp.value!=value)&&(i<this.size())){
  227.             i++;
  228.             if(temp.next!=null)
  229.                 temp = temp.next;
  230.             }
  231.         if(temp.value == value)
  232.             return i;
  233.         else{
  234.             System.out.println("Value '"+value+"' not found.");
  235.             return -1;
  236.         }
  237.     }
  238.     public boolean findAll(char value){ //--------------------------------------------boolean find char
  239.         Node temp = head;
  240.         int i =0;
  241.         boolean j=true;
  242.         while(temp.value!=value){
  243.             i++;
  244.             temp = temp.next;
  245.             if(i==this.size()){
  246.                 j=false;
  247.                 return j;
  248.             }
  249.         }
  250.         return j;
  251.     }
  252.     public int find(MyList queryStr){return 0;/*remove later*/}
  253.     public char[] toArray(){//------------------------------------------------char[] toArray
  254.         char[] tArray;
  255.         tArray = new char[this.size()];
  256.         if(head != null){
  257.             for(int i=0; i<this.size(); i++){
  258.                 tArray[i] = getNode(i).value;
  259.             }
  260.         }
  261.         else
  262.             System.out.println("No valid entries.");
  263.         return tArray;
  264.     }
  265.     public void reverse(){//------------------------------------------------------------void reverse
  266.         Node temp1 = null;
  267.         Node temp2=head;
  268.         while(temp2!=null){
  269.             Node temp3 = temp2.next;
  270.             temp2.next = temp1;
  271.             temp1 = temp2;
  272.             temp2 = temp3;
  273.         }
  274.         head = temp1;
  275.     }
  276.     public int size(){//------------------------------------------------------------------int size
  277.         int i=0;
  278.         if(head!=null){
  279.             //i++;
  280.             Node temp = head;
  281.             while(temp!=null){
  282.                 temp = temp.next;
  283.                 i++;
  284.             }
  285.         }
  286.         return i;
  287.     }
  288.     public void print(){//-------------------------------------------------------------void print
  289.         for(int i=0; i<this.size() ; i++){
  290.             System.out.println(getNode(i).value);
  291.         }
  292.     }
  293.     public String toString(){//------------------------------------------------------string toString
  294.         StringBuilder string = new StringBuilder(this.size());
  295.         for(char c: this.toArray())
  296.             string.append(c);
  297.         return string.toString();
  298.     }
  299.    
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement