Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can any one add these functions for the following code(linkedlist) ? 1) Given an unsorted linked list of Students (Not a generic type linked list

can any one add these functions for the following code(linkedlist) ?

  1. 1) Given an unsorted linked list of Students (Not a generic type linked list but one made

    specifically for students, you can do this by swapping out the T for students and taking out the ), write a member function called Sort which sorts the linked list using bubble sort based on their student IDs (Hint: dont actually swap nodes, just swap data)

  2. Write a Function called printEveryOther that prints out every other node of a linked list (so skips the first node, prints out the second node, skips the third node etc...).

3) Overload equals in your linked list implementation of List to mean two lists are equal if the data in each node of the list is the same in the same order. This should rely on type Ts equal.

4) Write recursive function called SumList, a member function of a linked list of Integer nodes, that RECURSIVELY sums up all the data values of the Linked List

main.java

import java.util.*; class Main { public static void main(String[] args) { LinkedList myList= new LinkedList(); Student A= new Student("Anisha", 700111111); Student B= new Student("Barry", 700222222); Student C= new Student("Carly", 700333333); Student D= new Student("Daniqua", 700444444); Student E= new Student("Eugenio", 700555555); myList.addToEnd(A); myList.addToEnd(B); myList.addToEnd(C); myList.addToEnd(D); myList.addToEnd(E); System.out.println("here is the list after adding Anisha...Eugenio"+myList); System.out.println("here's the list length after adding 5: "+myList.getLength()); System.out.println("removeFromFront should be Eugenio "+myList.removeFromFront()); System.out.println("new length after removing Eugenio:"+myList.getLength()); System.out.println("removeFromEnd should be Anisha" + myList.removeFromEnd()); System.out.println("new length after removing Anisha: "+myList.getLength()); System.out.println("removeTarget with Barry, should return true: "+myList.removeTarget(B)); System.out.println("new length after removing Barry "+myList.getLength()); } }

student.java

public final class Student { private int schoolID; private String name;

public Student(String namePassed,int schoolIDPassed) { schoolID= schoolIDPassed; name= namePassed; }

public int getSchoolID() { return schoolID; }

public void setSchoolID(int schoolIDPassed) { schoolID=schoolIDPassed; }

public String getName() { return name; }

public void setName(String namePassed) { name=namePassed; }

public boolean equals(Object toCompare) { Student temp= (Student) toCompare; return(temp.getSchoolID()==schoolID); } public String toString() { String toReturn="name: "+name+" id: "+schoolID; return (toReturn); }

}

LinkedList.java

public class LinkedList { Node head; public LinkedList() { head=null; } public int getLength() { Node current=head; int size=0; while(current!=null) { size++; current=current.getnext(); } return size; } public void addToFront(T toAdd) { Node addNode= new Node (toAdd, head); head=addNode; } public void addToEnd(T toAdd) { Node current=head; Node prev=head; while(current!=null) { prev=current; current=current.getnext(); } Node nodeToAdd= new Node(toAdd, null); if(prev==null) { head=nodeToAdd; return; } prev.setnext(nodeToAdd); } public T removeFromFront() { if(head==null) { return null; } Node toReturn=head; head=head.getnext(); return toReturn.getdata(); } public T removeFromEnd() { if(head==null) { return null; } Node current=head; Node prev=head; Node first=head; while(current!=null) { first=prev; prev=current; current=current.getnext(); } if(first==prev) { head=null; return first.getdata(); } first.setnext(null); return prev.getdata(); } public boolean removeTarget(T toRemove) { Node prev=head; Node current=head; if(current==head &¤t!=null&& current.getdata().equals(toRemove)) { head=current.getnext(); return true; } while (current!=null) { if(current.getdata().equals(toRemove)) { prev.setnext(current.getnext()); return true; } prev=current; current=current.getnext(); } return false; } @Override public String toString() { Node current=head; String toReturn="head->"; while(current!=null) { toReturn=toReturn+current.getdata()+"->"; current=current.getnext(); } toReturn=toReturn+"null"; return toReturn; } private class Node { private T data; private Node next; Node(T dataPassed, Node nextPassed) { data=dataPassed; next=nextPassed; } public void setdata(T dataPassed) { data=dataPassed; } public T getdata() { return data; } public Node getnext() { return next; } public void setnext(Node passed) { next=passed; } public String toString() { return(""+data); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions