Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

I am unsure how to go about doing this. I need to get it in.

I am trying to figure out how to put a insertion sort in my linked list my assignment. I am unsure how to go about doing this. I would really like some help with this asap. I need to get it in. Listed below is my code to show where I am trying to do it. Any help would be wonderful.

package zoolinkedlist;

/**

 *

 * @author April Adams

 */

public class ZooLinkedList{

   /**

    * @param args the command line arguments

    */

       private String First;

   public void setFirst(String First) {

       this.First = First;

   }

   public void setLast(String Last) {

       this.Last = Last;

   }

   public void setCity(String city) {

       this.city = city;

   }

   public void setCountry(String country) {

       this.country = country;

   }

   public void setPhone(String phone) {

       this.phone = phone;

   }

   public void setContribution(double contribution) {

       this.contribution = contribution;

   }

   public void setId(int id) {

       this.id = id;

   }

   public String getFirst() {

       return First;

   }

   public String getLast() {

       return Last;

   }

   public String getCity() {

       return city;

   }

   public String getCountry() {

       return country;

   }

   public String getPhone() {

       return phone;

   }

   public double getContribution() {

       return contribution;

   }

   public int getId() {

       return id;

   }

   private String Last;

   private String city;

   private String country;

   private String phone;

   private double contribution;

   private int id;

 public ZooLinkedList(String First,String Last, String city, String country, String phone, double contribution, int id) {

//initialize all values in the Contributor object

this.First = First;

this.Last = Last;

this.city = city;

this.country = country;

this.phone = phone;

this.contribution = contribution;

this.id = id;

}

   public void printZooLinkedList() {

//display the Contributor object

System.out.println("First: " + First);

System.out.println("Last:" + Last);

System.out.println("City: " + city);

System.out.println("Country: " + country);

System.out.println("Phone: " + phone);

System.out.println("Contribution: " + contribution);

System.out.println("ID: " + id);

System.out.println();

}

 public ZooLinkedList() {

   }

   @Override

   public String toString() {

       return "ZooLinkedList{"+"First=" + First +", Last=" + Last +" city=" + city + ", country=" + country + ", phone=" + phone + ", contribution=" + contribution + ", id=" + id + '}';

   }

}

 package zoolinkedlist;

/**

 *

 * @author April Adams

 */

public class Node {

   ZooLinkedList z;

   Node next;

public Node(ZooLinkedList data){

//initialize member variables

z=data;

next=null;

}

public void displayNode() {

//display the contents of this node

z.printZooLinkedList();

}

void setNext(Node next){

   this.next=next;

}

Node getNext(){

   return this.next;

}

}

public class SortingMethods {

      public static void main(String [] args) {

         Scanner file_inp = null;

         String first = null;

         String last = null;

         String city= null;

         String country = null;

         String phone = null;

         double contribution = 0;

         int id = 0;

         ZooLinkedList z = null;

       try {

            //opening of contributors file

        ZooLinkedList = new Scanner(new File("C:\Users\April Adams\Documents\NetBeansProjects\ZooLinkedList\Contributors.csv"));

        ZooLinkedList.useDelimiter(Pattern.compile("(\n)|(\r)|,"));

        //insertion sort for the link list

        for( int i = 1; i < ZooLinkedList.size(); i++){

                int j = i;

               ZooLinkedList tmp;

           while( j > 0 && ZooLinkedList.get(j-1).getFirst().compareTo( ZooLinkedList.get(j).getFirst()) > 0){

               tmp = ZooLinkedList.remove( j);

               ZooLinkedList.add( j-1, tmp);

               j = j - 1;

               }

          }

           }

      }

         catch (FileNotFoundException e) {

             System.err.println("Error opening file.");

         }

      }

package zoolinkedlist;

/**

 *

 * @author April Adams

 */

public class Stack{

private int size;

Node link_first;

   public Stack(){

       link_first=null;

       size = 0;

   }

   public int size(){

       return size;

   }

   public void push(Node newNode){

       if(link_first==null)

       link_first=newNode;

       else {

           newNode.setNext(link_first);

           link_first=newNode;

       }

   }

   public Node pop() {

       if(link_first==null){

         return null;

       }

         else if(link_first.getNext()==null){

           Node t=link_first;

           link_first=null;

           return t; 

         }

         else {

           Node t=link_first;

           link_first=link_first.getNext();

           return t; 

         }

   }

   public void print(){

       Node dsp = link_first;

       while (dsp != null){

           dsp.displayNode();

           dsp = dsp.next;

         }

        System.out.println();

   } 

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question