Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

by using java (netbeans) plz answer the Q: write the printBackwards() method to

ID: 3818500 • Letter: B

Question

by using java (netbeans) plz answer the Q:

write the printBackwards() method to print from the tail to the head of the doubly link list.

Then write a test application named Lab8test. In the main function do the following

*creat a department object.

*Input the number of courses in the department .

*Input the id and name of each course and add it to the department(Hint: use a loop)

*print the courses forward.

*Input the id of a course to be deleted. Delete the course.

*print number of courses.

* print the courses backward.

by using this code write the main:

package singlelinklist;

import java.util.Scanner;

interface InterfaceSLList {

public void addcourse( int id,String name );

public void deletecourse(int id);

public boolean exist(int id);

public void printcourses();

public int countcourses();

}

class department implements InterfaceSLList {

private class course {

private String name;

private int id;

private course next;   

// constructor   

private course( int id,String name) {

this.name = name;

this.id = id;

next = null;

}

private course(){

name="";

id=0;

}

}

// important variables: head and tail

private course head, tail;

// constructor method

public department() {

head = tail = null;

}

public void printcourses() {

if( head==null ) { // the list is empty

System.out.println("this list is empty");

return;

}

course probe = head;

System.out.println(" all course in the department are ");

do {

System.out.println("course id"+probe.id + "course name " + probe.name );

probe = probe.next;

} while ( probe!=null );

}

public boolean exist(int id)

{

//return when the linked list is empty

if( head==null )

{

return false;

}

course probe = head;

do {

if( probe.id==id ) {

  

return true;

}

probe = probe.next;

} while ( probe!=null ) ;

return false;

}

public int countcourses() {

if( head==null )

return 0;

course probe = head;

int count = 0;

do {

count = count + 1;

probe = probe.next;

} while ( probe!=null ) ;

return count;

}

public void addcourse(int id,String name) {

// create a new node for name

course newNode = new course(id, name);

// case 1: the list is empty

if (head == null) {

head = newNode;

tail = newNode;

}

else {

newNode.next = head;

head = newNode;

}

System.out.println(" course" + name + " with id "+ id +" has been added.");

}

public void deletecourse(int id){

// case 1: the linked list is empty

if( head==null )

{

System.out.println("this list is empty");

return;

}

course mynode = head;

course prev = head;

// find the node to be deleted

do {

if ( mynode.id==id)

{

if (head == mynode)

{// case we remove the first node

if (head.next ==null) tail =null;// one node update the tail

head = mynode.next; // update the head

} else if (tail == mynode)

{// case we remove the last node

tail = prev; // update the tail

prev.next = null; // set the next of the last node to NULL

}else

{ // case we remove a node in the middle

prev.next = mynode.next; // set the next of prev to next of //mynode (in order to delete mynode)

}

System.out.println( " course: " + mynode.name + " is removed ") ;

return; // leave the while loop

}

prev = mynode; // update prev to point to my current node

mynode = mynode.next; // update my_node to point to next node

}while (mynode !=null); // loop until the last node

// System.out.println("Sorry, " + name + " is not in the list");

}

}

Explanation / Answer

Hi,
please see below the classes.Please commentfor any queries/feedbacks.

Thanks,

InterfaceSLList.java

import java.util.Scanner;
public interface InterfaceSLList {
   public void addcourse( int id,String name );
   public void deletecourse(int id);
   public boolean exist(int id);
   public void printcourses();
   public int countcourses();
}

Department.java

