by using java solve this Q. by Edit in the this code using double link list pack
ID: 3818295 • Letter: B
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 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<size;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("-----------------------------");
}
}
Lab Assignment In the previous lab, we have created the Department class and the Course class along with their attributes and methods. We have implemented the above mentioned using singly linked lists. In this lab, you should implement the same using doubly linked list Write the printBackwards0 method to print from the tail to the head of the doubly linked list Then, write a test application named Lab8Test. In the main function, do the followin Create 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. The following lines are the singly linked list requirements. 1. Class Course that includes two instance variables: int id; ll the course ID String name; ll the course name Course next; Default and overloaded constructors 2. Class Department that includes two instance variables: Course head; Course Tail; Your class should have the following A method that initializes the instance variables. boolean exist(int) that checks whether the course object with id passed as parameter exists in the singly linked list or not. String) creates and adds the course if the course with id passed a void addCourse int, parameter does not exist in the singly linked list. void deleteCourse (int) to delete a course if it exists in the singly linked list void printCourses0 prints all the courses in the single linked list int countCourses0 counts the number of courses in the listExplanation / Answer
PROGRAM CODE:
package linkedlist;
class course {
public String name;
public int id;
public course next;
public course prev;
// constructor
public course( int id,String name) {
this.name = name;
this.id = id;
next = null;
prev = null;
}
public course(){
name="";
id=0;
next = null;
prev = null;
}
}
public class Department
{
// 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.prev = newNode;
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 = tail.prev;
}else
{ // case we remove a node in the middle
prev.next = mynode.next;
tail.prev = mynode.prev; // 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 void printBackwards()
{
if( tail==null ) { // the list is empty
System.out.println("this list is empty");
return;
}
course probe = tail;
System.out.println(" all course in the department are ");
do {
System.out.println("course id"+probe.id + "course name " + probe.name );
probe = probe.prev;
} while ( probe!=null );
}
}
DoublyLinkedList.java
package linkedlist;
import java.util.Scanner;
public class DoublyLinkedList {
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 courses to add......");
int size=input.nextInt();
System.out.println("Enter the course");
for(int i=0;i<size;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("--------------PRINT BACKWARDS---------------");
db.printBackwards();
System.out.println("-----------------------------");
System.out.println("There are "+db.countcourses()+" courses in the department");
System.out.println("-----------------------------");
}
}
OUTPUT:
Welcome to PMU department
please enter the number of courses to add......
3
Enter the course
Course id101
Course name CSE
courseCSE with id 101 has been added.
The course with id 101already exist
Course id102
Course name ECE
courseECE with id 102 has been added.
The course with id 102already exist
Course id103
Course name MECH
courseMECH with id 103 has been added.
The course with id 103already exist
-------------------------
all course in the department are
course id103course name MECH
course id102course name ECE
course id101course name CSE
-------------------------
Enter the id of course to be deleted...
102
course: ECE is removed
-----------------------------
all course in the department are
course id103course name MECH
course id101course name CSE
--------------PRINT BACKWARDS---------------
all course in the department are
course id101course name CSE
course id103course name MECH
-----------------------------
There are2 courses in the department
-----------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.