Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:23:54+00:00 2026-06-15T05:23:54+00:00

I am fairly new to java and I have been struggling with this exercise

  • 0

I am fairly new to java and I have been struggling with this exercise for two weeks now(It’s an homework exercise in my school). I need to create a topological sort and print out all of the possible connections. I have read a lot about topological sorting now, but we have this certain line of code that we have to work with. I’m pretty sure I could do the topological sorting when I have the list of vertices. My problem is, I don’t know how to list all of the vertices from this given code. Could anyone give me some tips or leads or perhaps an example, I would really really appreciate it.

Here is the given code we need to work with:

import java.util.*;

public class Answer {

   public static void main (String[] args) {
      Answer a = new Answer();
      a.run();
   }

   public void run() {

      // TODO!!! YOUR TESTS HERE!

      Graph g = new Graph ("G");
      Vertex a = new Vertex ("A");
      Vertex b = new Vertex ("B");
      Vertex c = new Vertex ("C");
      g.first = a;
      a.next = b;
      b.next = c;
      Edge ab = new Edge ("AB");
      Edge ac = new Edge ("AC");
      Edge ba = new Edge ("BA");
      Edge ca = new Edge ("CA");
      a.first = ab;
      b.first = ba;
      c.first = ca;
      ab.next = ac;
      ab.target = b;
      ac.target = c;
      ba.target = a;
      ca.target = a;
      System.out.println (g);
   }


   class Vertex {

      String id;
      Vertex next;
      Edge first;

      Vertex (String s, Vertex v, Edge e) {
         id = s;
         next = v;
         first = e;
      } 

      Vertex (String s) {
         this (s, null, null);
      }

      @Override
      public String toString() {
         return id;
      }

      // TODO!!! Your Vertex methods here!

   } // Vertex


   class Edge {

      String id;
      Vertex target;
      Edge next;

      Edge (String s, Vertex v, Edge e) {
         id = s;
         target = v;
         next = e;
      }

      Edge (String s) {
         this (s, null, null);
      }

      @Override
      public String toString() {
         return id;
      }

      // TODO!!! Your Edge methods here!

   } // Edge


   class Graph {

      String id;
      Vertex first;

      Graph (String s, Vertex v) {
         id = s;
         first = v;
      }

      Graph (String s) {
         this (s, null);
      }

      @Override
      public String toString() {
         String nl = System.getProperty ("line.separator");
         StringBuffer sb = new StringBuffer (nl);
         sb.append (id + nl);
         Vertex v = first;
         while (v != null) {
            sb.append (v.toString() + " --> ");
            Edge e = v.first;
            while (e != null) {
               sb.append (e.toString());
               sb.append ("(" + v.toString() + "->" 
                  + e.target.toString() + ") ");
               e = e.next;
            }
            sb.append (nl);
            v = v.next;
         }
         return sb.toString();
      }

      // TODO!!! Your Graph methods here!

   } // Graph
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:23:55+00:00Added an answer on June 15, 2026 at 5:23 am

    Apparently, the graph has a reference to the first vertex, and the vertices themselves are linked together into a singly linked list. This code should be all you need to collect the vertices into a Java list:

    public List<Vertex> allVertices(Graph g) {    
      final List<Vertex> vertices = new ArrayList<>();
      for (Vertex v = g.first; v != null; v = v.next)
         vertices.add(v);
      return vertices;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on Eclipse recently. I am fairly new to java programming,
I'm fairly new to programming in general but have been working with Java for
I'm relatively new to java (specifically swing) and have recently been making some fairly
I'm fairly new to Java development, and people have been suggesting I use Eclipse
I'm fairly new to programming and have been focussing a great deal on Java
I have been struggling with this for the entire day now, I hope somebody
I'm still fairly new to Java, but I'm fairly sure this shouldn't happen. There's
First of all, I'm fairly new to Java, so sorry if this question is
I am a brand new Java developer (I have been working in asp.net) and
I am fairly new to JAVA and especially concurrency, so probably/hopefully this is fairly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.