Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public static void main(String[] args) { GraphWithAdjacencyList g = new GraphWithAdjacencyList(Student.roster); g.addEdge(Student.roster[0], Student.roster[1]); g.addEdge(Student.roster[0], Student.roster[6]); g.addEdge(Student.roster[0], Student.roster[2]); g.addEdge(Student.roster[0], Student.roster[5]); g.addEdge(Student.roster[1], Student.roster[6]); g.addEdge(Student.roster[1], Student.roster[3]); g.addEdge(Student.roster[1], Student.roster[4]);

public static void main(String[] args) { GraphWithAdjacencyList g = new GraphWithAdjacencyList(Student.roster); g.addEdge(Student.roster[0], Student.roster[1]); g.addEdge(Student.roster[0], Student.roster[6]); g.addEdge(Student.roster[0], Student.roster[2]); g.addEdge(Student.roster[0], Student.roster[5]); g.addEdge(Student.roster[1], Student.roster[6]); g.addEdge(Student.roster[1], Student.roster[3]); g.addEdge(Student.roster[1], Student.roster[4]); g.addEdge(Student.roster[2], Student.roster[3]); g.addEdge(Student.roster[4], Student.roster[8]); g.addEdge(Student.roster[5], Student.roster[9]); g.addEdge(Student.roster[6], Student.roster[7]); g.addEdge(Student.roster[7], Student.roster[8]); System.out.println("--- Roster ---"); System.out.println(g.toString()); g.breadthFirstSearch(Student.roster[0]); System.out.println(); g.depthFirstSearch(Student.roster[0]); } }

class Student { public static Student[] roster = {new Student("A", 20, 10000), new Student("B", 30, 10509, new Student("C", 40, 15070), new Student("E", 35, 10400), new Student("F", 48, 10069), new Student("G", 39, 19080), new Student("H", 60, 10430), new Student("Q", 42, 18111), new Student("Y", 23, 18009), new Student("N", 26, 10700) }; private String name; private double age; private int zipcode; public MyGenericLinkedList friends; public Student(String name, double age, int zipcode) { this.name = name; this.age = age; this.zipcode = zipcode; friends = new MyGenericLinkedList(); } public String toString() { return "(" + name + ", " + age + ", " + zipcode + ")"; } }

class GraphWithAdjacencyList { MyGenericLinkedList studentList; public GraphWithAdjacencyList(Student[] s) { studentList = new MyGenericLinkedList(); for(int i = 0; i < s.length; i++) { studentList.add(s[i]); } } public void addEdge(Student x, Student y) { x.friends.add(y); y.friends.add(x); } public void breadthFirstSearch(Student start) { System.out.println("--- Breadth First Search ---"); // Fill in your code here

} public void depthFirstSearch(Student start) { System.out.println("--- Depth First Search ---"); // Fill in your code here } void DFS(Student current, boolean[] visited) { // Fill in your code// } public String toString() { // Fill in your code here

} }

class MyGenericQueue { private Node front; private Node back;

public MyGenericQueue() { front = null; back = null; }

public void add(T data) { Node temp = back; back = new Node(data); if(front == null) front = back; else temp.next = back; } public T peek() { if(front == null) return null; return front.data; }

public T remove() { if(front == null) return null;

Node temp = front; front = front.next; if(front == null) back = null; return (T)temp.data; } }

class MyGenericLinkedList { Node front; int size;

public MyGenericLinkedList() { front = null; size = 0; }

public void add(S value) { if (front == null) { front = new Node(value); size++; } else { Node current = front; while (current.next != null) { current = current.next; } current.next = new Node(value); size++; } } public S get(int index) { if((index < 0) || (index >= size())) return null; Node current = front; for (int i = 0; i < index; i++) { current = current.next; } return (S)current.data; } public int indexOf(S data) { int index = 0; Node current = front; while (current != null) { if(current.data == data) return index; current = current.next; index++; } return -1; }

public void remove(int index) { if((index < 0) || (index >= size())) return; if (index == 0) { front = front.next; size--; } else { Node current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; size--; } } public int size() { return size; } }

class Node { X data; Node next;

public Node(X data) { this.data = data; this.next = null; }

public Node(X data, Node next) { this.data = data; this.next = next; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+what kinds of policies and practices should be developed?

Answered: 1 week ago