Problem 1 Create Node class or use what you have done in Lab

Problem 1: Create Node class (or use what you have done in Lab4)

• Add methods to get and set, Data and Link

• Here the data is an object. (java object) your Linked List can hold any java class.

a) Create MyLinkedList2 class and write methods to

• Methods to insert nodes at the beginning, at the end and at a specific position. insertFirst(Node x), insertLast(Node x) , insertLast(Node x, int pos)

• Methods to delete node from the beginning and from the end. delFirst(), delLast()

• Method to search the Linked List for a specific object. searchList(x Object)

• Method to Sort the Linked List in ascendant or descendant order according to the object value(s). for example sorted by name, by age, by gpa. sortAsc(),sortDesc().

• Method to display all nodes objects (printing an object is related to its toString() method).

• Method to return the largest and smallest object in the list – maxList(), minList() according to some specific object members.

. • Method to return the number of nodes – listSize()

• Method to merge two linked Lists into one. MyLinkedList2 list3= mergeList(MyLinkedList2 list1, MyLinkedList2 list2)

b) Create Main class : MyLinkedList2Demo

• Create a linked List LL1 and insert some objects to it. Use any a student java class.

• Fill LL1 by adding students from an input file.

• Display all the objects (students) in the List LL1

• Search for a particular object (Student by name or by Id or both) in LL1 and print true or false

• Sort LL1 and display it ( using asc and desc on multiple members)

• Perform all the delete methods on LL1.

• Perform all the insert methods on LL1.

• Call and run all the implemented methods in a sequential order. NB: Organize a nice output for all the questions above.

Problem 2: Same as Pb1 using double linked lists. Create Nod3e and MyLinkedList3 classes.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

The input file :

id fname lname age gpa year college cuCourses totalCourses
201412345 Amna chamisi 17 2.1 2014 CIT 5 30
201112345 Amna chamisi 17 2.1 2014 ENG 31 32
201412345 Amna chamisi 17 2.1 2014 COE 5 29
201412345 Amna chamisi 17 2.1 2014 CIT 5 30
201412345 Amna chamisi 17 2.1 2014 CIT 5 30
201412345 Amna chamisi 17 2.1 2014 CIT 5 30
201412345 Amna chamisi 17 2.1 2014 CIT 5 30

Solution

/**

* Node.java

* @author

*

*/

public class Node {

   public Object getObj() {

       return obj;

   }

   public Node setObj(Object obj) {

       this.obj = obj;

       return this;

   }

   public Node getNext() {

       return next;

   }

   public Node setNext(Node next) {

       this.next = next;

       return this;

   }

   private Object obj;

   private Node next;

}

import java.util.Collections;

import java.util.Comparator;

import java.util.LinkedList;

/**

* MyLinkedList2.java

* @author

*

*/

public class MyLinkedList2 {

   public void insertFirst(Node x) {

       if (head == null) {

           head = x;

       } else {

           x.setNext(head);

       }

   }

   public void insertLast(Node x) {

       if (head == null) {

           head = x;

           return;

       } else {

           Node temp = head;

           while (temp.getNext() != null) {

               temp = temp.getNext();

           }

           temp.setNext(x);

       }

   }

   public void insertPos(Node x, int pos) {

       // starting with 0

       if (pos < 0)

           return;

       if (head == null) {

           if (pos > 0)

               return;

           head = x;

           return;

       }

       if (pos == 0) {

           insertFirst(x);

           return;

       }

       Node temp = head;

       for (int i = 1; i < pos; i++) {

           temp = temp.getNext();

       }

       x.setNext(temp.getNext());

       temp.setNext(x);

   }

   public void delFirst() {

       if (head == null)

           return;

       head = head.getNext();

   }

   public void delLast() {

       Node temp = head;

       if (head != null) {

           if (head.getNext() == null) {

               head = null;

               return;

           }

           while (temp.getNext().getNext() != null) {

               temp = temp.getNext();

           }

           temp.setNext(null);

       }

   }

   public boolean search(Object x, Comparator<Object> c) {

       if (head == null)

           return false;

       Node temp = head;

       while (temp.getNext() != null) {

           if (c.compare(x, temp.getObj()) == 0) {

               return true;

           }

           temp = temp.getNext();

       }

       return false;

   }

   public void sortAsc(Comparator<Object> c) {

       if (head == null)

           return;

       LinkedList<Object> ll = new LinkedList<>();

       Node temp = head;

       while (temp.getNext() != null) {

           ll.add(temp.getObj());

           temp = temp.getNext();

       }

       Collections.sort(ll, c);

      

       head=new Node().setObj((Student) ll.get(0));

       Node t = head;

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

           t.setNext(new Node().setObj((Student)ll.get(i)));

           t = t.getNext();

       }

   }

   public void sortDesc(Comparator<Object> c) {

       if (head == null)

           return;

       LinkedList<Object> ll = new LinkedList<>();

       Node temp = head;

       while (temp.getNext() != null) {

           ll.add(temp.getObj());

           temp = temp.getNext();

       }

       Collections.sort(ll, c);

      

       head=new Node().setObj((Student) ll.get(0));

       Node t = head;

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

           t.setNext(new Node().setObj((Student)ll.get(i)));

           t = t.getNext();

       }

   }

   public void printAll() {

           if(head==null)

               return;

           Node temp=head;

           while(temp.getNext()!=null)

           {

               System.out.println(temp.getObj());

               temp=temp.getNext();

           }

          

   }

   public Object maxList(Comparator<Object> c) {

       if(head==null)return null;

       Node max=head;

       Node temp=head;

       while(temp.getNext()!=null)

       {

           if(c.compare(max, temp)<0)

               max=temp;

           temp=temp.getNext();

       }

       return max;

   }

   public Object minList(Comparator<Object> c) {

       if(head==null)return null;

       Node min=head;

       Node temp=head;

       while(temp.getNext()!=null)

       {

           if(c.compare(min, temp)>0)

               min=temp;

           temp=temp.getNext();

       }

       return min;

   }

   public int listSize() {

       if(head==null)

           return 0;

       int i=0;

       Node temp=head;

       while(temp.getNext()!=null)

       {

          

           i++;

           temp=temp.getNext();

       }

       return i;

   }

   public static MyLinkedList2 mergeList(MyLinkedList2 list1, MyLinkedList2 list2) {

       Node temp=list1.head;

       if(temp==null) return list2;

       Node temp1=temp;

       while(temp1.getNext()!=null)

       {

          

           temp1=temp1.getNext();

       }

       temp1.setNext(list2.head);

       return list1;

      

   }

   private Node head;

}

