Purpose: To learn the basics of doubly linked lists
You are to implement 2 classes, Cabin and Train.
The Cabin class (also known as the node class) has the following fields and methods:
private String cabinType
private Cabin nextCabin
private Cabin prevCabin
Cabin(String type, Cabin next, Cabin prev) - A constructor that creates a Cabin object with a set cabinType and the prev/nextCabin fields set
Hint: if we want next/prev to point to null we can pass in null for those values
public Cabin getNextCabin() - returns the nextCabin from the current one
public void setNextCabin(Cabin newCabin) - changes the nextCabin to the cabin passed in
public Cabin getPrevCabin() - returns the prevCabin from the current one
public void setPrevCabin(Cabin newCabin) - changes the prevCabin to the cabin passed in
public String getCabintype() - returns the cabintype
Your train class has the following fields and methods
public Cabin Front (this is also known as head)- stores the first Cabin in the train
public Cabin Caboose (this is also known as tail)- stores the last Cabin in the train
public Train() - A Train constructor which initializes the the Front and Caboose variables to null
public void addCabin(String cabinType) - A method that adds a new Cabin to the end of the Train (aka it becomes the new tail)
public void addCabin(String cabinType, int pos) - A method that adds a new Cabin to the Train at the position passed in, if that position is not accessible such as if we try to add something at pos=45 and there are only 3 elements in the list we don’t add anything
Hint: Use a while loop that both keeps track of if we are at the end of the list and how many indexes we have passed
public Cabin removeCabin() - A method that removes a Cabin from the end of the Train’s cabinManifest, and returns the removed object (aka we remove the tail of the train)
public Cabin removeCabin(int pos) - A method that removes the Cabin at the passed in position from the Train’s cabinManifest, and returns the removed object if the position is not in the linked list nothing is removed we just return null.
Note: The head of the linked list should indexed at 0
Hint: Use a while loop that both keeps track of if we are at the end of the list and how many indexes we have passed
public void swap(int pos1, int pos2) - A method that takes the Cabins at each of the passed in positions and swaps them, if there are not cars at both positions it does nothing
Hint: Similar to the above position dependent methods use a while loop for EACH of the cabins to find them (so we will use 2 seperate loops NOT NESTED) then we can change the next and previous cabin fields
public String toString() - returns a string where each of the Cabin’s cabinType is separated by a comma and space. Ex: “cabintype1, cabintype2, cabintype3”
Note/Hint:It may be helpful to create a helper method that finds a node at a given position in the linked list, that way you would only have to write the code once and could use it in other methods. It is definitely is not necessary, but can help simplify the code.
You also need to create a Driver class with a main method. In main:
Create a Train object
Add Cabins until it is:
”Tanker”,”Passenger1”,”Passenger2”,”Bananas”,”Monkeys” Print out the toString() of the Train object
Call removeCabin() on the Train
Print out the toString() of the Train object
Call removeCabin(2) on the Train
Print out the toString() of the Train object
Call swap(1,2) on the train
Print out the toString() of the Train object
Add a Cabin called “Monkey Madness” to the front of the train
Print out the toString() of the Train object
Example Output:
”Tanker”,”Passenger1”,”Passenger2”,”Bananas”,”Monkeys”
”Tanker”,”Passenger1”,”Passenger2”,”Bananas” “Tanker”,”Passenger1”,”Bananas” “Tanker”,”Bananas”,”Passenger1”
“Monkey Madness”,“Tanker”,”Bananas”,”Passenger1”
//Cabin.java
class Cabin
{
//cabin fields
private String cabinType;
private Cabin nextCabin;
private Cabin prevCabin;
//constructor
Cabin(String type, Cabin next, Cabin prev)
{
this.cabinType = type;
this.nextCabin = next;
this.prevCabin = prev;
}
//getters
public Cabin getNextCabin()
{
return this.nextCabin;
}
public Cabin getPrevCabin()
{
return this.prevCabin;
}
public String getCabinType()
{
return this.cabinType;
}
//setters
public void setPrevCabin(Cabin prev)
{
this.prevCabin = prev;
}
public void setNextCabin(Cabin next)
{
this.nextCabin = next;
}
}
//Train.java
class Train
{
//Front and Tail cabins
public Cabin Front;
public Cabin Caboose;
//to count number of cabins
int len;
Train()
{
Front = null;
Caboose = null;
len = 0;
}
public void addCabin(String cabinType)
{
Cabin newCabin = new
Cabin(cabinType,null,null);
//initially there are no
cabins
//set front and tail cabbins to
newly created cabin
if(Front == null)
{
Front =
newCabin;
Caboose =
newCabin;
}
//if not add cabin to the
Caboose
//first link new cabin previous to
the Caboose
newCabin.setPrevCabin(Caboose);
//then link Caboose next to
newCabin
Caboose.setNextCabin(newCabin);
//and finally set caboose to
newCabin
Caboose = newCabin;
len++;
}
public void addCabin(String cabinType,int pos)
{
if(pos < len)
{
//here it is 0
based index
// last element
is always len - 1 not len
//if the given
value is the end position so call addCabin which adds
//cabin at
end
if(pos ==
0)
{
Cabin cabin = new
Cabin(cabinType,null,null);
//link new cabin next to the front
cabin.setNextCabin(Front);
//link front prev to new cabin
Front.setPrevCabin(cabin);
//finally make front as new cabin
Front = cabin;
}
else if(pos ==
len-1)
{
addCabin(cabinType);
}
else
{
Cabin temp = Front;
int ind = 0;
while(temp.getNextCabin()!=null && ind
< pos)
{
temp =
temp.getNextCabin();
ind++;
}
Cabin newCabin = new
Cabin(cabinType,null,null);
Cabin next = temp.getNextCabin();
newCabin.setPrevCabin(temp);
newCabin.setNextCabin(next);
next.setPrevCabin(newCabin);
temp.setNextCabin(newCabin);
}
len++;
}
}
//remove the last cabin
public Cabin removeCabin()
{
if(len == 1)
{
Cabin temp =
Front;
Front =
null;
Caboose =
null;
len--;
return
temp;
}
else
{
Cabin temp =
Caboose;
Caboose =
Caboose.getPrevCabin();
Caboose.setNextCabin(null);
len--;
return
temp;
}
}
//remove cabin at given position
public Cabin removeCabin(int pos)
{
if(pos < len)
{
if(pos ==
0)
{
if(len == 1)
{
Cabin temp = Front;
Front = null;
Caboose = null;
len--;
return temp;
}
else
{
Cabin temp = Front;
Front =
Front.getNextCabin();
Front.setPrevCabin(null);
len--;
return temp;
}
}
else if(pos ==
len-1)
{
Cabin temp = Caboose;
Caboose = Caboose.getPrevCabin();
Caboose.setNextCabin(null);
len--;
return temp;
}
else
{
Cabin temp = Front;
int ind = 0;
while(ind < pos )
{
temp =
temp.getNextCabin();
ind++;
}
Cabin next = temp.getNextCabin();
Cabin prev = temp.getPrevCabin();
prev.setNextCabin(next);
next.setPrevCabin(prev);
len--;
return temp;
}
}
return null;
}
//get a cabin at particualar position
public Cabin getCabin(int pos)
{
Cabin temp = Front;
int ind = 0;
while(ind < pos)
{
temp =
temp.getNextCabin();
ind++;
}
return temp;
}
//method that adds the cabin after the previous
public void addCabinAfter(Cabin node,Cabin prev)
{
if (prev != null)
{
node.setNextCabin(prev.getNextCabin());
prev.setNextCabin(node);
}
else
{
node.setNextCabin(Front);
Front =
node;
}
node.setPrevCabin(prev);
if(node.getNextCabin() !=
null)
{
node.getNextCabin().setPrevCabin(node);
}
}
//method that removes the given node
void remove(Cabin node)
{
if
(node.getPrevCabin()!=null)
{
node.getPrevCabin().setNextCabin(node.getNextCabin());
}
else
{
Front =
node.getNextCabin();
}
if
(node.getNextCabin()!=null)
{
node.getNextCabin().setPrevCabin(node.getPrevCabin());
}
else
{
Caboose =
node.getPrevCabin();
}
node.setPrevCabin(null);
node.setNextCabin(null);
}
//method that swaps the two positions given
public void swap(int pos1,int pos2)
{
if(pos1 < len && pos2
< len)
{
Cabin
first,second;
if(pos1 >
pos2)
{
int tempp = pos1;;
pos1 = pos2;
pos2 = tempp;
}
first =
getCabin(pos1);
second =
getCabin(pos2);
if (first ==
second) return;
Cabin
firstPrev = first.getPrevCabin();
Cabin secondPrev
= second.getPrevCabin();
if (firstPrev ==
second)
{
removeCabin(pos1);
addCabinAfter(first, secondPrev);
}
else if
(secondPrev == first)
{
removeCabin(pos2);
addCabinAfter(second,firstPrev);
}
else
{
removeCabin(pos1);
removeCabin(pos2);
addCabinAfter(first,secondPrev);
addCabinAfter(second,firstPrev);
}
}
}
public String toString()
{
Cabin temp = Front;
if(temp == null)
{
return "No
Cabins in train";
}
String res = "";
while(temp!=null)
{
res +=
temp.getCabinType();
temp =
temp.getNextCabin();
if(temp!=null)
{
res += ", ";
}
}
res +="\n";
return res;
}
}
//Main.java
class Main
{
public static void main(String [] args)
{
Train train = new Train();
train.toString();
train.addCabin("Tanker");
train.addCabin("Passenger1");
train.addCabin("Passenger2");
train.addCabin("Bananas");
train.addCabin("Monkeys");
System.out.println("Printing Cabins
after Insertion");
System.out.println(train.toString());
System.out.println("Printing Cabins
after Removal");
train.removeCabin();
System.out.println(train.toString());
System.out.println("Printing Cabins
after Removal");
train.removeCabin(2);
train.swap(1,2);
System.out.println("Printing Cabins
after Swapping");
System.out.println(train.toString());
train.addCabin("Monkeys
Madness",0);
System.out.println("Printing Cabins
after Adding Monkeys Madness to the Front");
System.out.println(train.toString());
}
}

Purpose: To learn the basics of doubly linked lists You are to implement 2 classes, Cabin...
Please please help me with this code please Purpose: To learn the basics of linked lists You are to implement 2 classes, Scene and Movie. Note: Please dont use inbuild linkedList methods and functions The Scene class has the following fields and methods: p rivate String event private Scene nextScene Public Scene(String event) - A constructor that creates a Scene object with the event set and nextScene set to null Public Scene(String event, Scene next) - A constructor that creates...
Needs Help with Java Programming language! Lights Camera Action Purpose: To learn the basics of linked lists You are to implement 2 classes, Scene and Movie. The Scene class has the following fields and methods: private String event private Scene nextScene Public Scene(String event) - A constructor that creates a Scene object with the event set and nextScene set to null Public Scene(String event, Scene next) - A constructor that creates a Scene object with the event and the nextScene...
This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...
CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...
Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...
In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...
In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...