class Department implements InterfaceSLList {

private class course {
private String name;
private int id;
private course next;   
// constructor   
private course( int id,String name) {
this.name = name;
this.id = id;
next = null;
}
private course(){
name="";
id=0;
}
}
// important variables: head and tail
private course head, tail;
// constructor method
public Department() {
head = tail = null;
}

public void printcourses() {
if( head==null ) { // the list is empty
System.out.println("this list is empty");
return;
}
course probe = head;
System.out.println(" all course in the department are ");
do {
System.out.println("course id"+probe.id + " course name " + probe.name );
probe = probe.next;
} while ( probe!=null );
}

//Method to print the list backwards
public void printBackwards() {
   course [] reverseArray = new course[countcourses()];
   int counter=0;
if( head==null ) { // the list is empty
System.out.println("this list is empty");
return;
}
course probe = head;
System.out.println(" all course in the department are ");
do {
   reverseArray[counter] = probe;
probe = probe.next;
   counter++;
} while ( probe!=null );

for(int i=counter-1;i>=0;i--){
   course currProbe = reverseArray[i];
   System.out.println("course id "+currProbe.id + " course name " + currProbe.name );
}
}



public boolean exist(int id)
{
//return when the linked list is empty
if( head==null )
{

return false;
}
course probe = head;
do {
if( probe.id==id ) {
  
return true;
}
probe = probe.next;
} while ( probe!=null ) ;
return false;
}

public int countcourses() {
if( head==null )
return 0;
course probe = head;
int count = 0;
do {
count = count + 1;
probe = probe.next;
} while ( probe!=null ) ;
return count;
}

public void addcourse(int id,String name) {
// create a new node for name
course newNode = new course(id, name);
// case 1: the list is empty
if (head == null) {
head = newNode;
tail = newNode;
}
else {
newNode.next = head;
head = newNode;
}
System.out.println(" course " + name + " with id "+ id +" has been added.");
}

public void deletecourse(int id){
// case 1: the linked list is empty
if( head==null )
{
System.out.println("this list is empty");
return;
}
course mynode = head;
course prev = head;
// find the node to be deleted
do {
if ( mynode.id==id)
{
if (head == mynode)
{// case we remove the first node
if (head.next ==null) tail =null;// one node update the tail
head = mynode.next; // update the head
} else if (tail == mynode)
{// case we remove the last node
tail = prev; // update the tail
prev.next = null; // set the next of the last node to NULL
}else
{ // case we remove a node in the middle
prev.next = mynode.next; // set the next of prev to next of //mynode (in order to delete mynode)
}
System.out.println( " course: " + mynode.name + " is removed ") ;
return; // leave the while loop
}
prev = mynode; // update prev to point to my current node
mynode = mynode.next; // update my_node to point to next node
}while (mynode !=null); // loop until the last node
// System.out.println("Sorry, " + name + " is not in the list");
}
}

Lab8test.java

import java.util.Scanner;


public class Lab8test {

   public static void main(String [] args){
      
       Department department = new Department();
       Scanner scan = new Scanner(System.in);
       String noOfCourses= "";
       int noOfCourseInt =0;
      
       //Treading the no of courses from user
       System.out.println("Enter the no of courses: ");
       noOfCourses = scan.nextLine();
       noOfCourseInt = Integer.valueOf(noOfCourses);
      
       //Reading the id and name of the course from the user in loop
       for(int i=0;i<noOfCourseInt;i++){
           System.out.println("Id :");
           String id = scan.nextLine();
           System.out.println("Name : ");
           String name = scan.nextLine();
          
           department.addcourse(Integer.valueOf(id), name); //Adding the course to department
       }
      
       //Printing the course in proper order
       department.printcourses();
      
       System.out.println("Enter the id of the course to be deleted : ");
       String idToDelete = scan.nextLine();
      
       department.deletecourse(Integer.valueOf(idToDelete)); //Deleting
       System.out.println("No of courses: "+department.countcourses());

       //Print backwards
       System.out.println("Course in revese direction : " );
       department.printBackwards();
   }

}

Sample output:

Enter the no of courses:
3
Id :
101
Name :
Maths

course Maths with id 101 has been added.
Id :
102
Name :
Science

course Science with id 102 has been added.
Id :
103
Name :
Computer Science

course Computer Science with id 103 has been added.

all course in the department are
course id103 course name Computer Science
course id102 course name Science
course id101 course name Maths
Enter the id of the course to be deleted :
103

course: Computer Science is removed

No of courses: 2
Course in revese direction :

all course in the department are
course id 101 course name Maths
course id 102 course name Science