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;
r.
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
1. 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
8. 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() {
@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() {
@Override
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
9. 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() {
@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 {
10. 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;
}
11. 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;