Lists and Arrays: Checking Network Redundancy in Java

import java.io.*;
import java.util.*;

//  No pointer to functions - use interface instead.
interface DisplayFunc
{
   public void display (Object obj);
}

class CityObjectDisplayFunc implements DisplayFunc
{
   public void display (Object obj)
   {
      System.out.print(((City)obj).city()+ " "); 
   }
}

// Array class unnecessary 

// Objects of cell class hold actually interesting objects, such
// as cities, in a linked list.
class Cell
{
   Object object;
   Cell next;

   Cell(Object obj, Cell lst)
   {
      object = obj;
      next = lst;
   }
}

// Queue class puts together cells to make a queue.
class Queue
{
   Cell head;
   Cell tail;
   DisplayFunc dispfn;

   Queue(DisplayFunc d)
   {
      dispfn = d;
      head = null;
      tail = null;
   }

   void enqueue(Object obj)
   {
      if (obj == null) return;
      Cell h = new Cell(obj, null);
      if (head == null)
         head = h;
      else
         tail.next = h;
      tail = h;
   }

   Object dequeue()
   {
      if (head == null) return null;
      Cell ptr = head;
      Object object = head.object;
      head = head.next;
      if (head == null) tail = null;
      return object;
   }

   void display()
   {
     if (head == null) { System.out.println("(empty)");  return; }
     for (Cell t=head ; t != null ; t=t.next) dispfn.display(t.object);
     System.out.println();
   }

   boolean empty() { return head == null; }
}

// City objects are what we are primarily interested in here.
class City
{
   int cty;
   int prnt;

   City (int c, int p) { cty = c; prnt = p; }
   int parent()  { return prnt; }
   int city()    { return cty; }
}

public class A
{
   static int max (int a, int b) { if (a < b) return b; else return a; }

   /***--------------------------------------------------***/
   /*** Redundancy:                                      ***/
   /***   Input:  A network (n by n array of 0-1 values) ***/
   /***   Output: 1 if network is redundant              ***/
   /***           0 otherwise                            ***/
   /*** Checks network links for redundant connections   ***/
   /***--------------------------------------------------***/
   static int redundancy(boolean[][] links, int cities)
   {
      Queue s = new Queue(new CityObjectDisplayFunc());
      boolean visited[] = new boolean[cities+1];
      City current;
      int i;

      s.enqueue (new City(0,-1));
      s.display();
      while (!s.empty())
      {
         current = (City)s.dequeue();
         s.display();
         if (visited[current.city()]) continue;
         visited[current.city()] = true;
         for (i = 0 ; i <= cities ; i++)
         {
            if (!links[current.city()][i] || current.city() == i) continue;
            if (!visited[i])
            {
               s.enqueue(new City(i, current.city()));
               s.display();
            }
            else
            if (i != current.parent()) return 1;
         }
      }
      return 0;
   }

   public static void main(String arg[])
   {
      boolean[][] links = new boolean[200][200];
      int city1, city2, cost, cities=0;

      // Read data from file specified in command line
      try
      {
         String file = arg[0];
         DataInputStream is = new DataInputStream(new FileInputStream(file));
         while (true)
	 {
            String s = is.readLine();
            try
	    {
               StringTokenizer t = new StringTokenizer(s," ");
               city1=Integer.parseInt(t.nextToken());
               city2=Integer.parseInt(t.nextToken());
               links[city1][city2] = true;
               cities = max(max(cities, city1), city2);
	    }
            catch (NullPointerException e){ break; }
	 }
      }
      catch (Exception e)
      {
         System.out.println("Usage: java A ");
         System.exit(0);
      }

      int result = redundancy(links, cities);
      System.out.println("<<" + result + ">>");
   }
}

//Sample Output:
//   0 
//   (empty)
//   3 
//   3 4 
//   3 4 5 
//   4 5 
//   4 5 2 
//   4 5 2 4 
//   4 5 2 4 5 
//   5 2 4 5 
//   <<1>>

// On Input:
//   0 3
//   0 4
//   0 5
//   1 2
//   1 5
//   2 1
//   2 3
//   2 6
//   3 0
//   3 2
//   3 4
//   3 5
//   4 0
//   4 3
//   4 6
//   5 0
//   5 1
//   5 3
//   6 2
//   6 4