import java.util.Comparator;

import java.util.Scanner;

/**

* MyLinkList2Demo.java

*

* @author

*

*/

public class MyLinkList2Demo {

   public static void main(String[] args) {

       MyLinkedList2 ll1 = new MyLinkedList2();

       Scanner sc = new Scanner(System.in);

       Student st = null;

       int l = 0;

       while (l < 4) {

           l++;

           Student student = new Student(sc.nextInt(), sc.next(), sc.next(), sc.nextInt(), sc.nextDouble(),

                   sc.nextInt(), sc.next(), sc.nextInt(), sc.nextInt());

           st = student;

           ll1.insertLast(new Node().setObj(student)); // inserting into linked

                                                       // list

       }

       sc.close();

       ll1.printAll();

       ll1.search(st, new Comparator<Object>() {

           @Override

           public int compare(Object o1, Object o2) {

               Student s1 = (Student) o1;

               Student s2 = (Student) o2;

               if (s1.getAge() < s2.getAge()) // if you want search based on

                                               // different parameter just

                                               // change here

               {

                   return -1;

               } else if (s1.getAge() == s2.getAge()) {

                   return 0;

               }

               return 1;

           }

       });

       ll1.sortAsc(new Comparator<Object>() {

           @Override

           public int compare(Object o1, Object o2) {

               Student s1 = (Student) o1;

               Student s2 = (Student) o2;

               if (s1.getAge() < s2.getAge()) // if you want sort based on

                                               // different parameter just

                                               // change here

               {

                   return -1; // read about compare method

               } else if (s1.getAge() == s2.getAge()) {

                   return 0;

               }

               return 1;

           }

       });

       ll1.sortDesc(new Comparator<Object>() {

           @Override

           public int compare(Object o1, Object o2) {

               Student s1 = (Student) o1;

               Student s2 = (Student) o2;

               if (s1.getAge() < s2.getAge()) // if you want sort based on

                                               // different parameter just

                                               // change here

               {

                   return 1; // read about compare method

               } else if (s1.getAge() == s2.getAge()) {

                   return 0;

               }

               return -1;

           }

       });

   }

}

/**

* Student.java

* @author

*

*/

public class Student {

   public int getId() {

       return id;

   }

   public void setId(int id) {

       this.id = id;

   }

   public String getFname() {

       return fname;

   }

   public void setFname(String fname) {

       this.fname = fname;

   }

   public String getLname() {

       return lname;

   }

   public void setLname(String lname) {

       this.lname = lname;

   }

   public int getAge() {

       return age;

   }

   public void setAge(int age) {

       this.age = age;

   }

   public double getGpa() {

       return gpa;

   }

public void setGpa(double gpa) {

       this.gpa = gpa;

   }

   public int getYear() {

       return year;

   }

   public void setYear(int year) {

       this.year = year;

   }

   public String getCollege() {

       return college;

   }

   public void setCollege(String college) {

       this.college = college;

   }

   public int getCuCourses() {

       return cuCourses;

   }

   public void setCuCourses(int cuCourses) {

       this.cuCourses = cuCourses;

   }

   public int getTotalCourses() {

       return totalCourses;

   }

   public void setTotalCourses(int totalCourses) {

       this.totalCourses = totalCourses;

   }

private int id;

   private String fname;

   private String lname;

private int age;

private double gpa;

private int year;

   private String college;

private int cuCourses;

private int totalCourses;

   public Student(int id, String fname, String lname, int age, double gpa, int year, String college, int cuCourses,

           int totalCourses) {

       super();

       this.id = id;

       this.fname = fname;

       this.lname = lname;

       this.age = age;

       this.gpa = gpa;

       this.year = year;

       this.college = college;

       this.cuCourses = cuCourses;

       this.totalCourses = totalCourses;

   }

@Override

   public String toString() {

       return \"Student [id=\" + id + \", fname=\" + fname + \", lname=\" + lname + \", age=\" + age + \", gpa=\" + gpa

               + \", year=\" + year + \", college=\" + college + \", cuCourses=\" + cuCourses + \", totalCourses=\"

               + totalCourses + \"]\ \";

   }

  

  

}

Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li
Problem 1: Create Node class (or use what you have done in Lab4) • Add methods to get and set, Data and Link • Here the data is an object. (java object) your Li

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site