Question: by using java solve this Q. by Edit in the this code using double link list package singlelinklist; import java.util.Scanner; interface InterfaceSLList { public void
by using java solve this Q.

by Edit in the this code using double link list
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");
}
}
public class Singlelinklist {
public static void main(String[] args) {
Scanner input=new Scanner (System.in);
department db=new department();
System.out.println("Welcome to PMU department");
System.out.println("please enter the number of course to add......");
int size=input.nextInt();
System.out.println("Enter the course");
for(int i=0;i System.out.print("Course id"); int id=input.nextInt(); System.out.print("Course name "); String name=input.next(); db.addcourse(id, name); if (!db.exist(id)) db.addcourse(id, name); else System.out.println("The course with id "+id+"already exist"); } System.out.println("-------------------------"); db.printcourses(); System.out.println("-------------------------"); System.out.println("Enter the id of course to be deleted..."); int d=input.nextInt(); if(db.exist(d)) db.deletecourse(d); else System.out.println("The course with id "+d+"is not exsit..t"); System.out.println("-----------------------------"); db.printcourses(); System.out.println("-----------------------------"); System.out.println("There are"+db.countcourses()+" courses in the department"); System.out.println("-----------------------------"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
