Complete the constructor of class HouseList, which is supposed to be a singly-li
ID: 3930320 • Letter: C
Question
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) {Explanation / Answer
// 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.