Complete the constructor of class HouseList which is suppose

Complete the constructor of class HouseList, which is supposed to be a singly-linked list of Houses with a load (dummy) node: public class HouseList{ public class HouseList Node{ public HouseList Node first; public House Data; public HouseList Node last; public HouseList Node Next; public int length; } public HouseList () } Assuming class House has a method called get Rooms which returns as an integer the number of rooms in the house, write a method for class HouseList that will return how many houses on the List which have a given number of rooms. Public int count Houses (int Number of Rooms) {

Solution


// constructor

public HouseList(){
  
   // creating Dummy node
   HouseListNode dummy = new HouseListNode();
   dummy.Data= null; // storing \'null\' as House in dummy node of list
   dummy.Next = null; // initially dummy node pointing to null

   // initializing first and last node with dummy node
   first = dummy;
   last = dummy;

   length = 0;
}


// Hi here you have not mentioned about the function that returns the Number of Rooms in a House (in side House Class)
// I am assuming that \'getNumOfRooms()\' is a method inside House class that returns the number of Rooms

public int countHouses(int numberOfRooms){
  
   // list is empty
   if(first.Next == null){
       return 0;
   }

   HouseListNode temp = first.net; // getting first valid node of list

   int countHouse = 0;

   while(temp != null){

       if(temp.Data.getNumOfRooms() == numberOfRooms)
           countHouse++;

       temp = temp.Next;
   }

   return countHouse;
}

 Complete the constructor of class HouseList, which is supposed to be a singly-linked list of Houses with a load (dummy) node: public class HouseList{ public cl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site