Hello, i am working on java program which implements Graph as an
adjacency list structure. code is the implementation of
RailNetwork.
i need help to change the code so it could hold String instead of
integers. so, for example, Station "Station name" is connected to
"Station name"
and also i need to add class edge which basically will be a direction for those give stations. so the output will be like
"Station name" is connected to "Station name" with "this direction" (north line for example)
Thank you
import java.util.LinkedList;
// Undirected graph reprsenting rail network
public class RailNetwork {
int station; // number of stations
LinkedList railLinks[]; // Adjacency List for rail links between
stations
// constructor
public RailNetwork(int station) {
this.station = station;
railLinks = new LinkedList[station];
for (int i = 0; i < station ; i++) {
railLinks[i] = new LinkedList<>(); // initialize linked lists
representing rail links
}
}
// function to add rail links between source station and
destionation station in both directions
public void addRailLink(int sourceStaion, int
destinationStaion){
railLinks[sourceStaion].addFirst(destinationStaion); //add rail link from source station to destination station
//add back rail link from source station to destination
station
railLinks[destinationStaion].addFirst(sourceStaion);
}
// function to print rail network
public void printRailNetwork(){
for (int k = 0; k < station ; k++) {
if(railLinks[k].size() > 0) { // if size of raillinks is greater
than zero loop throught them to print
for (int l = 0; l < railLinks[k].size(); l++) {
System.out.print("Station " + k + " is connected to: ");
System.out.println(railLinks[k].get(l) + " "); // print rail
link
}
System.out.println();
}
}
}
public static void main(String[] args) {
RailNetwork railNetwork = new RailNetwork(5); // a railnetwork
object of 5 stations
railNetwork.addRailLink(1,2); // add rail link between 1 and
2
railNetwork.addRailLink(1,3); // add rail link between 1 and
3
railNetwork.addRailLink(2,4); // add rail link between 2 and
4
railNetwork.addRailLink(2,0); // add rail link between 2 and
0
railNetwork.addRailLink(2,3); // add rail link between 2 and
3
railNetwork.addRailLink(4,0); // add rail link between 4 and
0
railNetwork.addRailLink(0,3); // add rail link between 0 and
3
railNetwork.printRailNetwork(); // print rail network
}
}
output
Station 0 is connected to: 3
Station 0 is connected to: 4
Station 0 is connected to: 2
Station 1 is connected to: 3
Station 1 is connected to: 2
Station 2 is connected to: 3
Station 2 is connected to: 0
Station 2 is connected to: 4
Station 2 is connected to: 1
Station 3 is connected to: 0
Station 3 is connected to: 2
Station 3 is connected to: 1
Station 4 is connected to: 0
Station 4 is connected to: 2
import java.util.LinkedList;
// Undirected graph reprsenting rail network
public class RailNetwork {
int station; // number of stations
LinkedList railLinks[]; // Adjacency List for rail
links between stations
String[] stationNamesArray;
// constructor
public RailNetwork(int station) {
this.station = station;
railLinks = new LinkedList[station
+ 1];
stationNamesArray = new
String[station + 1];
for (int i = 0; i <= station;
i++) {
stationNamesArray[i] = "empty";
railLinks[i] =
new LinkedList<>(); // initialize linked lists representing
rail links
}
}
// function to add rail links between source
station and destionation station in
// both directions
public void addRailLink(String sourceStation, String
destinationStation, String direction) {
int sourceStationIndex = getStationIndex(sourceStation);
if (sourceStationIndex < 0
&& sourceStationIndex > -100000000) {
stationNamesArray[-sourceStationIndex] = sourceStation;
sourceStationIndex = Math.abs(sourceStationIndex);
}
int destinationStationIndex = getStationIndex(destinationStation);
if (destinationStationIndex <
0 && destinationStationIndex > -100000000) {
stationNamesArray[-destinationStationIndex] =
destinationStation;
destinationStationIndex = Math.abs(destinationStationIndex);
}
railLinks[sourceStationIndex].addFirst(new
Edge(destinationStationIndex, direction)); // add rail link
railLinks[destinationStationIndex].addFirst(new
Edge(sourceStationIndex, getOppositeDirection(direction))); // add
backward link
}
// function to get station index
public int getStationIndex(String stationName) {
for (int i = 1; i <
stationNamesArray.length; i++) {
if
(stationNamesArray[i] == stationName)
return i;
if
(stationNamesArray[i] == "empty")
return -i;
}
return -100000000;
}
// function to get backward direction for back link
private String getOppositeDirection(String direction)
{
switch (direction) {
case "North":
return
"South";
case "north":
return
"south";
case "South":
return
"North";
case "south":
return
"north";
case "East":
return
"West";
case "east":
return
"west";
case "West":
return
"East";
case "west":
return
"east";
default:
return "wrong
direction";
}
}
// function to print rail network
public void printRailNetwork() {
for (int k = 1; k <= station;
k++) {
if
(railLinks[k].size() > 0) { // if size of raillinks is greater
than zero loop throught them to print
for (int l = 0; l < railLinks[k].size(); l++)
{
System.out.print("Station " +
stationNamesArray[k] + " is connected to: ");
System.out.println(stationNamesArray[((Edge)
railLinks[k].get(l)).getVertex()] + " in "
+ ((Edge) railLinks[k].get(l)).getDirection() +
" Line"); // print rail link
}
System.out.println();
}
}
}
public static void main(String[] args) {
RailNetwork railNetwork = new
RailNetwork(5); // a railnetwork object of 5 stations
railNetwork.addRailLink("A", "B",
"North"); // add rail link between 1 and 2
railNetwork.addRailLink("A", "C",
"South"); // add rail link between 1 and 3
railNetwork.addRailLink("B", "D",
"West"); // add rail link between 2 and 4
railNetwork.addRailLink("B", "F",
"East"); // add rail link between 2 and 0
railNetwork.addRailLink("B", "C",
"West"); // add rail link between 2 and 3
railNetwork.addRailLink("D", "F",
"North"); // add rail link between 4 and 0
railNetwork.addRailLink("F", "C",
"East"); // add rail link between 0 and 3
railNetwork.printRailNetwork(); //
print rail network
}
class Edge {
int vertex;
String direction;
Edge(int vertex, String
direction) {
this.vertex =
vertex;
this.direction =
direction;
}
public int getVertex() {
return
vertex;
}
public String getDirection()
{
return
direction;
}
}
}
// Ouptut
Hello, i am working on java program which implements Graph as an adjacency list structure. code...
I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...
How would I traverse through this graph? Provide example code, please! class Edge { int src, dest; Edge(int src, int dest) { this.src = src; this.dest = dest; } }; // class to represent a graph object class Graph { // A list of lists to represent adjacency list List<List<Integer>> adj = new ArrayList<>(); // Constructor to construct graph public Graph(List<Edge> edges) { // allocate memory for adjacency list for (int i = 0; i < edges.size(); i++) { adj.add(i,...
Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList { private class Node { private Object data; //Assume data implemented Comparable private Node next, prev; private Node(Object data, Node pref, Node next) { this.data = data; ...
Consider the java Graph class below which represents an undirected graph in an adjacency list. How would you add a method to delete an edge from the graph? // Exercise 4.1.3 (Solution published at http://algs4.cs.princeton.edu/) package algs41; import stdlib.*; import algs13.Bag; /** * The <code>Graph</code> class represents an undirected graph of vertices * named 0 through V-1. * It supports the following operations: add an edge to the graph, * iterate over all of the neighbors adjacent to a vertex....
Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...
JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...
/* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on the graph */ #include <iostream> #include <sstream> #include <fstream> #include <vector> #include <utility> #include <unordered_map> #include <set> #include <queue> using namespace std; // Each vertex has an integer id. typedef vector<vector<pair<int,int>>> adjlist; // Pair: (head vertex, edge weight) adjlist makeGraph(ifstream& ifs); void printGraph(const adjlist& alist); vector<int> BFS(const adjlist& alist, int source); // Return vertices in BFS order vector<int> DFS(const adjlist& alist, int source); //...
a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....
Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...